Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / libbb / execable.c
blob178a00a5f1f69a762c7f20e5b2dd852bcf2ae2b0
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Utility routines.
5 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
10 #include "libbb.h"
12 /* check if path points to an executable file;
13 * return 1 if found;
14 * return 0 otherwise;
16 int FAST_FUNC execable_file(const char *name)
18 struct stat s;
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
25 * (or NULL),
26 * you may call find_execable again with this PATHp to continue
27 * (if it's not NULL).
28 * return NULL otherwise; (PATHp is undefined)
29 * in all cases (*PATHp) contents will be trashed (s/:/NUL/).
31 char* FAST_FUNC find_execable(const char *filename, char **PATHp)
33 char *p, *n;
35 p = *PATHp;
36 while (p) {
37 n = strchr(p, ':');
38 if (n)
39 *n++ = '\0';
40 if (*p != '\0') { /* it's not a PATH="foo::bar" situation */
41 p = concat_path_file(p, filename);
42 if (execable_file(p)) {
43 *PATHp = n;
44 return p;
46 free(p);
48 p = n;
49 } /* on loop exit p == NULL */
50 return p;
53 /* search $PATH for an executable file;
54 * return 1 if found;
55 * return 0 otherwise;
57 int FAST_FUNC exists_execable(const char *filename)
59 char *path = xstrdup(getenv("PATH"));
60 char *tmp = path;
61 char *ret = find_execable(filename, &tmp);
62 free(path);
63 if (ret) {
64 free(ret);
65 return 1;
67 return 0;
70 #if ENABLE_FEATURE_PREFER_APPLETS
71 /* just like the real execvp, but try to launch an applet named 'file' first */
72 int FAST_FUNC BB_EXECVP(const char *file, char *const argv[])
74 if (find_applet_by_name(file) >= 0)
75 execvp(bb_busybox_exec_path, argv);
76 return execvp(file, argv);
78 #endif
80 int FAST_FUNC BB_EXECVP_or_die(char **argv)
82 BB_EXECVP(argv[0], argv);
83 /* SUSv3-mandated exit codes */
84 xfunc_error_retval = (errno == ENOENT) ? 127 : 126;
85 bb_perror_msg_and_die("can't execute '%s'", argv[0]);