X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=which.c;h=1d75244654571c73923d20a97c0aa4473cab74fc;hb=4949faf4b2090ca23c2aeb34535fdbe57754913a;hp=8d4422a78d1d15ef18276b33b3691d8b466fcc75;hpb=b610615be9aedfac07d1e01f12575707fa3a227c;p=oweals%2Fbusybox.git diff --git a/which.c b/which.c index 8d4422a78..1d7524465 100644 --- a/which.c +++ b/which.c @@ -2,7 +2,7 @@ /* * Which implementation for busybox * - * Copyright (C) 2000 by Lineo, inc. + * Copyright (C) 1999,2000,2001 by Lineo, inc. * Written by Erik Andersen , * * This program is free software; you can redistribute it and/or modify @@ -21,74 +21,55 @@ * */ -#include "internal.h" +/* getopt not needed */ +#include #include -#include -#include - +#include +#include "busybox.h" extern int which_main(int argc, char **argv) { - char *path_list, *test, *tmp, *path_parsed; - char buf[PATH_MAX]; + char *path_list, *path_n; struct stat filestat; - int count = 0; + int i, count=1, found, status = EXIT_SUCCESS; - if (argc <= 1 || **(argv + 1) == '-') { - usage("which [COMMAND ...]\n" -#ifndef BB_FEATURE_TRIVIAL_HELP - "\nLocates a COMMAND.\n" -#endif - ); - } + if (argc <= 1 || **(argv + 1) == '-') + show_usage(); argc--; path_list = getenv("PATH"); if (!path_list) path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; - path_parsed = malloc (strlen(path_list) + 1); - strcpy (path_parsed, path_list); - /* Replace colons with zeros in path_parsed and count them */ - count = 1; - test = path_parsed; - while (1) { - tmp = strchr(test, ':'); - if (tmp == NULL) - break; - *tmp = 0; - test = tmp + 1; - count++; - } - + for(i=strlen(path_list); i > 0; i--) + if (path_list[i]==':') { + path_list[i]=0; + count++; + } while(argc-- > 0) { - int i; - int found = FALSE; - test = path_parsed; + path_n = path_list; argv++; + found = 0; for (i = 0; i < count; i++) { - strcpy (buf, test); + char buf[strlen(path_n)+1+strlen(*argv)]; + strcpy (buf, path_n); strcat (buf, "/"); strcat (buf, *argv); if (stat (buf, &filestat) == 0 && filestat.st_mode & S_IXUSR) { - found = TRUE; + printf ("%s\n", buf); + found = 1; break; } - test += (strlen(test) + 1); - } - if (found == TRUE) - printf ("%s\n", buf); - else - { - printf ("which: no %s in (%s)\n", *argv, path_list); - exit (FALSE); + path_n += (strlen(path_n) + 1); } + if (!found) + status = EXIT_FAILURE; } - return(TRUE); + return status; } /*