[BusyBox] [PATCH] `which' does not find files when full path specified

Arthur Othieno a.othieno at bluewin.ch
Tue Oct 21 08:00:55 UTC 2003


On Tue, Oct 21, 2003 at 12:59:01AM +0200, Tomasz Motylewski wrote:
> 
> busybox v0.60.5
> 
> #which opcontrol 
> /usr/local/bin/opcontrol
> 
> #which /usr/local/bin/opcontrol
> 
> 
> Debian woody:
> #which opcontrol
> /usr/local/bin/opcontrol
> 
> #which /usr/local/bin/opcontrol
> /usr/local/bin/opcontrol
> 
> Best regards,
> --
> Tomasz Motylewski

Fix attached.


-- 
Linux is a true multitasking system. Are you?
-------------- next part --------------
Index: debianutils/which.c
===================================================================
RCS file: /var/cvs/busybox/debianutils/which.c,v
retrieving revision 1.3
diff -u -r1.3 which.c
--- debianutils/which.c	14 Jul 2003 21:20:47 -0000	1.3
+++ debianutils/which.c	21 Oct 2003 07:56:23 -0000
@@ -26,10 +26,19 @@
 #include <stdlib.h>
 #include "busybox.h"
 
+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) == '-')
@@ -52,18 +61,27 @@
 		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 mailing list