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