aboutsummaryrefslogtreecommitdiff
path: root/debianutils/which.c
diff options
context:
space:
mode:
Diffstat (limited to 'debianutils/which.c')
-rw-r--r--debianutils/which.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/debianutils/which.c b/debianutils/which.c
index a7d55a215..5d564e01d 100644
--- a/debianutils/which.c
+++ b/debianutils/which.c
@@ -12,7 +12,11 @@
12//config: which is used to find programs in your PATH and 12//config: which is used to find programs in your PATH and
13//config: print out their pathnames. 13//config: print out their pathnames.
14 14
15//applet:IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which)) 15// NOTE: For WIN32 this applet is NOEXEC as file_is_win32_exe() and
16// find_executable() both allocate memory.
17
18//applet:IF_PLATFORM_MINGW32(IF_WHICH(APPLET_NOEXEC(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which)))
19//applet:IF_PLATFORM_POSIX(IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which)))
16 20
17//kbuild:lib-$(CONFIG_WHICH) += which.o 21//kbuild:lib-$(CONFIG_WHICH) += which.o
18 22
@@ -33,6 +37,10 @@ int which_main(int argc UNUSED_PARAM, char **argv)
33{ 37{
34 const char *env_path; 38 const char *env_path;
35 int status = 0; 39 int status = 0;
40#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE
41 /* 'Which' in argv[0] indicates we were run from a standalone shell */
42 int sh_standalone = argv[0][0] == 'W';
43#endif
36 44
37 env_path = getenv("PATH"); 45 env_path = getenv("PATH");
38 if (!env_path) 46 if (!env_path)
@@ -44,21 +52,60 @@ int which_main(int argc UNUSED_PARAM, char **argv)
44 do { 52 do {
45 int missing = 1; 53 int missing = 1;
46 54
55#if ENABLE_PLATFORM_MINGW32 && ENABLE_FEATURE_SH_STANDALONE
56 if (sh_standalone && find_applet_by_name(*argv) >= 0) {
57 missing = 0;
58 puts(applet_to_exe(*argv));
59 if (!option_mask32) /* -a not set */
60 break;
61 }
62#endif
63
64#if !ENABLE_PLATFORM_MINGW32
47 /* If file contains a slash don't use PATH */ 65 /* If file contains a slash don't use PATH */
48 if (strchr(*argv, '/')) { 66 if (strchr(*argv, '/')) {
49 if (file_is_executable(*argv)) { 67 if (file_is_executable(*argv)) {
50 missing = 0; 68 missing = 0;
51 puts(*argv); 69 puts(*argv);
52 } 70 }
71#else
72 if (has_path(*argv)) {
73 char *path = file_is_win32_exe(*argv);
74
75 if (path) {
76 missing = 0;
77 puts(bs_to_slash(path));
78 }
79 else if (unix_path(*argv)) {
80 const char *name = bb_basename(*argv);
81# if ENABLE_FEATURE_SH_STANDALONE
82 if (sh_standalone && find_applet_by_name(name) >= 0) {
83 missing = 0;
84 puts(name);
85 } else
86# endif
87 {
88 argv[0] = (char *)name;
89 goto try_PATH;
90 }
91 }
92#endif
53 } else { 93 } else {
54 const char *path; 94 const char *path;
55 char *p; 95 char *p;
56 96
97#if ENABLE_PLATFORM_MINGW32
98 try_PATH:
99#endif
57 path = env_path; 100 path = env_path;
58 /* NOFORK NB: xmalloc inside find_executable(), must have no allocs above! */ 101 /* NOFORK NB: xmalloc inside find_executable(), must have no allocs above! */
59 while ((p = find_executable(*argv, &path)) != NULL) { 102 while ((p = find_executable(*argv, &path)) != NULL) {
60 missing = 0; 103 missing = 0;
104#if ENABLE_PLATFORM_MINGW32
105 puts(bs_to_slash(p));
106#else
61 puts(p); 107 puts(p);
108#endif
62 free(p); 109 free(p);
63 if (!option_mask32) /* -a not set */ 110 if (!option_mask32) /* -a not set */
64 break; 111 break;