diff options
| author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2004-03-01 08:32:49 +0000 |
|---|---|---|
| committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2004-03-01 08:32:49 +0000 |
| commit | 90e651a35a5fba399b84f4760dfbda731eea5ca5 (patch) | |
| tree | 21ee5fc99e6fcf75843d65f78bee13d00fd0f77c /debianutils | |
| parent | c2376ec4f9dc1bdaa4fe228942cf4cbc3d64c240 (diff) | |
| download | busybox-w32-90e651a35a5fba399b84f4760dfbda731eea5ca5.tar.gz busybox-w32-90e651a35a5fba399b84f4760dfbda731eea5ca5.tar.bz2 busybox-w32-90e651a35a5fba399b84f4760dfbda731eea5ca5.zip | |
Check file has execute permission for the current user, minor formating
git-svn-id: svn://busybox.net/trunk/busybox@8569 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'debianutils')
| -rw-r--r-- | debianutils/which.c | 44 |
1 files changed, 20 insertions, 24 deletions
diff --git a/debianutils/which.c b/debianutils/which.c index 120f1e72f..1e9d276fd 100644 --- a/debianutils/which.c +++ b/debianutils/which.c | |||
| @@ -18,66 +18,59 @@ | |||
| 18 | * along with this program; if not, write to the Free Software | 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * | 20 | * |
| 21 | * Based on which from debianutils | ||
| 21 | */ | 22 | */ |
| 22 | 23 | ||
| 23 | /* getopt not needed */ | 24 | |
| 24 | #include <string.h> | 25 | #include <string.h> |
| 25 | #include <stdio.h> | 26 | #include <stdio.h> |
| 26 | #include <stdlib.h> | 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> | ||
| 29 | |||
| 27 | #include "busybox.h" | 30 | #include "busybox.h" |
| 28 | 31 | ||
| 29 | static int file_exists(char *file) | ||
| 30 | { | ||
| 31 | struct stat filestat; | ||
| 32 | 32 | ||
| 33 | if (stat(file, &filestat) == 0 && | ||
| 34 | S_ISREG(filestat.st_mode) && | ||
| 35 | filestat.st_mode & S_IXUSR) | ||
| 36 | return 1; | ||
| 37 | else | ||
| 38 | return 0; | ||
| 39 | } | ||
| 40 | |||
| 41 | extern int which_main(int argc, char **argv) | 33 | extern int which_main(int argc, char **argv) |
| 42 | { | 34 | { |
| 43 | char *path_list, *path_n; | 35 | char *path_list; |
| 44 | int i, count=1, found, status = EXIT_SUCCESS; | 36 | int i, count=1, status = EXIT_SUCCESS; |
| 45 | 37 | ||
| 46 | if (argc <= 1 || **(argv + 1) == '-') | 38 | if (argc <= 1 || **(argv + 1) == '-') { |
| 47 | bb_show_usage(); | 39 | bb_show_usage(); |
| 40 | } | ||
| 48 | argc--; | 41 | argc--; |
| 49 | 42 | ||
| 50 | path_list = getenv("PATH"); | 43 | path_list = getenv("PATH"); |
| 51 | if (path_list != NULL) { | 44 | if (path_list != NULL) { |
| 52 | for(i=strlen(path_list); i > 0; i--) | 45 | for (i=strlen(path_list); i > 0; i--) { |
| 53 | if (path_list[i]==':') { | 46 | if (path_list[i]==':') { |
| 54 | path_list[i]=0; | 47 | path_list[i]=0; |
| 55 | count++; | 48 | count++; |
| 56 | } | 49 | } |
| 50 | } | ||
| 57 | } else { | 51 | } else { |
| 58 | path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; | 52 | path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; |
| 59 | count = 5; | 53 | count = 5; |
| 60 | } | 54 | } |
| 61 | 55 | ||
| 62 | while(argc-- > 0) { | 56 | while (argc-- > 0) { |
| 63 | char *buf; | 57 | char *buf; |
| 64 | path_n = path_list; | 58 | char *path_n; |
| 65 | argv++; | 59 | argv++; |
| 66 | found = 0; | 60 | char found = 0; |
| 67 | 61 | ||
| 68 | /* | 62 | /* |
| 69 | * Check if we were given the full path, first. | 63 | * Check if we were given the full path, first. |
| 70 | * Otherwise see if the file exists in our $PATH. | 64 | * Otherwise see if the file exists in our $PATH. |
| 71 | */ | 65 | */ |
| 66 | path_n = path_list; | ||
| 72 | buf = *argv; | 67 | buf = *argv; |
| 73 | if (file_exists(buf)) { | 68 | if (access(buf, X_OK) == 0) { |
| 74 | puts(buf); | ||
| 75 | found = 1; | 69 | found = 1; |
| 76 | } else { | 70 | } else { |
| 77 | for (i = 0; i < count; i++) { | 71 | for (i = 0; i < count; i++) { |
| 78 | buf = concat_path_file(path_n, *argv); | 72 | buf = concat_path_file(path_n, *argv); |
| 79 | if (file_exists(buf)) { | 73 | if (access(buf, X_OK) == 0) { |
| 80 | puts(buf); | ||
| 81 | found = 1; | 74 | found = 1; |
| 82 | break; | 75 | break; |
| 83 | } | 76 | } |
| @@ -85,8 +78,11 @@ extern int which_main(int argc, char **argv) | |||
| 85 | path_n += (strlen(path_n) + 1); | 78 | path_n += (strlen(path_n) + 1); |
| 86 | } | 79 | } |
| 87 | } | 80 | } |
| 88 | if (!found) | 81 | if (found) { |
| 82 | puts(buf); | ||
| 83 | } else { | ||
| 89 | status = EXIT_FAILURE; | 84 | status = EXIT_FAILURE; |
| 85 | } | ||
| 90 | } | 86 | } |
| 91 | return status; | 87 | return status; |
| 92 | } | 88 | } |
