aboutsummaryrefslogtreecommitdiff
path: root/debianutils/which.c
diff options
context:
space:
mode:
Diffstat (limited to 'debianutils/which.c')
-rw-r--r--debianutils/which.c99
1 files changed, 28 insertions, 71 deletions
diff --git a/debianutils/which.c b/debianutils/which.c
index bcca95331..fe6cf2f78 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,98 +20,59 @@
24int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 20int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
25int which_main(int argc UNUSED_PARAM, char **argv) 21int 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 {
35 int missing = 1;
36 char *p;
37
46#if ENABLE_FEATURE_PREFER_APPLETS 38#if ENABLE_FEATURE_PREFER_APPLETS
47 if ( find_applet_by_name(*argv) >= 0 ) { 39 if ( find_applet_by_name(*argv) >= 0 ) {
40 missing = 0;
48 puts(*argv); 41 puts(*argv);
49 IF_DESKTOP(if ( !opt )) 42 if (!option_mask32) /* -a not set */
50 continue; 43 break;
51 } 44 }
52#endif 45#endif
53 46
54#if ENABLE_DESKTOP 47 /* If file contains a slash don't use PATH */
55/* Much bloat just to support -a */
56 if (strchr(*argv, '/') || (ENABLE_PLATFORM_MINGW32 && strchr(*argv, '\\'))) { 48 if (strchr(*argv, '/') || (ENABLE_PLATFORM_MINGW32 && strchr(*argv, '\\'))) {
57 if (execable_file(*argv)) { 49 if (file_is_executable(*argv)) {
50 missing = 0;
58 puts(*argv); 51 puts(*argv);
59 continue;
60 } 52 }
61#if ENABLE_PLATFORM_MINGW32 53#if ENABLE_PLATFORM_MINGW32
62 else if ((p=win32_execable_file(*argv)) != NULL) { 54 else if ((p=win32_execable_file(*argv)) != NULL) {
55 missing = 0;
63 puts(p); 56 puts(p);
64 free(p); 57 free(p);
65 continue;
66 } 58 }
67#endif 59#endif
68 status = EXIT_FAILURE;
69 } else { 60 } else {
70 char *path2 = xstrdup(path); 61 char *path;
71 char *tmp = path2; 62 char *tmp;
72 63
73 p = find_execable(*argv, &tmp); 64 path = tmp = xstrdup(env_path);
74 if (!p) 65 while ((p = find_executable(*argv, &tmp)) != NULL) {
75 status = EXIT_FAILURE; 66 missing = 0;
76 else {
77 print:
78 puts(p);
79 free(p);
80 if (opt) {
81 /* -a: show matches in all PATH components */
82 if (tmp) {
83 p = find_execable(*argv, &tmp);
84 if (p)
85 goto print;
86 }
87 }
88 }
89 free(path2);
90 }
91#else
92/* Just ignoring -a */
93 if (strchr(*argv, '/') || (ENABLE_PLATFORM_MINGW32 && strchr(*argv, '\\'))) {
94 if (execable_file(*argv)) {
95 puts(*argv);
96 continue;
97 }
98#if ENABLE_PLATFORM_MINGW32
99 else if ((p=win32_execable_file(*argv)) != NULL) {
100 puts(p);
101 free(p);
102 continue;
103 }
104#endif
105 } else {
106 char *path2 = xstrdup(path);
107 char *tmp = path2;
108 p = find_execable(*argv, &tmp);
109 free(path2);
110 if (p) {
111 puts(p); 67 puts(p);
112 free(p); 68 free(p);
113 continue; 69 if (!option_mask32) /* -a not set */
70 break;
114 } 71 }
72 free(path);
115 } 73 }
116 status = EXIT_FAILURE; 74 status |= missing;
117#endif 75 } while (*++argv);
118 } while (*(++argv) != NULL);
119 76
120 fflush_stdout_and_exit(status); 77 return status;
121} 78}