Check file has execute permission for the current user, minor formating

This commit is contained in:
Glenn L McGrath 2004-03-01 08:32:49 +00:00
parent d5d5e54290
commit e84152e9e1

View File

@ -18,42 +18,36 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
* Based on which from debianutils
*/ */
/* getopt not needed */
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "busybox.h" #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) extern int which_main(int argc, char **argv)
{ {
char *path_list, *path_n; char *path_list;
int i, count=1, found, status = EXIT_SUCCESS; int i, count=1, status = EXIT_SUCCESS;
if (argc <= 1 || **(argv + 1) == '-') if (argc <= 1 || **(argv + 1) == '-') {
bb_show_usage(); bb_show_usage();
}
argc--; argc--;
path_list = getenv("PATH"); path_list = getenv("PATH");
if (path_list != NULL) { if (path_list != NULL) {
for(i=strlen(path_list); i > 0; i--) for (i=strlen(path_list); i > 0; i--) {
if (path_list[i]==':') { if (path_list[i]==':') {
path_list[i]=0; path_list[i]=0;
count++; count++;
} }
}
} else { } else {
path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin";
count = 5; count = 5;
@ -61,23 +55,22 @@ extern int which_main(int argc, char **argv)
while (argc-- > 0) { while (argc-- > 0) {
char *buf; char *buf;
path_n = path_list; char *path_n;
argv++; argv++;
found = 0; char found = 0;
/* /*
* Check if we were given the full path, first. * Check if we were given the full path, first.
* Otherwise see if the file exists in our $PATH. * Otherwise see if the file exists in our $PATH.
*/ */
path_n = path_list;
buf = *argv; buf = *argv;
if (file_exists(buf)) { if (access(buf, X_OK) == 0) {
puts(buf);
found = 1; found = 1;
} else { } else {
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
buf = concat_path_file(path_n, *argv); buf = concat_path_file(path_n, *argv);
if (file_exists(buf)) { if (access(buf, X_OK) == 0) {
puts(buf);
found = 1; found = 1;
break; break;
} }
@ -85,9 +78,12 @@ extern int which_main(int argc, char **argv)
path_n += (strlen(path_n) + 1); path_n += (strlen(path_n) + 1);
} }
} }
if (!found) if (found) {
puts(buf);
} else {
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
}
return status; return status;
} }