[BusyBox-cvs] busybox.stable which.c,1.21,1.22

Erik Andersen andersen at busybox.net
Wed Oct 22 10:38:26 UTC 2003


Update of /var/cvs/busybox.stable
In directory winder:/tmp/cvs-serv12063

Modified Files:
	which.c 
Log Message:
Tomasz Motylewski reported that the 'which' applet does not find
files when the full file PATH is specified.

This patch from Arthur Othieno fixes it.


Index: which.c
===================================================================
RCS file: /var/cvs/busybox.stable/which.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- which.c	19 Nov 2001 23:34:17 -0000	1.21
+++ which.c	22 Oct 2003 10:38:22 -0000	1.22
@@ -2,8 +2,7 @@
 /*
  * Which implementation for busybox
  *
- * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
- * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee at debian.org>
+ * Copyright (C) 1999-2003 by Erik Andersen <andersen at codepoet.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,10 +26,19 @@
 #include <stdlib.h>
 #include "busybox.h"
 
+static int file_exists(char *file)
+{
+	struct stat filestat;
+
+	if (stat(file, &filestat) == 0 && filestat.st_mode & S_IXUSR)
+		return 1;
+	else
+		return 0;
+}
+	
 extern int which_main(int argc, char **argv)
 {
 	char *path_list, *path_n;
-	struct stat filestat;
 	int i, count=1, found, status = EXIT_SUCCESS;
 
 	if (argc <= 1 || **(argv + 1) == '-')
@@ -38,32 +46,42 @@
 	argc--;
 
 	path_list = getenv("PATH");
-	if (!path_list)
-		path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
-
-	/* Replace colons with zeros in path_parsed and count them */
-	for(i=strlen(path_list); i > 0; i--) 
-		if (path_list[i]==':') {
-			path_list[i]=0;
-			count++;
-		}
+	if (path_list != NULL) {
+		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) { 
 		path_n = path_list;
 		argv++;
 		found = 0;
-		for (i = 0; i < count; i++) {
-			char *buf;
-			buf = concat_path_file(path_n, *argv);
-			if (stat (buf, &filestat) == 0
-			    && filestat.st_mode & S_IXUSR)
-			{
-				puts(buf);
-				found = 1;
-				break;
+		char *buf;
+
+		/*
+		 * Check if we were given the full path, first.
+		 * Otherwise see if the file exists in our $PATH.
+		 */
+		buf = *argv;
+		if (file_exists(buf)) {
+			puts(buf);
+			found = 1;
+		} else {
+			for (i = 0; i < count; i++) {
+				buf = concat_path_file(path_n, *argv);
+				if (file_exists(buf)) {
+					puts(buf);
+					found = 1;
+					break;
+				}
+				free(buf);
+				path_n += (strlen(path_n) + 1);
 			}
-			free(buf);
-			path_n += (strlen(path_n) + 1);
 		}
 		if (!found)
 			status = EXIT_FAILURE;




More information about the busybox-cvs mailing list