diff options
Diffstat (limited to 'debianutils/which.c')
-rw-r--r-- | debianutils/which.c | 64 |
1 files changed, 52 insertions, 12 deletions
diff --git a/debianutils/which.c b/debianutils/which.c index 5ab67194d..41a864cfa 100644 --- a/debianutils/which.c +++ b/debianutils/which.c | |||
@@ -13,30 +13,69 @@ | |||
13 | #include "libbb.h" | 13 | #include "libbb.h" |
14 | 14 | ||
15 | int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 15 | int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
16 | int which_main(int argc, char **argv) | 16 | int which_main(int argc ATTRIBUTE_UNUSED, char **argv) |
17 | { | 17 | { |
18 | USE_DESKTOP(int opt;) | ||
18 | int status = EXIT_SUCCESS; | 19 | int status = EXIT_SUCCESS; |
20 | char *path; | ||
19 | char *p; | 21 | char *p; |
20 | 22 | ||
21 | if (argc <= 1 || argv[1][0] == '-') { | 23 | opt_complementary = "-1"; /* at least one argument */ |
22 | bb_show_usage(); | 24 | USE_DESKTOP(opt =) getopt32(argv, "a"); |
23 | } | 25 | argv += optind; |
24 | 26 | ||
25 | /* This matches what is seen on e.g. ubuntu | 27 | /* This matches what is seen on e.g. ubuntu. |
26 | * "which" there is a shell script */ | 28 | * "which" there is a shell script. */ |
27 | if (!getenv("PATH")) { | 29 | path = getenv("PATH"); |
28 | putenv((char*)bb_PATH_root_path); | 30 | if (!path) { |
31 | path = (char*)bb_PATH_root_path; | ||
32 | putenv(path); | ||
33 | path += 5; /* skip "PATH=" */ | ||
29 | } | 34 | } |
30 | 35 | ||
31 | while (--argc > 0) { | 36 | do { |
32 | argv++; | 37 | #if ENABLE_DESKTOP |
38 | /* Much bloat just to support -a */ | ||
33 | if (strchr(*argv, '/')) { | 39 | if (strchr(*argv, '/')) { |
34 | if (execable_file(*argv)) { | 40 | if (execable_file(*argv)) { |
35 | puts(*argv); | 41 | puts(*argv); |
36 | continue; | 42 | continue; |
37 | } | 43 | } |
44 | status = EXIT_FAILURE; | ||
38 | } else { | 45 | } else { |
39 | p = find_execable(*argv); | 46 | char *path2 = xstrdup(path); |
47 | char *tmp = path2; | ||
48 | |||
49 | p = find_execable(*argv, &tmp); | ||
50 | if (!p) | ||
51 | status = EXIT_FAILURE; | ||
52 | else { | ||
53 | print: | ||
54 | puts(p); | ||
55 | free(p); | ||
56 | if (opt) { | ||
57 | /* -a: show matches in all PATH components */ | ||
58 | if (tmp) { | ||
59 | p = find_execable(*argv, &tmp); | ||
60 | if (p) | ||
61 | goto print; | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | free(path2); | ||
66 | } | ||
67 | #else | ||
68 | /* Just ignoring -a */ | ||
69 | if (strchr(*argv, '/')) { | ||
70 | if (execable_file(*argv)) { | ||
71 | puts(*argv); | ||
72 | continue; | ||
73 | } | ||
74 | } else { | ||
75 | char *path2 = xstrdup(path); | ||
76 | char *tmp = path2; | ||
77 | p = find_execable(*argv, &tmp); | ||
78 | free(path2); | ||
40 | if (p) { | 79 | if (p) { |
41 | puts(p); | 80 | puts(p); |
42 | free(p); | 81 | free(p); |
@@ -44,7 +83,8 @@ int which_main(int argc, char **argv) | |||
44 | } | 83 | } |
45 | } | 84 | } |
46 | status = EXIT_FAILURE; | 85 | status = EXIT_FAILURE; |
47 | } | 86 | #endif |
87 | } while (*(++argv) != NULL); | ||
48 | 88 | ||
49 | fflush_stdout_and_exit(status); | 89 | fflush_stdout_and_exit(status); |
50 | } | 90 | } |