From e84152e9e1703402087b6d2776571e291db54e19 Mon Sep 17 00:00:00 2001 From: Glenn L McGrath Date: Mon, 1 Mar 2004 08:32:49 +0000 Subject: Check file has execute permission for the current user, minor formating --- debianutils/which.c | 44 ++++++++++++++++++++------------------------ 1 file 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 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * + * Based on which from debianutils */ -/* getopt not needed */ + #include #include #include +#include + #include "busybox.h" -static int file_exists(char *file) -{ - struct stat filestat; - if (stat(file, &filestat) == 0 && - S_ISREG(filestat.st_mode) && - filestat.st_mode & S_IXUSR) - return 1; - else - return 0; -} - extern int which_main(int argc, char **argv) { - char *path_list, *path_n; - int i, count=1, found, status = EXIT_SUCCESS; + char *path_list; + int i, count=1, status = EXIT_SUCCESS; - if (argc <= 1 || **(argv + 1) == '-') + if (argc <= 1 || **(argv + 1) == '-') { bb_show_usage(); + } argc--; path_list = getenv("PATH"); if (path_list != NULL) { - for(i=strlen(path_list); i > 0; i--) + for (i=strlen(path_list); i > 0; i--) { if (path_list[i]==':') { path_list[i]=0; count++; } + } } else { path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; count = 5; } - while(argc-- > 0) { + while (argc-- > 0) { char *buf; - path_n = path_list; + char *path_n; argv++; - found = 0; + char found = 0; /* * Check if we were given the full path, first. * Otherwise see if the file exists in our $PATH. */ + path_n = path_list; buf = *argv; - if (file_exists(buf)) { - puts(buf); + if (access(buf, X_OK) == 0) { found = 1; } else { for (i = 0; i < count; i++) { buf = concat_path_file(path_n, *argv); - if (file_exists(buf)) { - puts(buf); + if (access(buf, X_OK) == 0) { found = 1; break; } @@ -85,8 +78,11 @@ extern int which_main(int argc, char **argv) path_n += (strlen(path_n) + 1); } } - if (!found) + if (found) { + puts(buf); + } else { status = EXIT_FAILURE; + } } return status; } -- cgit v1.2.3