diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2014-05-02 17:15:58 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2014-05-02 17:15:58 +0200 |
commit | e765b5ac349a8f9305e52b3ab2c3ac78c17bf283 (patch) | |
tree | 2dec8a3b34450b7088ae7c3a4b374e953c0a4a73 /libbb/executable.c | |
parent | 15a357e5962634c94ee322fee4da897312090a89 (diff) | |
download | busybox-w32-e765b5ac349a8f9305e52b3ab2c3ac78c17bf283.tar.gz busybox-w32-e765b5ac349a8f9305e52b3ab2c3ac78c17bf283.tar.bz2 busybox-w32-e765b5ac349a8f9305e52b3ab2c3ac78c17bf283.zip |
libbb: rename execable -> executable. No code changes
English speakers complained that it sounded awfully broken.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/executable.c')
-rw-r--r-- | libbb/executable.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/libbb/executable.c b/libbb/executable.c new file mode 100644 index 000000000..85ecc3e6c --- /dev/null +++ b/libbb/executable.c | |||
@@ -0,0 +1,92 @@ | |||
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 | |||
10 | #include "libbb.h" | ||
11 | |||
12 | /* check if path points to an executable file; | ||
13 | * return 1 if found; | ||
14 | * return 0 otherwise; | ||
15 | */ | ||
16 | int FAST_FUNC file_is_executable(const char *name) | ||
17 | { | ||
18 | struct stat s; | ||
19 | return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode)); | ||
20 | } | ||
21 | |||
22 | /* search (*PATHp) for an executable file; | ||
23 | * return allocated string containing full path if found; | ||
24 | * PATHp points to the component after the one where it was found | ||
25 | * (or NULL), | ||
26 | * you may call find_executable 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/). | ||
30 | */ | ||
31 | char* FAST_FUNC find_executable(const char *filename, char **PATHp) | ||
32 | { | ||
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 | */ | ||
41 | char *p, *n; | ||
42 | |||
43 | p = *PATHp; | ||
44 | while (p) { | ||
45 | n = strchr(p, ':'); | ||
46 | if (n) | ||
47 | *n++ = '\0'; | ||
48 | p = concat_path_file( | ||
49 | p[0] ? p : ".", /* handle "::" case */ | ||
50 | filename | ||
51 | ); | ||
52 | if (file_is_executable(p)) { | ||
53 | *PATHp = n; | ||
54 | return p; | ||
55 | } | ||
56 | free(p); | ||
57 | p = n; | ||
58 | } /* on loop exit p == NULL */ | ||
59 | return p; | ||
60 | } | ||
61 | |||
62 | /* search $PATH for an executable file; | ||
63 | * return 1 if found; | ||
64 | * return 0 otherwise; | ||
65 | */ | ||
66 | int FAST_FUNC executable_exists(const char *filename) | ||
67 | { | ||
68 | char *path = xstrdup(getenv("PATH")); | ||
69 | char *tmp = path; | ||
70 | char *ret = find_executable(filename, &tmp); | ||
71 | free(path); | ||
72 | free(ret); | ||
73 | return ret != NULL; | ||
74 | } | ||
75 | |||
76 | #if ENABLE_FEATURE_PREFER_APPLETS | ||
77 | /* just like the real execvp, but try to launch an applet named 'file' first */ | ||
78 | int FAST_FUNC BB_EXECVP(const char *file, char *const argv[]) | ||
79 | { | ||
80 | if (find_applet_by_name(file) >= 0) | ||
81 | execvp(bb_busybox_exec_path, argv); | ||
82 | return execvp(file, argv); | ||
83 | } | ||
84 | #endif | ||
85 | |||
86 | int FAST_FUNC BB_EXECVP_or_die(char **argv) | ||
87 | { | ||
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 | } | ||