diff options
author | Ron Yorston <rmy@pobox.com> | 2012-04-17 11:47:16 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2012-04-17 11:47:16 +0100 |
commit | 7209ab16b8ef7ac9d7a298434aaf456d1cc6a982 (patch) | |
tree | cb0ca0df2bb4d71ff4096eea5c7c8fa8ad2d66e5 /libbb | |
parent | 3f092d0d50a8212dbc3af096f952c34cba128302 (diff) | |
download | busybox-w32-7209ab16b8ef7ac9d7a298434aaf456d1cc6a982.tar.gz busybox-w32-7209ab16b8ef7ac9d7a298434aaf456d1cc6a982.tar.bz2 busybox-w32-7209ab16b8ef7ac9d7a298434aaf456d1cc6a982.zip |
Detect and execute shell scripts based on presence of '#!'
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/execable.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/libbb/execable.c b/libbb/execable.c index e35dad208..afb957e43 100644 --- a/libbb/execable.c +++ b/libbb/execable.c | |||
@@ -16,15 +16,31 @@ | |||
16 | int FAST_FUNC execable_file(const char *name) | 16 | int FAST_FUNC execable_file(const char *name) |
17 | { | 17 | { |
18 | struct stat s; | 18 | struct stat s; |
19 | if (ENABLE_PLATFORM_MINGW32) { | 19 | #if ENABLE_PLATFORM_MINGW32 |
20 | int len = strlen(name); | 20 | if (!stat(name, &s) && S_ISREG(s.st_mode)) { |
21 | return len > 4 && | 21 | int len, fd, n; |
22 | char buf[100]; | ||
23 | |||
24 | if ((len=strlen(name)) > 4 && | ||
22 | (!strcasecmp(name+len-4, ".exe") || | 25 | (!strcasecmp(name+len-4, ".exe") || |
23 | !strcasecmp(name+len-4, ".com")) && | 26 | !strcasecmp(name+len-4, ".com"))) { |
24 | !stat(name, &s) && | 27 | return 1; |
25 | S_ISREG(s.st_mode); | 28 | } |
29 | |||
30 | fd = open(name, O_RDONLY); | ||
31 | if (fd < 0) | ||
32 | return 0; | ||
33 | n = read(fd, buf, sizeof(buf)-1); | ||
34 | close(fd); | ||
35 | if (n < 4) /* at least '#!/x' and not error */ | ||
36 | return 0; | ||
37 | |||
38 | return (buf[0] == '#' && buf[1] == '!'); | ||
26 | } | 39 | } |
40 | return 0; | ||
41 | #else | ||
27 | return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode)); | 42 | return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode)); |
43 | #endif | ||
28 | } | 44 | } |
29 | 45 | ||
30 | /* search (*PATHp) for an executable file; | 46 | /* search (*PATHp) for an executable file; |
@@ -67,11 +83,13 @@ char* FAST_FUNC find_execable(const char *filename, char **PATHp) | |||
67 | memcpy(np+len, ".exe", 5); | 83 | memcpy(np+len, ".exe", 5); |
68 | if (execable_file(np)) { | 84 | if (execable_file(np)) { |
69 | *PATHp = n; | 85 | *PATHp = n; |
86 | free(p); | ||
70 | return np; | 87 | return np; |
71 | } | 88 | } |
72 | memcpy(np+len, ".com", 5); | 89 | memcpy(np+len, ".com", 5); |
73 | if (execable_file(np)) { | 90 | if (execable_file(np)) { |
74 | *PATHp = n; | 91 | *PATHp = n; |
92 | free(p); | ||
75 | return np; | 93 | return np; |
76 | } | 94 | } |
77 | } | 95 | } |