*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
-
#include "libbb.h"
/* check if path points to an executable file;
* 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)
{
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;
}
*/
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;
}
if (argv[0]) {
BB_EXECVP_or_die(argv);
}
- run_shell(getenv("SHELL"), /*login:*/ 1, NULL, NULL);
+ run_shell(getenv("SHELL"), /*login:*/ 1, NULL);
}