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 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
| 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 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 16 | int FAST_FUNC execable_file(const char *name) |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 17 | { |
| 18 | struct stat s; |
| 19 | return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode)); |
| 20 | } |
| 21 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 22 | /* search (*PATHp) for an executable file; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 23 | * return allocated string containing full path if found; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 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/). |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 30 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 31 | char* FAST_FUNC find_execable(const char *filename, char **PATHp) |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 32 | { |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 33 | char *p, *n; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 34 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 35 | p = *PATHp; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 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)) { |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 43 | *PATHp = n; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 44 | return p; |
| 45 | } |
| 46 | free(p); |
| 47 | } |
| 48 | p = n; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 49 | } /* on loop exit p == NULL */ |
| 50 | return p; |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* search $PATH for an executable file; |
| 54 | * return 1 if found; |
| 55 | * return 0 otherwise; |
| 56 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 57 | int FAST_FUNC exists_execable(const char *filename) |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 58 | { |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 59 | char *path = xstrdup(getenv("PATH")); |
| 60 | char *tmp = path; |
| 61 | char *ret = find_execable(filename, &tmp); |
| 62 | free(path); |
Denis Vlasenko | 5cb4648 | 2006-10-12 06:12:11 +0000 | [diff] [blame] | 63 | if (ret) { |
| 64 | free(ret); |
| 65 | return 1; |
| 66 | } |
| 67 | return 0; |
| 68 | } |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 70 | #if ENABLE_FEATURE_PREFER_APPLETS |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 71 | /* just like the real execvp, but try to launch an applet named 'file' first |
| 72 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 73 | int FAST_FUNC bb_execvp(const char *file, char *const argv[]) |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 74 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 75 | return execvp(find_applet_by_name(file) >= 0 ? bb_busybox_exec_path : file, |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 76 | argv); |
| 77 | } |
| 78 | #endif |