Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
| 11 | /* check if path points to an executable file; |
| 12 | * return 1 if found; |
| 13 | * return 0 otherwise; |
| 14 | */ |
Denys Vlasenko | e765b5a | 2014-05-02 17:15:58 +0200 | [diff] [blame] | 15 | int FAST_FUNC file_is_executable(const char *name) |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 16 | { |
| 17 | struct stat s; |
| 18 | return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode)); |
| 19 | } |
| 20 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 21 | /* search (*PATHp) for an executable file; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 22 | * return allocated string containing full path if found; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 23 | * PATHp points to the component after the one where it was found |
| 24 | * (or NULL), |
Denys Vlasenko | e765b5a | 2014-05-02 17:15:58 +0200 | [diff] [blame] | 25 | * you may call find_executable again with this PATHp to continue |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 26 | * (if it's not NULL). |
| 27 | * return NULL otherwise; (PATHp is undefined) |
Denys Vlasenko | cca7c61 | 2018-01-12 13:21:33 +0100 | [diff] [blame] | 28 | * in all cases (*PATHp) contents are temporarily modified |
| 29 | * but are restored on return (s/:/NUL/ and back). |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 30 | */ |
Denys Vlasenko | e765b5a | 2014-05-02 17:15:58 +0200 | [diff] [blame] | 31 | char* FAST_FUNC find_executable(const char *filename, char **PATHp) |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 32 | { |
Denys Vlasenko | 15a357e | 2014-05-02 17:08:29 +0200 | [diff] [blame] | 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. |
| 40 | */ |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 41 | char *p, *n; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 43 | p = *PATHp; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 44 | while (p) { |
Denys Vlasenko | cca7c61 | 2018-01-12 13:21:33 +0100 | [diff] [blame] | 45 | int ex; |
| 46 | |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 47 | n = strchr(p, ':'); |
Denys Vlasenko | cca7c61 | 2018-01-12 13:21:33 +0100 | [diff] [blame] | 48 | if (n) *n = '\0'; |
Denys Vlasenko | 15a357e | 2014-05-02 17:08:29 +0200 | [diff] [blame] | 49 | p = concat_path_file( |
| 50 | p[0] ? p : ".", /* handle "::" case */ |
| 51 | filename |
| 52 | ); |
Denys Vlasenko | cca7c61 | 2018-01-12 13:21:33 +0100 | [diff] [blame] | 53 | ex = file_is_executable(p); |
| 54 | if (n) *n++ = ':'; |
| 55 | if (ex) { |
Denys Vlasenko | 15a357e | 2014-05-02 17:08:29 +0200 | [diff] [blame] | 56 | *PATHp = n; |
| 57 | return p; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 58 | } |
Denys Vlasenko | 15a357e | 2014-05-02 17:08:29 +0200 | [diff] [blame] | 59 | free(p); |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 60 | p = n; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 61 | } /* on loop exit p == NULL */ |
| 62 | return p; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* search $PATH for an executable file; |
| 66 | * return 1 if found; |
| 67 | * return 0 otherwise; |
| 68 | */ |
Denys Vlasenko | e765b5a | 2014-05-02 17:15:58 +0200 | [diff] [blame] | 69 | int FAST_FUNC executable_exists(const char *filename) |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 70 | { |
Denys Vlasenko | cca7c61 | 2018-01-12 13:21:33 +0100 | [diff] [blame] | 71 | char *path = getenv("PATH"); |
| 72 | char *ret = find_executable(filename, &path); |
Denys Vlasenko | 15a357e | 2014-05-02 17:08:29 +0200 | [diff] [blame] | 73 | free(ret); |
| 74 | return ret != NULL; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 75 | } |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 77 | #if ENABLE_FEATURE_PREFER_APPLETS |
Denys Vlasenko | c71b469 | 2011-02-02 03:28:56 +0100 | [diff] [blame] | 78 | /* just like the real execvp, but try to launch an applet named 'file' first */ |
| 79 | int FAST_FUNC BB_EXECVP(const char *file, char *const argv[]) |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 80 | { |
Denys Vlasenko | c71b469 | 2011-02-02 03:28:56 +0100 | [diff] [blame] | 81 | if (find_applet_by_name(file) >= 0) |
| 82 | execvp(bb_busybox_exec_path, argv); |
| 83 | return execvp(file, argv); |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 84 | } |
| 85 | #endif |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 86 | |
Denys Vlasenko | 8220399 | 2016-04-02 18:06:24 +0200 | [diff] [blame] | 87 | void FAST_FUNC BB_EXECVP_or_die(char **argv) |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 88 | { |
| 89 | BB_EXECVP(argv[0], argv); |
| 90 | /* SUSv3-mandated exit codes */ |
| 91 | xfunc_error_retval = (errno == ENOENT) ? 127 : 126; |
| 92 | bb_perror_msg_and_die("can't execute '%s'", argv[0]); |
| 93 | } |