changeset 186:25447caf1b4b

Change command main() functions to return void, and exit(toys.exitval) from the toybox infrastructure instead. Eliminates a return call from each command.
author Rob Landley <rob@landley.net>
date Thu, 29 Nov 2007 18:14:37 -0600
parents 29e2051296fd
children c983a0af6d4e
files main.c toys/basename.c toys/bzcat.c toys/catv.c toys/count.c toys/df.c toys/dirname.c toys/dmesg.c toys/echo.c toys/false.c toys/hello.c toys/help.c toys/mdev.c toys/mke2fs.c toys/mkfifo.c toys/oneit.c toys/pwd.c toys/readlink.c toys/sha1.c toys/sleep.c toys/sync.c toys/touch.c toys/toylist.h toys/toysh.c toys/true.c toys/tty.c toys/which.c toys/yes.c www/index.html
diffstat 28 files changed, 53 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/main.c	Thu Nov 29 18:14:37 2007 -0600
@@ -80,13 +80,12 @@
 
 	which = toy_find(argv[0]);
 	if (!which) return;
-
 	toy_init(which, argv);
-
-	exit(toys.which->toy_main());
+	toys.which->toy_main();
+	exit(toys.exitval);
 }
 
-int toybox_main(void)
+void toybox_main(void)
 {
 	static char *toy_paths[]={"usr/","bin/","sbin/",0};
 	int i, len = 0;
@@ -115,7 +114,6 @@
 		}
 	}
 	putchar('\n');
-	return 0;
 }
 
 int main(int argc, char *argv[])
@@ -132,5 +130,6 @@
 	}
 
 	toys.argv = argv-1;
-	return toybox_main();
+	toybox_main();
+	return 0;
 }
--- a/toys/basename.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/basename.c	Thu Nov 29 18:14:37 2007 -0600
@@ -6,7 +6,7 @@
 
 #include "toys.h"
 
-int basename_main(void)
+void basename_main(void)
 {
 	char *name = basename(*toys.optargs);
 	char *suffix = toys.optargs[1];
@@ -15,5 +15,4 @@
 		if (end>name && !strcmp(end,suffix)) *end=0;
 	}
 	puts(name);
-	return 0;
 }
--- a/toys/bzcat.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/bzcat.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,9 +5,7 @@
 
 #include "toys.h"
 
-int bzcat_main(void)
+void bzcat_main(void)
 {
 	bunzipStream(0, 1);
-
-	return 0;
 }
--- a/toys/catv.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/catv.c	Thu Nov 29 18:14:37 2007 -0600
@@ -34,21 +34,19 @@
 			}
 			if (c < 32) {
 				if (c == 10) {
-					if (toys.optflags & 1) putchar('$');
+					if (toys.optflags & 1) xputc('$');
 				} else if (toys.optflags & (c==9 ? 2 : 4)) {
 					printf("^%c", c+'@');
 					continue;
 				}
 			}
-			putchar(c);
+			xputc(c);
 		}
 	}
 }
 
-int catv_main(void)
+void catv_main(void)
 {
 	toys.optflags^=4;
 	loopfiles(toys.optargs, do_catv);
-
-	return toys.exitval;
 }
--- a/toys/count.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/count.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,7 +5,7 @@
 
 #include "toys.h"
 
-int count_main(void)
+void count_main(void)
 {
 	uint64_t size = 0;
 	int len;
@@ -18,5 +18,4 @@
 		fdprintf(2, "%"PRIu64" bytes\r", size);
 	}
 	fdprintf(2,"\n");
-	return 0;
 }
--- a/toys/df.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/df.c	Thu Nov 29 18:14:37 2007 -0600
@@ -55,7 +55,7 @@
 	}
 }
 
-int df_main(void)
+void df_main(void)
 {
 	struct mtab_list *mt, *mt2, *mtlist;
 
@@ -114,6 +114,4 @@
 	}
 
 	if (CFG_TOYBOX_FREE) llist_free(mtlist, NULL);
-
-	return 0;
 }
--- a/toys/dirname.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/dirname.c	Thu Nov 29 18:14:37 2007 -0600
@@ -7,8 +7,7 @@
 #include "toys.h"
 #include <libgen.h>
 
-int dirname_main(void)
+void dirname_main(void)
 {
 	puts(dirname(*toys.optargs));
-	return 0;
 }
--- a/toys/dmesg.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/dmesg.c	Thu Nov 29 18:14:37 2007 -0600
@@ -10,7 +10,7 @@
 
 #define TT toy.dmesg
 
-int dmesg_main(void)
+void dmesg_main(void)
 {
 	// For -n just tell kernel to which messages to keep.
 	if (toys.optflags & 2) {
@@ -30,11 +30,9 @@
 		// Display data, filtering out level markers.
 		for (i=0; i<size; ) {
 			if (last=='\n' && data[i]=='<') i += 3;
-			else putchar(last = data[i++]);
+			else xputc(last = data[i++]);
 		}
-		if (last!='\n') putchar('\n');
+		if (last!='\n') xputc('\n');
 		if (CFG_TOYBOX_FREE) free(data);
 	}
-
-	return 0;
 }
--- a/toys/echo.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/echo.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,7 +5,7 @@
 
 #include "toys.h"
 
-int echo_main(void)
+void echo_main(void)
 {
 	int i = 0;
 	char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v";
@@ -48,5 +48,4 @@
 	if (!(toys.optflags&1)) xputc('\n');
 done:
 	xflush();
-	return 0;
 }
--- a/toys/false.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/false.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,7 +5,7 @@
 
 #include "toys.h"
 
-int false_main(void)
+void false_main(void)
 {
-	return 1;
+	toys.exitval = 1;
 }
--- a/toys/hello.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/hello.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,8 +5,7 @@
 
 #include "toys.h"
 
-int hello_main(void)
+void hello_main(void)
 {
 	printf("Hello world\n");
-	return 0;
 }
--- a/toys/help.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/help.c	Thu Nov 29 18:14:37 2007 -0600
@@ -14,7 +14,7 @@
 #include "toys/toylist.h"
 ;
 
-int help_main(void)
+void help_main(void)
 {
 	struct toy_list *t = toy_find(*toys.optargs);
 	int i = t-toy_list;
@@ -29,5 +29,4 @@
 	}
 
 	fprintf(toys.exithelp ? stderr : stdout, "%s", s);
-	return 0;
 }
--- a/toys/mdev.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/mdev.c	Thu Nov 29 18:14:37 2007 -0600
@@ -180,17 +180,15 @@
 	closedir(dir);
 }
 
-int mdev_main(int argc, char *argv[])
+void mdev_main(int argc, char *argv[])
 {
 	if (toys.optflags) {
 		strcpy(toybuf, "/sys/block");
 		find_dev(toybuf);
 		strcpy(toybuf, "/sys/class");
 		find_dev(toybuf);
-		return 0;
+		return;
 	}
 
 	// hotplug support goes here
-
-	return 0;
 }
--- a/toys/mke2fs.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/mke2fs.c	Thu Nov 29 18:14:37 2007 -0600
@@ -339,7 +339,7 @@
 // The first argument is the name of the file to create.  If it already
 // exists, that size will be used.
 
-int mke2fs_main(void)
+void mke2fs_main(void)
 {
 	int i, temp;
 	off_t length;
@@ -561,8 +561,6 @@
 		// Write data blocks (TODO)
 		put_zeroes((end-start) * TT.blocksize);
 	}
-
-	return 0;
 }
 
 // Scratch pad:
--- a/toys/mkfifo.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/mkfifo.c	Thu Nov 29 18:14:37 2007 -0600
@@ -7,7 +7,7 @@
 
 #include "toys.h"
 
-int mkfifo_main(void)
+void mkfifo_main(void)
 {
 	char *arg;
 	int i;
@@ -24,6 +24,4 @@
 	for (i = 0; (arg = toys.optargs[i]); i++)
 		if (mkfifo(arg, mode))
 			perror_exit(arg);
-
-	return 0;
 }
--- a/toys/oneit.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/oneit.c	Thu Nov 29 18:14:37 2007 -0600
@@ -16,7 +16,7 @@
 // PID 1 then reaps zombies until the child process it spawned exits, at which
 // point it calls sync() and reboot().  I could stick a kill -1 in there.
 
-int oneit_main(void)
+void oneit_main(void)
 {
   int i;
   pid_t pid;
@@ -38,7 +38,7 @@
     open("/dev/tty0",O_RDWR);
   }
 
-  // Can't xexec() here because we vforked so we don't want to error_exit().
+  // Can't xexec() here, because we vforked so we don't want to error_exit().
   toy_exec(toys.optargs);
   execvp(*toys.optargs, toys.optargs);
   _exit(127);
--- a/toys/pwd.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/pwd.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,12 +5,10 @@
 
 #include "toys.h"
 
-int pwd_main(void)
+void pwd_main(void)
 {
 	char *pwd = xgetcwd();
 
 	xprintf("%s\n", pwd);
 	if (CFG_TOYBOX_FREE) free(pwd);
-
-	return 0;
 }
--- a/toys/readlink.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/readlink.c	Thu Nov 29 18:14:37 2007 -0600
@@ -6,15 +6,12 @@
 
 #include "toys.h"
 
-int readlink_main(void)
+void readlink_main(void)
 {
 	char *s = xreadlink(*toys.optargs);
 
 	if (s) {
 		xputs(s);
 		if (CFG_TOYBOX_FREE) free(s);
-		return 0;
-	}
-
-	return 1;
+	} else toys.exitval = 1;
 }
--- a/toys/sha1.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/sha1.c	Thu Nov 29 18:14:37 2007 -0600
@@ -46,7 +46,7 @@
 	int i;
 
 	for (i = 0; i < 20; i++) printf("%02x", this[i]);
-	putchar('\n');
+	xputc('\n');
 }
 
 /* Hash a single 512-bit block. This is the core of the algorithm. */
--- a/toys/sleep.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/sleep.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,7 +5,7 @@
 
 #include "toys.h"
 
-int sleep_main(void)
+void sleep_main(void)
 {
-	return sleep(atol(*toys.optargs));
+	toys.exitval = sleep(atol(*toys.optargs));
 }
--- a/toys/sync.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/sync.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,8 +5,7 @@
 
 #include "toys.h"
 
-int sync_main(void)
+void sync_main(void)
 {
 	sync();
-	return 0;
 }
--- a/toys/touch.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/touch.c	Thu Nov 29 18:14:37 2007 -0600
@@ -20,7 +20,7 @@
 #define TIME		0x10
 #define LENGTH		0x20
 
-int touch_main(void)
+void touch_main(void)
 {
 	char *arg;
 	int i, set_a, set_m, create;
@@ -87,6 +87,4 @@
 error:
 			perror_exit(arg);
 	}
-
-	return 0;
 }
--- a/toys/toylist.h	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/toylist.h	Thu Nov 29 18:14:37 2007 -0600
@@ -9,7 +9,7 @@
 // file twice (with different macros) to populate toy_list[].
 
 #ifndef NEWTOY
-#define NEWTOY(name, opts, flags) int name##_main(void);
+#define NEWTOY(name, opts, flags) void name##_main(void);
 #define OLDTOY(name, oldname, opts, flags)
 
 struct df_data {
@@ -90,7 +90,7 @@
 
 extern struct toy_list {
 	char *name;
-	int (*toy_main)(void);
+	void (*toy_main)(void);
 	char *options;
 	int flags;
 } toy_list[];
--- a/toys/toysh.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/toysh.c	Thu Nov 29 18:14:37 2007 -0600
@@ -133,7 +133,8 @@
 		memcpy(&temp, &toys, sizeof(struct toy_context));
 		bzero(&toys, sizeof(struct toy_context));
 		toy_init(tl, cmd->argv);
-		cmd->pid = tl->toy_main();
+		tl->toy_main();
+		cmd->pid = toys.exitval;
 		free(toys.optargs);
 		memcpy(&toys, &temp, sizeof(struct toy_context));
 	} else {
@@ -184,19 +185,18 @@
 	}
 }
 
-int cd_main(void)
+void cd_main(void)
 {
 	char *dest = *toys.optargs ? *toys.optargs : getenv("HOME");
 	if (chdir(dest)) error_exit("chdir %s",dest);
-	return 0;
 }
 
-int exit_main(void)
+void exit_main(void)
 {
 	exit(*toys.optargs ? atoi(*toys.optargs) : 0);
 }
 
-int toysh_main(void)
+void toysh_main(void)
 {
 	FILE *f;
 
@@ -210,12 +210,12 @@
 		size_t cmdlen = 0;
 		for (;;) {
 			char *command = 0;
-			if (!f) putchar('$');
+			if (!f) xputc('$');
 			if (1 > getline(&command, &cmdlen, f ? : stdin)) break;
 			handle(command);
 			free(command);
 		}
 	}
 
-	return 1;
+	toys.exitval = 1;
 }
--- a/toys/true.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/true.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,7 +5,7 @@
 
 #include "toys.h"
 
-int true_main(void)
+void true_main(void)
 {
-	return 0;
+	return;
 }
--- a/toys/tty.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/tty.c	Thu Nov 29 18:14:37 2007 -0600
@@ -6,12 +6,12 @@
 
 #include "toys.h"
 
-int tty_main(void)
+void tty_main(void)
 {
 	char *name = ttyname(0);
 	if (!toys.optflags) {
 		if (name) puts(name);
 		else puts("Not a tty");
 	}
-	return !name;
+	toys.exitval = !name;
 }
--- a/toys/which.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/which.c	Thu Nov 29 18:14:37 2007 -0600
@@ -52,16 +52,12 @@
 	return 0;
 }
 
-int which_main(void)
+void which_main(void)
 {
-	int rc = 0;
-
-	if (!*toys.optargs) rc++;
+	if (!*toys.optargs) toys.exitval++;
 	else {
 		int i;
-		for (i=0; toys.optargs[i]; i++) rc |= which_in_path(toys.optargs[i]);
+		for (i=0; toys.optargs[i]; i++)
+			toys.exitval |= which_in_path(toys.optargs[i]);
 	}
-	// if (CFG_TOYBOX_FREE) free(argv);
-
-	return rc;
 }
--- a/toys/yes.c	Thu Nov 29 17:49:50 2007 -0600
+++ b/toys/yes.c	Thu Nov 29 18:14:37 2007 -0600
@@ -5,7 +5,7 @@
 
 #include "toys.h"
 
-int yes_main(void)
+void yes_main(void)
 {
 	for (;;) {
 		int i;
@@ -16,6 +16,4 @@
 		if (!i) xputc('y');
 		xputc('\n');
 	}
-
-	return 0;
 }