diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-10-08 04:03:17 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-10-08 04:03:17 +0200 |
commit | 49d9e06fbab0b02a71deed57610edb0c8f4fb20c (patch) | |
tree | 6b5263bcdb0972eaf17f35dbb8c2ce65d13a790e /shell/hush.c | |
parent | 8c4bccb83e5e2594f16310b7cbe07bf05fc9f13a (diff) | |
download | busybox-w32-49d9e06fbab0b02a71deed57610edb0c8f4fb20c.tar.gz busybox-w32-49d9e06fbab0b02a71deed57610edb0c8f4fb20c.tar.bz2 busybox-w32-49d9e06fbab0b02a71deed57610edb0c8f4fb20c.zip |
libbb: modify find_executable() to not temporarily write to PATH
This allows to simplify "which" applet code
function old new delta
find_executable 93 111 +18
which_main 191 177 -14
builtin_source 316 294 -22
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 18/-36) Total: -18 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to '')
-rw-r--r-- | shell/hush.c | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/shell/hush.c b/shell/hush.c index 4e477d05a..04dda0734 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -8207,41 +8207,34 @@ static int setup_redirects(struct command *prog, struct squirrel **sqp) | |||
8207 | //TODO: shares code with find_executable() in libbb, factor out? | 8207 | //TODO: shares code with find_executable() in libbb, factor out? |
8208 | static char *find_in_PATH(const char *name) | 8208 | static char *find_in_PATH(const char *name) |
8209 | { | 8209 | { |
8210 | char *ret; | 8210 | char *p = (char*) get_local_var_value("PATH"); |
8211 | const char *PATH = get_local_var_value("PATH"); | ||
8212 | 8211 | ||
8213 | if (!PATH) | 8212 | if (!p) |
8214 | return NULL; | 8213 | return NULL; |
8215 | ret = NULL; | ||
8216 | while (1) { | 8214 | while (1) { |
8217 | const char *end = strchrnul(PATH, ':'); | 8215 | const char *end = strchrnul(p, ':'); |
8218 | int sz = end - PATH; /* must be int! */ | 8216 | int sz = end - p; /* must be int! */ |
8219 | 8217 | ||
8220 | free(ret); | ||
8221 | if (sz != 0) { | 8218 | if (sz != 0) { |
8222 | ret = xasprintf("%.*s/%s", sz, PATH, name); | 8219 | p = xasprintf("%.*s/%s", sz, p, name); |
8223 | } else { | 8220 | } else { |
8224 | /* We have xxx::yyyy in $PATH, | 8221 | /* We have xxx::yyy in $PATH, |
8225 | * it means "use current dir" */ | 8222 | * it means "use current dir" */ |
8226 | ret = xstrdup(name); | 8223 | p = xstrdup(name); |
8227 | } | 8224 | } |
8228 | if (access(ret, F_OK) == 0) | 8225 | if (access(p, F_OK) == 0) |
8229 | break; | 8226 | return p; |
8230 | 8227 | free(p); | |
8231 | if (*end == '\0') { | 8228 | if (*end == '\0') |
8232 | free(ret); | ||
8233 | return NULL; | 8229 | return NULL; |
8234 | } | 8230 | p = (char *) end + 1; |
8235 | PATH = end + 1; | ||
8236 | } | 8231 | } |
8237 | |||
8238 | return ret; | ||
8239 | } | 8232 | } |
8240 | 8233 | ||
8241 | #if ENABLE_HUSH_TYPE || ENABLE_HUSH_COMMAND | 8234 | #if ENABLE_HUSH_TYPE || ENABLE_HUSH_COMMAND |
8242 | static char *find_executable_in_PATH(const char *name) | 8235 | static char *find_executable_in_PATH(const char *name) |
8243 | { | 8236 | { |
8244 | char *PATH; | 8237 | const char *PATH; |
8245 | if (strchr(name, '/')) { | 8238 | if (strchr(name, '/')) { |
8246 | /* Name with '/' is tested verbatim, with no PATH traversal: | 8239 | /* Name with '/' is tested verbatim, with no PATH traversal: |
8247 | * "cd /bin; type ./cat" should print "./cat is ./cat", | 8240 | * "cd /bin; type ./cat" should print "./cat is ./cat", |
@@ -8251,7 +8244,7 @@ static char *find_executable_in_PATH(const char *name) | |||
8251 | return xstrdup(name); | 8244 | return xstrdup(name); |
8252 | return NULL; | 8245 | return NULL; |
8253 | } | 8246 | } |
8254 | PATH = (char*)get_local_var_value("PATH"); | 8247 | PATH = get_local_var_value("PATH"); |
8255 | return find_executable(name, &PATH); /* path == NULL is ok */ | 8248 | return find_executable(name, &PATH); /* path == NULL is ok */ |
8256 | } | 8249 | } |
8257 | #endif | 8250 | #endif |