diff options
Diffstat (limited to 'libbb/executable.c')
-rw-r--r-- | libbb/executable.c | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/libbb/executable.c b/libbb/executable.c index 325dd01..29d2a2c 100644 --- a/libbb/executable.c +++ b/libbb/executable.c @@ -25,7 +25,8 @@ int FAST_FUNC file_is_executable(const char *name) * you may call find_executable again with this PATHp to continue * (if it's not NULL). * return NULL otherwise; (PATHp is undefined) - * in all cases (*PATHp) contents will be trashed (s/:/NUL/). + * in all cases (*PATHp) contents are temporarily modified + * but are restored on return (s/:/NUL/ and back). */ char* FAST_FUNC find_executable(const char *filename, char **PATHp) { @@ -41,14 +42,17 @@ char* FAST_FUNC find_executable(const char *filename, char **PATHp) p = *PATHp; while (p) { + int ex; + n = strchr(p, ':'); - if (n) - *n++ = '\0'; + if (n) *n = '\0'; p = concat_path_file( p[0] ? p : ".", /* handle "::" case */ filename ); - if (file_is_executable(p)) { + ex = file_is_executable(p); + if (n) *n++ = ':'; + if (ex) { *PATHp = n; return p; } @@ -64,10 +68,8 @@ char* FAST_FUNC find_executable(const char *filename, char **PATHp) */ int FAST_FUNC executable_exists(const char *filename) { - char *path = xstrdup(getenv("PATH")); - char *tmp = path; - char *ret = find_executable(filename, &tmp); - free(path); + char *path = getenv("PATH"); + char *ret = find_executable(filename, &path); free(ret); return ret != NULL; } |