which: -84 bytes

This commit is contained in:
Denis Vlasenko 2006-10-05 21:10:53 +00:00
parent a3310527db
commit 01c27fc5ac
2 changed files with 27 additions and 50 deletions

View File

@ -116,12 +116,12 @@ int busybox_main(int argc, char **argv)
/* Obtain the terminal width. */ /* Obtain the terminal width. */
get_terminal_width_height(0, &output_width, NULL); get_terminal_width_height(0, &output_width, NULL);
/* leading tab and room to wrap */ /* leading tab and room to wrap */
output_width -= 20; output_width -= sizeof("start-stop-daemon, ") + 8;
} else output_width = 60; } else output_width = 80 - sizeof("start-stop-daemon, ") - 8;
printf("%s\n" printf("%s\n"
"Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n" "Copyright (C) 1998-2006  Erik Andersen, Rob Landley, and others.\n"
"Licensed under GPLv2.  See source distribution for full notice.\n\n" "Licensed under GPLv2.  See source distribution for full notice.\n\n"
"Usage: busybox [function] [arguments]...\n" "Usage: busybox [function] [arguments]...\n"
" or: [function] [arguments]...\n\n" " or: [function] [arguments]...\n\n"
"\tBusyBox is a multi-call binary that combines many common Unix\n" "\tBusyBox is a multi-call binary that combines many common Unix\n"

View File

@ -26,43 +26,20 @@ int which_main(int argc, char **argv)
{ {
int status; int status;
size_t i, count; 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(); bb_show_usage();
} }
argc--; argc--;
path_list = getenv("PATH"); path_list = getenv("PATH");
if (path_list != NULL) { if (path_list != NULL) {
size_t path_len = strlen(path_list);
char *new_list = NULL;
count = 1; count = 1;
p = path_list;
for (i = 0; i <= path_len; i++) { while ((p = strchr(p, ':')) != NULL) {
char *this_i = &path_list[i]; *p++ = 0;
if (*this_i == ':') { count++;
/* ^::[^:] == \.: */
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;
}
}
} }
} else { } else {
path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; 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) { while (argc-- > 0) {
struct stat stat_b; struct stat stat_b;
char *buf; char *buf;
char *path_n;
int found = 0;
argv++; argv++;
path_n = path_list; buf = argv[0];
buf = *argv;
/* if filename is either absolute or contains slashes, /* If filename is either absolute or contains slashes,
* stat it */ * stat it */
if (strchr(buf, '/') != NULL && is_executable_file(buf, &stat_b)) { if (strchr(buf, '/')) {
found++; if (is_executable_file(buf, &stat_b)) {
puts(buf);
goto next;
}
} else { } 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++) { 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)) { if (is_executable_file(buf, &stat_b)) {
found++; puts(buf);
break; free(buf);
goto next;
} }
free(buf); free(buf);
path_n += (strlen(path_n) + 1); p += strlen(p) + 1;
} }
} }
if (found) { status = EXIT_FAILURE;
puts(buf); next: /* nothing */;
} else {
status = EXIT_FAILURE;
}
} }
bb_fflush_stdout_and_exit(status); bb_fflush_stdout_and_exit(status);
} }