diff options
author | Tito Ragusa <farmatito@tiscali.it> | 2014-05-03 16:34:36 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2014-05-04 17:35:31 +0200 |
commit | a875d59d24dc4e501131a2d5cb6a4afdb48745c1 (patch) | |
tree | e8ab1dc06b14ec59195741a07237a997ca8df189 /debianutils | |
parent | e765b5ac349a8f9305e52b3ab2c3ac78c17bf283 (diff) | |
download | busybox-w32-a875d59d24dc4e501131a2d5cb6a4afdb48745c1.tar.gz busybox-w32-a875d59d24dc4e501131a2d5cb6a4afdb48745c1.tar.bz2 busybox-w32-a875d59d24dc4e501131a2d5cb6a4afdb48745c1.zip |
which: rewrite
function old new delta
which_main 237 212 -25
Signed-off-by: Tito Ragusa <farmatito@tiscali.it>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'debianutils')
-rw-r--r-- | debianutils/which.c | 83 |
1 files changed, 23 insertions, 60 deletions
diff --git a/debianutils/which.c b/debianutils/which.c index 760bcdcad..d50e7a0d3 100644 --- a/debianutils/which.c +++ b/debianutils/which.c | |||
@@ -1,13 +1,9 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | 2 | /* |
3 | * Which implementation for busybox | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | 3 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
6 | * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu> | 4 | * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu> |
7 | * | 5 | * |
8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
9 | * | ||
10 | * Based on which from debianutils | ||
11 | */ | 7 | */ |
12 | 8 | ||
13 | //usage:#define which_trivial_usage | 9 | //usage:#define which_trivial_usage |
@@ -24,76 +20,43 @@ | |||
24 | int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 20 | int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
25 | int which_main(int argc UNUSED_PARAM, char **argv) | 21 | int which_main(int argc UNUSED_PARAM, char **argv) |
26 | { | 22 | { |
27 | IF_DESKTOP(int opt;) | 23 | const char *env_path; |
28 | int status = EXIT_SUCCESS; | 24 | int status = 0; |
29 | char *path; | 25 | |
30 | char *p; | 26 | env_path = getenv("PATH"); |
27 | if (!env_path) | ||
28 | env_path = bb_default_root_path; | ||
31 | 29 | ||
32 | opt_complementary = "-1"; /* at least one argument */ | 30 | opt_complementary = "-1"; /* at least one argument */ |
33 | IF_DESKTOP(opt =) getopt32(argv, "a"); | 31 | getopt32(argv, "a"); |
34 | argv += optind; | 32 | argv += optind; |
35 | 33 | ||
36 | /* This matches what is seen on e.g. ubuntu. | ||
37 | * "which" there is a shell script. */ | ||
38 | path = getenv("PATH"); | ||
39 | if (!path) { | ||
40 | path = (char*)bb_PATH_root_path; | ||
41 | putenv(path); | ||
42 | path += 5; /* skip "PATH=" */ | ||
43 | } | ||
44 | |||
45 | do { | 34 | do { |
46 | #if ENABLE_DESKTOP | 35 | int missing = 1; |
47 | /* Much bloat just to support -a */ | ||
48 | if (strchr(*argv, '/')) { | ||
49 | if (file_is_executable(*argv)) { | ||
50 | puts(*argv); | ||
51 | continue; | ||
52 | } | ||
53 | status = EXIT_FAILURE; | ||
54 | } else { | ||
55 | char *path2 = xstrdup(path); | ||
56 | char *tmp = path2; | ||
57 | 36 | ||
58 | p = find_executable(*argv, &tmp); | 37 | /* If file contains a slash don't use PATH */ |
59 | if (!p) | ||
60 | status = EXIT_FAILURE; | ||
61 | else { | ||
62 | print: | ||
63 | puts(p); | ||
64 | free(p); | ||
65 | if (opt) { | ||
66 | /* -a: show matches in all PATH components */ | ||
67 | if (tmp) { | ||
68 | p = find_executable(*argv, &tmp); | ||
69 | if (p) | ||
70 | goto print; | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | free(path2); | ||
75 | } | ||
76 | #else | ||
77 | /* Just ignoring -a */ | ||
78 | if (strchr(*argv, '/')) { | 38 | if (strchr(*argv, '/')) { |
79 | if (file_is_executable(*argv)) { | 39 | if (file_is_executable(*argv)) { |
40 | missing = 0; | ||
80 | puts(*argv); | 41 | puts(*argv); |
81 | continue; | ||
82 | } | 42 | } |
83 | } else { | 43 | } else { |
84 | char *path2 = xstrdup(path); | 44 | char *path; |
85 | char *tmp = path2; | 45 | char *tmp; |
86 | p = find_executable(*argv, &tmp); | 46 | char *p; |
87 | free(path2); | 47 | |
88 | if (p) { | 48 | path = tmp = xstrdup(env_path); |
49 | while ((p = find_executable(*argv, &tmp)) != NULL) { | ||
50 | missing = 0; | ||
89 | puts(p); | 51 | puts(p); |
90 | free(p); | 52 | free(p); |
91 | continue; | 53 | if (!option_mask32) /* -a not set */ |
54 | break; | ||
92 | } | 55 | } |
56 | free(path); | ||
93 | } | 57 | } |
94 | status = EXIT_FAILURE; | 58 | status |= missing; |
95 | #endif | 59 | } while (*++argv); |
96 | } while (*(++argv) != NULL); | ||
97 | 60 | ||
98 | fflush_stdout_and_exit(status); | 61 | return status; |
99 | } | 62 | } |