Some corrections, some additions, some embellishments.
[oweals/busybox.git] / busybox.c
index d356e4caea4924bca476ae9a65194a0d1dacb94c..0439fb566cb5892d8abed2a1ff784d6750ddecac 100644 (file)
--- a/busybox.c
+++ b/busybox.c
@@ -3,6 +3,10 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+
+#undef APPLET
+#undef APPLET_NOUSAGE
+#undef PROTOTYPES
 #include "applets.h"
 
 #define bb_need_full_version
@@ -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);
@@ -74,21 +78,13 @@ static void install_links(const char *busybox, int use_symbolic_links)
                rc = Link(busybox, command);
 
                if (rc) {
-                       errorMsg("%s: %s\n", command, strerror(errno));
+                       perror_msg("%s", command);
                }
        }
 }
 
 #endif /* BB_FEATURE_INSTALLER */
 
-static int applet_name_compare(const void *x, const void *y)
-{
-       const struct BB_applet *applet1 = x;
-       const struct BB_applet *applet2 = y;
-
-       return strcmp(applet1->name, applet2->name);
-}
-
 int main(int argc, char **argv)
 {
        struct BB_applet search_applet, *applet;
@@ -130,8 +126,6 @@ int main(int argc, char **argv)
                        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 */
@@ -156,11 +150,11 @@ int main(int argc, char **argv)
 
 int busybox_main(int argc, char **argv)
 {
-       int col = 0;
+       int col = 0, len, i;
 
        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;
 
@@ -185,8 +179,21 @@ int busybox_main(int argc, char **argv)
                fprintf(stderr, "\n\n");
                exit(-1);
        }
-       /* 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));
 }