diff options
author | Pavel Roskin <proski@gnu.org> | 2000-06-05 23:41:27 +0000 |
---|---|---|
committer | Pavel Roskin <proski@gnu.org> | 2000-06-05 23:41:27 +0000 |
commit | c389d9118152e45303173c221a33a8083e769884 (patch) | |
tree | db75949ed22190763981c4b19752e6450723fd8f | |
parent | 82c0ac7e4f2def7ebc7110fa817ce534094af2a2 (diff) | |
download | busybox-w32-c389d9118152e45303173c221a33a8083e769884.tar.gz busybox-w32-c389d9118152e45303173c221a33a8083e769884.tar.bz2 busybox-w32-c389d9118152e45303173c221a33a8083e769884.zip |
"which" rewritten to use stat(). Fixes to improve its compatability
with traditional implementations
-rw-r--r-- | Changelog | 2 | ||||
-rw-r--r-- | findutils/which.c | 63 | ||||
-rw-r--r-- | which.c | 63 |
3 files changed, 92 insertions, 36 deletions
@@ -50,6 +50,8 @@ | |||
50 | * Fixed all fatalError() calls lacking a "\n", thanks to Pavel Roskin. | 50 | * Fixed all fatalError() calls lacking a "\n", thanks to Pavel Roskin. |
51 | * Fixed a segfault in yes when no args were given -- Pavel Roskin. | 51 | * Fixed a segfault in yes when no args were given -- Pavel Roskin. |
52 | * Simplified freeramdisk and added argument checking -- Pavel Roskin. | 52 | * Simplified freeramdisk and added argument checking -- Pavel Roskin. |
53 | * "which" rewritten to use stat(). Fixes to improve its compatability | ||
54 | with traditional implementations -- Pavel Roskin. | ||
53 | * More doc updates | 55 | * More doc updates |
54 | 56 | ||
55 | 57 | ||
diff --git a/findutils/which.c b/findutils/which.c index 46c2204a5..7a11bf787 100644 --- a/findutils/which.c +++ b/findutils/which.c | |||
@@ -23,15 +23,18 @@ | |||
23 | 23 | ||
24 | #include "internal.h" | 24 | #include "internal.h" |
25 | #include <stdio.h> | 25 | #include <stdio.h> |
26 | #include <dirent.h> | 26 | #include <sys/stat.h> |
27 | #include <sys/param.h> | ||
27 | 28 | ||
28 | 29 | ||
29 | extern int which_main(int argc, char **argv) | 30 | extern int which_main(int argc, char **argv) |
30 | { | 31 | { |
31 | char *path_list, *test, *tmp; | 32 | char *path_list, *test, *tmp, *path_parsed; |
32 | struct dirent *next; | 33 | char buf[PATH_MAX]; |
34 | struct stat filestat; | ||
35 | int count = 0; | ||
33 | 36 | ||
34 | if (**(argv + 1) == '-') { | 37 | if (argc <= 1 || **(argv + 1) == '-') { |
35 | usage("which [COMMAND ...]\n" | 38 | usage("which [COMMAND ...]\n" |
36 | #ifndef BB_FEATURE_TRIVIAL_HELP | 39 | #ifndef BB_FEATURE_TRIVIAL_HELP |
37 | "\nLocates a COMMAND.\n" | 40 | "\nLocates a COMMAND.\n" |
@@ -44,21 +47,45 @@ extern int which_main(int argc, char **argv) | |||
44 | if (!path_list) | 47 | if (!path_list) |
45 | path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; | 48 | path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; |
46 | 49 | ||
47 | while(argc-- > 0 && *(argv++) != '\0' && strlen(*argv)) { | 50 | path_parsed = malloc (strlen(path_list) + 1); |
48 | for( test=path_list; (tmp=strchr(test, ':')) && (tmp+1)!=NULL; test=++tmp) { | 51 | strcpy (path_parsed, path_list); |
49 | DIR *dir; | 52 | |
50 | *tmp='\0'; | 53 | /* Replace colons with zeros in path_parsed and count them */ |
51 | //printf("Checking directory '%s'\n", test); | 54 | count = 1; |
52 | dir = opendir(test); | 55 | test = path_parsed; |
53 | if (!dir) | 56 | while (1) { |
54 | continue; | 57 | tmp = strchr(test, ':'); |
55 | while ((next = readdir(dir)) != NULL) { | 58 | if (tmp == NULL) |
56 | //printf("Checking file '%s'\n", next->d_name); | 59 | break; |
57 | if ((strcmp(next->d_name, *argv) == 0)) { | 60 | *tmp = 0; |
58 | printf("%s/%s\n", test, next->d_name); | 61 | test = tmp + 1; |
59 | exit(TRUE); | 62 | count++; |
60 | } | 63 | } |
64 | |||
65 | |||
66 | while(argc-- > 0) { | ||
67 | int i; | ||
68 | int found = FALSE; | ||
69 | test = path_parsed; | ||
70 | argv++; | ||
71 | for (i = 0; i < count; i++) { | ||
72 | strcpy (buf, test); | ||
73 | strcat (buf, "/"); | ||
74 | strcat (buf, *argv); | ||
75 | if (stat (buf, &filestat) == 0 | ||
76 | && filestat.st_mode & S_IXUSR) | ||
77 | { | ||
78 | found = TRUE; | ||
79 | break; | ||
61 | } | 80 | } |
81 | test += (strlen(test) + 1); | ||
82 | } | ||
83 | if (found == TRUE) | ||
84 | printf ("%s\n", buf); | ||
85 | else | ||
86 | { | ||
87 | printf ("which: no %s in (%s)\n", *argv, path_list); | ||
88 | exit (FALSE); | ||
62 | } | 89 | } |
63 | } | 90 | } |
64 | exit(TRUE); | 91 | exit(TRUE); |
@@ -23,15 +23,18 @@ | |||
23 | 23 | ||
24 | #include "internal.h" | 24 | #include "internal.h" |
25 | #include <stdio.h> | 25 | #include <stdio.h> |
26 | #include <dirent.h> | 26 | #include <sys/stat.h> |
27 | #include <sys/param.h> | ||
27 | 28 | ||
28 | 29 | ||
29 | extern int which_main(int argc, char **argv) | 30 | extern int which_main(int argc, char **argv) |
30 | { | 31 | { |
31 | char *path_list, *test, *tmp; | 32 | char *path_list, *test, *tmp, *path_parsed; |
32 | struct dirent *next; | 33 | char buf[PATH_MAX]; |
34 | struct stat filestat; | ||
35 | int count = 0; | ||
33 | 36 | ||
34 | if (**(argv + 1) == '-') { | 37 | if (argc <= 1 || **(argv + 1) == '-') { |
35 | usage("which [COMMAND ...]\n" | 38 | usage("which [COMMAND ...]\n" |
36 | #ifndef BB_FEATURE_TRIVIAL_HELP | 39 | #ifndef BB_FEATURE_TRIVIAL_HELP |
37 | "\nLocates a COMMAND.\n" | 40 | "\nLocates a COMMAND.\n" |
@@ -44,21 +47,45 @@ extern int which_main(int argc, char **argv) | |||
44 | if (!path_list) | 47 | if (!path_list) |
45 | path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; | 48 | path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; |
46 | 49 | ||
47 | while(argc-- > 0 && *(argv++) != '\0' && strlen(*argv)) { | 50 | path_parsed = malloc (strlen(path_list) + 1); |
48 | for( test=path_list; (tmp=strchr(test, ':')) && (tmp+1)!=NULL; test=++tmp) { | 51 | strcpy (path_parsed, path_list); |
49 | DIR *dir; | 52 | |
50 | *tmp='\0'; | 53 | /* Replace colons with zeros in path_parsed and count them */ |
51 | //printf("Checking directory '%s'\n", test); | 54 | count = 1; |
52 | dir = opendir(test); | 55 | test = path_parsed; |
53 | if (!dir) | 56 | while (1) { |
54 | continue; | 57 | tmp = strchr(test, ':'); |
55 | while ((next = readdir(dir)) != NULL) { | 58 | if (tmp == NULL) |
56 | //printf("Checking file '%s'\n", next->d_name); | 59 | break; |
57 | if ((strcmp(next->d_name, *argv) == 0)) { | 60 | *tmp = 0; |
58 | printf("%s/%s\n", test, next->d_name); | 61 | test = tmp + 1; |
59 | exit(TRUE); | 62 | count++; |
60 | } | 63 | } |
64 | |||
65 | |||
66 | while(argc-- > 0) { | ||
67 | int i; | ||
68 | int found = FALSE; | ||
69 | test = path_parsed; | ||
70 | argv++; | ||
71 | for (i = 0; i < count; i++) { | ||
72 | strcpy (buf, test); | ||
73 | strcat (buf, "/"); | ||
74 | strcat (buf, *argv); | ||
75 | if (stat (buf, &filestat) == 0 | ||
76 | && filestat.st_mode & S_IXUSR) | ||
77 | { | ||
78 | found = TRUE; | ||
79 | break; | ||
61 | } | 80 | } |
81 | test += (strlen(test) + 1); | ||
82 | } | ||
83 | if (found == TRUE) | ||
84 | printf ("%s\n", buf); | ||
85 | else | ||
86 | { | ||
87 | printf ("which: no %s in (%s)\n", *argv, path_list); | ||
88 | exit (FALSE); | ||
62 | } | 89 | } |
63 | } | 90 | } |
64 | exit(TRUE); | 91 | exit(TRUE); |