325dd0107b89745fce644928ff91f74084f347a7
[oweals/busybox.git] / libbb / executable.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 #include "libbb.h"
10
11 /* check if path points to an executable file;
12  * return 1 if found;
13  * return 0 otherwise;
14  */
15 int FAST_FUNC file_is_executable(const char *name)
16 {
17         struct stat s;
18         return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode));
19 }
20
21 /* search (*PATHp) for an executable file;
22  * return allocated string containing full path if found;
23  *  PATHp points to the component after the one where it was found
24  *  (or NULL),
25  *  you may call find_executable again with this PATHp to continue
26  *  (if it's not NULL).
27  * return NULL otherwise; (PATHp is undefined)
28  * in all cases (*PATHp) contents will be trashed (s/:/NUL/).
29  */
30 char* FAST_FUNC find_executable(const char *filename, char **PATHp)
31 {
32         /* About empty components in $PATH:
33          * http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
34          * 8.3 Other Environment Variables - PATH
35          * A zero-length prefix is a legacy feature that indicates the current
36          * working directory. It appears as two adjacent colons ( "::" ), as an
37          * initial colon preceding the rest of the list, or as a trailing colon
38          * following the rest of the list.
39          */
40         char *p, *n;
41
42         p = *PATHp;
43         while (p) {
44                 n = strchr(p, ':');
45                 if (n)
46                         *n++ = '\0';
47                 p = concat_path_file(
48                         p[0] ? p : ".", /* handle "::" case */
49                         filename
50                 );
51                 if (file_is_executable(p)) {
52                         *PATHp = n;
53                         return p;
54                 }
55                 free(p);
56                 p = n;
57         } /* on loop exit p == NULL */
58         return p;
59 }
60
61 /* search $PATH for an executable file;
62  * return 1 if found;
63  * return 0 otherwise;
64  */
65 int FAST_FUNC executable_exists(const char *filename)
66 {
67         char *path = xstrdup(getenv("PATH"));
68         char *tmp = path;
69         char *ret = find_executable(filename, &tmp);
70         free(path);
71         free(ret);
72         return ret != NULL;
73 }
74
75 #if ENABLE_FEATURE_PREFER_APPLETS
76 /* just like the real execvp, but try to launch an applet named 'file' first */
77 int FAST_FUNC BB_EXECVP(const char *file, char *const argv[])
78 {
79         if (find_applet_by_name(file) >= 0)
80                 execvp(bb_busybox_exec_path, argv);
81         return execvp(file, argv);
82 }
83 #endif
84
85 void FAST_FUNC BB_EXECVP_or_die(char **argv)
86 {
87         BB_EXECVP(argv[0], argv);
88         /* SUSv3-mandated exit codes */
89         xfunc_error_retval = (errno == ENOENT) ? 127 : 126;
90         bb_perror_msg_and_die("can't execute '%s'", argv[0]);
91 }
92
93 /* Typical idiom for applets which exec *optional* PROG [ARGS] */
94 void FAST_FUNC exec_prog_or_SHELL(char **argv)
95 {
96         if (argv[0]) {
97                 BB_EXECVP_or_die(argv);
98         }
99         run_shell(getenv("SHELL"), /*login:*/ 1, NULL);
100 }