1 /* vi: set sw=4 ts=4: */
3 * Which implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 * Based on which from debianutils
15 int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
16 int which_main(int argc UNUSED_PARAM, char **argv)
19 int status = EXIT_SUCCESS;
23 opt_complementary = "-1"; /* at least one argument */
24 IF_DESKTOP(opt =) getopt32(argv, "a");
27 /* This matches what is seen on e.g. ubuntu.
28 * "which" there is a shell script. */
29 path = getenv("PATH");
31 path = (char*)bb_PATH_root_path;
33 path += 5; /* skip "PATH=" */
38 /* Much bloat just to support -a */
39 if (strchr(*argv, '/')) {
40 if (execable_file(*argv)) {
44 status = EXIT_FAILURE;
46 char *path2 = xstrdup(path);
49 p = find_execable(*argv, &tmp);
51 status = EXIT_FAILURE;
57 /* -a: show matches in all PATH components */
59 p = find_execable(*argv, &tmp);
68 /* Just ignoring -a */
69 if (strchr(*argv, '/')) {
70 if (execable_file(*argv)) {
75 char *path2 = xstrdup(path);
77 p = find_execable(*argv, &tmp);
85 status = EXIT_FAILURE;
87 } while (*(++argv) != NULL);
89 fflush_stdout_and_exit(status);