Thought of another janitorial item for the list.
[oweals/busybox.git] / busybox.c
index 696dd4262559bf46a30f6c1b48e25e081a34daea..0250e4c108ccee77d65c0b28c4e467e617d5724b 100644 (file)
--- a/busybox.c
+++ b/busybox.c
@@ -1,8 +1,14 @@
 /* vi: set sw=4 ts=4: */
-#include "busybox.h"
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 #include <errno.h>
+#include <stdlib.h>
+#include "busybox.h"
+
+#undef APPLET
+#undef APPLET_NOUSAGE
+#undef PROTOTYPES
 #include "applets.h"
 
 #define bb_need_full_version
@@ -10,8 +16,6 @@
 #include "messages.c"
 
 static int been_there_done_that = 0;
-
-
 const char *applet_name;
 
 #ifdef BB_FEATURE_INSTALLER
@@ -50,7 +54,7 @@ static char *busybox_fullpath()
        if (len != -1) {
                path[len] = 0;
        } else {
-               errorMsg("%s: %s\n", proc, strerror(errno));
+               perror_msg("%s", proc);
                return NULL;
        }
        return strdup(path);
@@ -65,28 +69,47 @@ static void install_links(const char *busybox, int use_symbolic_links)
        int i;
        int rc;
 
-       if (use_symbolic_links) Link = symlink;
+       if (use_symbolic_links) 
+               Link = symlink;
 
        for (i = 0; applets[i].name != NULL; i++) {
                sprintf ( command, "%s/%s", 
-                               install_dir[applets[i].location], 
-                               applets[i].name);
+                               install_dir[applets[i].location], applets[i].name);
                rc = Link(busybox, command);
 
                if (rc) {
-                       errorMsg("%s: %s\n", command, strerror(errno));
+                       perror_msg("%s", command);
                }
        }
 }
 
 #endif /* BB_FEATURE_INSTALLER */
 
-
 int main(int argc, char **argv)
 {
-       const char                              *s;
-       int u, l; /* Lower and upper bounds for the binary search. */
-       applet_name = "busybox";
+       const char *s;
+
+       for (s = applet_name = argv[0]; *s != '\0';) {
+               if (*s++ == '/')
+                       applet_name = s;
+       }
+
+#ifdef BB_SH
+       /* Add in a special case hack -- whenever **argv == '-'
+        * (i.e. '-su' or '-sh') always invoke the shell */
+       if (**argv == '-' && *(*argv+1)!= '-') {
+               exit(((*(shell_main)) (argc, argv)));
+       }
+#endif
+
+       run_applet_by_name(applet_name, argc, argv);
+       error_msg_and_die("applet not found");
+}
+
+
+int busybox_main(int argc, char **argv)
+{
+       int col = 0, len, i;
 
 #ifdef BB_FEATURE_INSTALLER    
        /* 
@@ -118,59 +141,9 @@ int main(int argc, char **argv)
        }
 #endif /* BB_FEATURE_INSTALLER */
 
-       for (s = applet_name = argv[0]; *s != '\0';) {
-               if (*s++ == '/')
-                       applet_name = s;
-       }
-
-       *argv = (char*)applet_name;
-
-#ifdef BB_SH
-       /* Add in a special case hack -- whenever **argv == '-'
-        * (i.e. '-su' or '-sh') always invoke the shell */
-       if (**argv == '-' && *(*argv+1)!= '-') {
-               exit(((*(shell_main)) (argc, argv)));
-       }
-#endif
-
-       /* Do a binary search to find the applet entry given the name. */
-
-       u = NUM_APPLETS - 1;
-       l = 0;
-
-       for (;;) {
-               int i = l + (u - l) / 2;
-               int j = strcmp(applet_name, applets[i].name);
-
-               if (j == 0) {
-                       if (applets[i].usage && argv[1] && strcmp(argv[1], "--help") == 0)
-                               usage(applets[i].usage);
-                       exit(((*(applets[i].main)) (argc, argv)));
-               }
-               
-               if (u == l)
-                       break;
-                       
-               if (j < 0)
-                       u = i - 1;
-               else
-                       l = i + 1;
-
-               if (u < l)
-                       break;
-       }
-
-       return(busybox_main(argc, argv));
-}
-
-
-int busybox_main(int argc, char **argv)
-{
-       int col = 0;
-
        argc--;
-       argv++;
 
+       /* If we've already been here once, exit now */
        if (been_there_done_that == 1 || argc < 1) {
                const struct BB_applet *a = applets;
 
@@ -193,10 +166,23 @@ int busybox_main(int argc, char **argv)
                        }
                }
                fprintf(stderr, "\n\n");
-               exit(-1);
+               exit(0);
        }
-       /* If we've already been here once, exit now */
+
+       /* Flag that we've been here already */
        been_there_done_that = 1;
+       
+       /* Move the command line down a notch */
+       len = argv[argc] + strlen(argv[argc]) - argv[1];
+       memmove(argv[0], argv[1], len);
+       memset(argv[0] + len, 0, argv[1] - argv[0]);
+
+       /* Fix up the argv pointers */
+       len = argv[1] - argv[0];
+       memmove(argv, argv + 1, sizeof(char *) * (argc + 1));
+       for (i = 0; i < argc; i++)
+               argv[i] -= len;
+
        return (main(argc, argv));
 }