aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2012-04-17 11:42:11 +0100
committerRon Yorston <rmy@pobox.com>2012-04-17 11:42:11 +0100
commit8c77ef82b5e6e8c53b9d0103ae182587d5088ced (patch)
treeee4cf19b9b0e59f1d0c67b20d76d6a9b96c5ab58 /libbb
parent1d1f787a7c2864149d037681badd196b363a3503 (diff)
downloadbusybox-w32-8c77ef82b5e6e8c53b9d0103ae182587d5088ced.tar.gz
busybox-w32-8c77ef82b5e6e8c53b9d0103ae182587d5088ced.tar.bz2
busybox-w32-8c77ef82b5e6e8c53b9d0103ae182587d5088ced.zip
Detect and execute shell scripts based on presence of '#!'
Diffstat (limited to 'libbb')
-rw-r--r--libbb/execable.c30
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 @@
16int FAST_FUNC execable_file(const char *name) 16int 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 }