1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 /* check if path points to an executable file;
16 int FAST_FUNC
file_is_executable(const char *name
)
19 return (!access(name
, X_OK
) && !stat(name
, &s
) && S_ISREG(s
.st_mode
));
22 /* search (*PATHp) for an executable file;
23 * return allocated string containing full path if found;
24 * PATHp points to the component after the one where it was found
26 * you may call find_executable again with this PATHp to continue
28 * return NULL otherwise; (PATHp is undefined)
29 * in all cases (*PATHp) contents will be trashed (s/:/NUL/).
31 char* FAST_FUNC
find_executable(const char *filename
, char **PATHp
)
33 /* About empty components in $PATH:
34 * http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
35 * 8.3 Other Environment Variables - PATH
36 * A zero-length prefix is a legacy feature that indicates the current
37 * working directory. It appears as two adjacent colons ( "::" ), as an
38 * initial colon preceding the rest of the list, or as a trailing colon
39 * following the rest of the list.
49 p
[0] ? p
: ".", /* handle "::" case */
52 if (file_is_executable(p
)) {
58 } /* on loop exit p == NULL */
62 /* search $PATH for an executable file;
66 int FAST_FUNC
executable_exists(const char *filename
)
68 char *path
= xstrdup(getenv("PATH"));
70 char *ret
= find_executable(filename
, &tmp
);
76 #if ENABLE_FEATURE_PREFER_APPLETS
77 /* just like the real execvp, but try to launch an applet named 'file' first */
78 int FAST_FUNC
BB_EXECVP(const char *file
, char *const argv
[])
80 if (find_applet_by_name(file
) >= 0)
81 execvp(bb_busybox_exec_path
, argv
);
82 return execvp(file
, argv
);
86 int FAST_FUNC
BB_EXECVP_or_die(char **argv
)
88 BB_EXECVP(argv
[0], argv
);
89 /* SUSv3-mandated exit codes */
90 xfunc_error_retval
= (errno
== ENOENT
) ? 127 : 126;
91 bb_perror_msg_and_die("can't execute '%s'", argv
[0]);