blob: 325dd0107b89745fce644928ff91f74084f347a7 [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 */
Denis Vlasenko5cb46482006-10-12 06:12:11 +00009#include "libbb.h"
10
11/* check if path points to an executable file;
12 * return 1 if found;
13 * return 0 otherwise;
14 */
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020015int FAST_FUNC file_is_executable(const char *name)
Denis Vlasenko5cb46482006-10-12 06:12:11 +000016{
17 struct stat s;
18 return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode));
19}
20
Denis Vlasenkof592aa32008-06-05 13:33:59 +000021/* search (*PATHp) for an executable file;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000022 * return allocated string containing full path if found;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000023 * PATHp points to the component after the one where it was found
24 * (or NULL),
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020025 * you may call find_executable again with this PATHp to continue
Denis Vlasenkof592aa32008-06-05 13:33:59 +000026 * (if it's not NULL).
27 * return NULL otherwise; (PATHp is undefined)
28 * in all cases (*PATHp) contents will be trashed (s/:/NUL/).
Denis Vlasenko5cb46482006-10-12 06:12:11 +000029 */
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020030char* FAST_FUNC find_executable(const char *filename, char **PATHp)
Denis Vlasenko5cb46482006-10-12 06:12:11 +000031{
Denys Vlasenko15a357e2014-05-02 17:08:29 +020032 /* 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 */
Denis Vlasenkof592aa32008-06-05 13:33:59 +000040 char *p, *n;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000041
Denis Vlasenkof592aa32008-06-05 13:33:59 +000042 p = *PATHp;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000043 while (p) {
44 n = strchr(p, ':');
45 if (n)
46 *n++ = '\0';
Denys Vlasenko15a357e2014-05-02 17:08:29 +020047 p = concat_path_file(
48 p[0] ? p : ".", /* handle "::" case */
49 filename
50 );
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020051 if (file_is_executable(p)) {
Denys Vlasenko15a357e2014-05-02 17:08:29 +020052 *PATHp = n;
53 return p;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000054 }
Denys Vlasenko15a357e2014-05-02 17:08:29 +020055 free(p);
Denis Vlasenko5cb46482006-10-12 06:12:11 +000056 p = n;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000057 } /* on loop exit p == NULL */
58 return p;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000059}
60
61/* search $PATH for an executable file;
62 * return 1 if found;
63 * return 0 otherwise;
64 */
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020065int FAST_FUNC executable_exists(const char *filename)
Denis Vlasenko5cb46482006-10-12 06:12:11 +000066{
Denis Vlasenkof592aa32008-06-05 13:33:59 +000067 char *path = xstrdup(getenv("PATH"));
68 char *tmp = path;
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020069 char *ret = find_executable(filename, &tmp);
Denis Vlasenkof592aa32008-06-05 13:33:59 +000070 free(path);
Denys Vlasenko15a357e2014-05-02 17:08:29 +020071 free(ret);
72 return ret != NULL;
Denis Vlasenko5cb46482006-10-12 06:12:11 +000073}
Denis Vlasenko1d76f432007-02-06 01:20:12 +000074
Denis Vlasenko80d14be2007-04-10 23:03:30 +000075#if ENABLE_FEATURE_PREFER_APPLETS
Denys Vlasenkoc71b4692011-02-02 03:28:56 +010076/* just like the real execvp, but try to launch an applet named 'file' first */
77int FAST_FUNC BB_EXECVP(const char *file, char *const argv[])
Denis Vlasenko1d76f432007-02-06 01:20:12 +000078{
Denys Vlasenkoc71b4692011-02-02 03:28:56 +010079 if (find_applet_by_name(file) >= 0)
80 execvp(bb_busybox_exec_path, argv);
81 return execvp(file, argv);
Denis Vlasenko1d76f432007-02-06 01:20:12 +000082}
83#endif
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020084
Denys Vlasenko82203992016-04-02 18:06:24 +020085void FAST_FUNC BB_EXECVP_or_die(char **argv)
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020086{
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}
Denys Vlasenko82203992016-04-02 18:06:24 +020092
93/* Typical idiom for applets which exec *optional* PROG [ARGS] */
94void FAST_FUNC exec_prog_or_SHELL(char **argv)
95{
96 if (argv[0]) {
97 BB_EXECVP_or_die(argv);
98 }
Denys Vlasenko79e25982016-11-03 22:13:08 +010099 run_shell(getenv("SHELL"), /*login:*/ 1, NULL);
Denys Vlasenko82203992016-04-02 18:06:24 +0200100}