which: -84 bytes
authorDenis Vlasenko <vda.linux@googlemail.com>
Thu, 5 Oct 2006 21:10:53 +0000 (21:10 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Thu, 5 Oct 2006 21:10:53 +0000 (21:10 -0000)
applets/busybox.c
debianutils/which.c

index 625a492f2c3a0af86c14b44771abfae0a48eadaa..9f2eddebd8cbe3e752748d39ed64bdce2ea7bdc7 100644 (file)
@@ -116,12 +116,12 @@ int busybox_main(int argc, char **argv)
                                /* Obtain the terminal width.  */
                                get_terminal_width_height(0, &output_width, NULL);
                                /* leading tab and room to wrap */
-                               output_width -= 20;
-                       } else output_width = 60;
+                               output_width -= sizeof("start-stop-daemon, ") + 8;
+                       } else output_width = 80 - sizeof("start-stop-daemon, ") - 8;
 
                        printf("%s\n"
-                                  "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
-                                  "Licensed under GPLv2.  See source distribution for full notice.\n\n"
+                              "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
+                              "Licensed under GPLv2.  See source distribution for full notice.\n\n"
                               "Usage: busybox [function] [arguments]...\n"
                               "   or: [function] [arguments]...\n\n"
                               "\tBusyBox is a multi-call binary that combines many common Unix\n"
index db28f78032976443a3508db97fd219e1321c8aa4..583d9461368fe589f4b34ecab5560e170e0960c5 100644 (file)
@@ -26,43 +26,20 @@ int which_main(int argc, char **argv)
 {
        int status;
        size_t i, count;
-       char *path_list;
+       char *path_list, *p;
 
-       if (argc <= 1 || **(argv + 1) == '-') {
+       if (argc <= 1 || argv[1][0] == '-') {
                bb_show_usage();
        }
        argc--;
 
        path_list = getenv("PATH");
        if (path_list != NULL) {
-               size_t path_len = strlen(path_list);
-               char *new_list = NULL;
                count = 1;
-
-               for (i = 0; i <= path_len; i++) {
-                       char *this_i = &path_list[i];
-                       if (*this_i == ':') {
-                               /* ^::[^:] == \.: */
-                               if (!i && (*(this_i + 1) == ':')) {
-                                       *this_i = '.';
-                                       continue;
-                               }
-                               *this_i = 0;
-                               count++;
-                               /* ^:[^:] == \.0 and [^:]::[^:] == 0\.0 and [^:]:$ == 0\.0 */
-                               if (!i || (*(this_i + 1) == ':') || (i == path_len-1)) {
-                                       new_list = xrealloc(new_list, path_len += 1);
-                                       if (i) {
-                                               memmove(&new_list[i+2], &path_list[i+1], path_len-i);
-                                               new_list[i+1] = '.';
-                                               memmove(new_list, path_list, i);
-                                       } else {
-                                               memmove(&new_list[i+1], &path_list[i], path_len-i);
-                                               new_list[i] = '.';
-                                       }
-                                       path_list = new_list;
-                               }
-                       }
+               p = path_list;
+               while ((p = strchr(p, ':')) != NULL) {
+                       *p++ = 0;
+                       count++;
                }
        } else {
                path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin";
@@ -73,34 +50,34 @@ int which_main(int argc, char **argv)
        while (argc-- > 0) {
                struct stat stat_b;
                char *buf;
-               char *path_n;
-               int found = 0;
 
                argv++;
-               path_n = path_list;
-               buf = *argv;
+               buf = argv[0];
 
-               /* if filename is either absolute or contains slashes,
+               /* If filename is either absolute or contains slashes,
                 * stat it */
-               if (strchr(buf, '/') != NULL && is_executable_file(buf, &stat_b)) {
-                       found++;
+               if (strchr(buf, '/')) {
+                       if (is_executable_file(buf, &stat_b)) {
+                               puts(buf);
+                               goto next;
+                       }
                } else {
-                       /* Couldn't access file and file doesn't contain slashes */
+                       /* File doesn't contain slashes */
+                       p = path_list;
                        for (i = 0; i < count; i++) {
-                               buf = concat_path_file(path_n, *argv);
+                               /* Empty component in PATH is treated as . */
+                               buf = concat_path_file(p[0] ? p : ".", argv[0]);
                                if (is_executable_file(buf, &stat_b)) {
-                                       found++;
-                                       break;
+                                       puts(buf);
+                                       free(buf);
+                                       goto next;
                                }
                                free(buf);
-                               path_n += (strlen(path_n) + 1);
+                               p += strlen(p) + 1;
                        }
                }
-               if (found) {
-                       puts(buf);
-               } else {
-                       status = EXIT_FAILURE;
-               }
+               status = EXIT_FAILURE;
+ next:         /* nothing */;
        }
        bb_fflush_stdout_and_exit(status);
 }