blob: 05e70312f00b82687447fb784b680f3e5013f65d [file] [log] [blame]
Denis Vlasenko5cb46482006-10-12 06:12:11 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko5cb46482006-10-12 06:12:11 +00008 */
9
10#include "libbb.h"
11
12/* check if path points to an executable file;
13 * return 1 if found;
14 * return 0 otherwise;
15 */
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020016int FAST_FUNC file_is_executable(const char *name)
Denis Vlasenko5cb46482006-10-12 06:12:11 +000017{
18 struct stat s;
19 return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode));
20}
21
Denis Vlasenkof592aa32008-06-05 13:33:59 +000022/* search (*PATHp) for an executable file;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000023 * return allocated string containing full path if found;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000024 * PATHp points to the component after the one where it was found
25 * (or NULL),
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020026 * you may call find_executable again with this PATHp to continue
Denis Vlasenkof592aa32008-06-05 13:33:59 +000027 * (if it's not NULL).
28 * return NULL otherwise; (PATHp is undefined)
29 * in all cases (*PATHp) contents will be trashed (s/:/NUL/).
Denis Vlasenko5cb46482006-10-12 06:12:11 +000030 */
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020031char* FAST_FUNC find_executable(const char *filename, char **PATHp)
Denis Vlasenko5cb46482006-10-12 06:12:11 +000032{
Denys Vlasenko15a357e2014-05-02 17:08:29 +020033 /* 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.
40 */
Denis Vlasenkof592aa32008-06-05 13:33:59 +000041 char *p, *n;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000042
Denis Vlasenkof592aa32008-06-05 13:33:59 +000043 p = *PATHp;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000044 while (p) {
45 n = strchr(p, ':');
46 if (n)
47 *n++ = '\0';
Denys Vlasenko15a357e2014-05-02 17:08:29 +020048 p = concat_path_file(
49 p[0] ? p : ".", /* handle "::" case */
50 filename
51 );
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020052 if (file_is_executable(p)) {
Denys Vlasenko15a357e2014-05-02 17:08:29 +020053 *PATHp = n;
54 return p;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000055 }
Denys Vlasenko15a357e2014-05-02 17:08:29 +020056 free(p);
Denis Vlasenko5cb46482006-10-12 06:12:11 +000057 p = n;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000058 } /* on loop exit p == NULL */
59 return p;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000060}
61
62/* search $PATH for an executable file;
63 * return 1 if found;
64 * return 0 otherwise;
65 */
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020066int FAST_FUNC executable_exists(const char *filename)
Denis Vlasenko5cb46482006-10-12 06:12:11 +000067{
Denis Vlasenkof592aa32008-06-05 13:33:59 +000068 char *path = xstrdup(getenv("PATH"));
69 char *tmp = path;
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020070 char *ret = find_executable(filename, &tmp);
Denis Vlasenkof592aa32008-06-05 13:33:59 +000071 free(path);
Denys Vlasenko15a357e2014-05-02 17:08:29 +020072 free(ret);
73 return ret != NULL;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000074}
Denis Vlasenko1d76f432007-02-06 01:20:12 +000075
Denis Vlasenko80d14be2007-04-10 23:03:30 +000076#if ENABLE_FEATURE_PREFER_APPLETS
Denys Vlasenkoc71b4692011-02-02 03:28:56 +010077/* just like the real execvp, but try to launch an applet named 'file' first */
78int FAST_FUNC BB_EXECVP(const char *file, char *const argv[])
Denis Vlasenko1d76f432007-02-06 01:20:12 +000079{
Denys Vlasenkoc71b4692011-02-02 03:28:56 +010080 if (find_applet_by_name(file) >= 0)
81 execvp(bb_busybox_exec_path, argv);
82 return execvp(file, argv);
Denis Vlasenko1d76f432007-02-06 01:20:12 +000083}
84#endif
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020085
Denys Vlasenko82203992016-04-02 18:06:24 +020086void FAST_FUNC BB_EXECVP_or_die(char **argv)
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020087{
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]);
92}
Denys Vlasenko82203992016-04-02 18:06:24 +020093
94/* Typical idiom for applets which exec *optional* PROG [ARGS] */
95void FAST_FUNC exec_prog_or_SHELL(char **argv)
96{
97 if (argv[0]) {
98 BB_EXECVP_or_die(argv);
99 }
100 run_shell(getenv("SHELL"), /*login:*/ 1, NULL, NULL);
101}