[BusyBox-cvs] busybox/debianutils which.c,1.6,1.7

Glenn McGrath bug1 at busybox.net
Mon Mar 1 08:32:52 UTC 2004


Update of /var/cvs/busybox/debianutils
In directory nail:/tmp/cvs-serv19360/debianutils

Modified Files:
	which.c 
Log Message:
Check file has execute permission for the current user, minor formating


Index: which.c
===================================================================
RCS file: /var/cvs/busybox/debianutils/which.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- a/which.c	29 Oct 2003 04:50:35 -0000	1.6
+++ b/which.c	1 Mar 2004 08:32:49 -0000	1.7
@@ -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 <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.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)
 {
-	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 @@
 				path_n += (strlen(path_n) + 1);
 			}
 		}
-		if (!found)
+		if (found) {
+			puts(buf);
+		} else {
 			status = EXIT_FAILURE;
+		}
 	}
 	return status;
 }




More information about the busybox-cvs mailing list