[BusyBox] "which" issue

Felipe Kellermann stdfk at terra.com.br
Wed Dec 1 05:08:45 UTC 2004


On Tue, 30 Nov 2004 2:20pm  -0600, Brian T wrote:

> Seems like a problem with the "which" applet looking in the current 
> running directoy, and then stopping if it finds a match.  I have a 
> directory called "cron" in the /root/ directory on an embedded unit.

OK, which is really broken about the wd...
Stopping if it finds a match is the default behaviour.


> [root at testhost root]# find / -name cron
> /bin/cron
> /var/cron
> /root/cron
> [root at testhost root]# cd ..
> [root at testhost /]# which cron
> /bin/cron
> [root at testhost /]#
> [root at testhost /]# echo $PATH
> /usr/sbin:/bin:/usr/bin:/sbin

Using the attached patch:
~ $ echo $PATH
/bin:/usr/bin
~ $ which ls
/bin/ls
~ $ touch ls && chmod +x ls
~ $ which ls
/bin/ls

Forcing an absolute argument:
~ $ which /tmp/ls
no ls in (/tmp)
~ $ which ./ls
./ls

Attached is the patch that fixes the problem -- I just removed the 'no %s 
in %s' (basename, dirname) part as it's not used on busybox's which and I 
added just to make my example discernible.

-- 
Felipe Kellermann
-------------- next part --------------
Index: debianutils/which.c
===================================================================
RCS file: /var/cvs/busybox/debianutils/which.c,v
retrieving revision 1.9
diff -u -3 -p -u -p -r1.9 which.c
--- debianutils/which.c	15 Mar 2004 08:28:24 -0000	1.9
+++ debianutils/which.c	1 Dec 2004 05:06:11 -0000
@@ -65,8 +65,10 @@ extern int which_main(int argc, char **a
 		 */
 		path_n = path_list;
 		buf = *argv;
-		if (access(buf, X_OK) == 0) {
-			found = 1;
+		if (strchr(buf, '/') != NULL) {
+			if (access(buf, X_OK) == 0) {
+				found = 1;
+			}
 		} else {
 			for (i = 0; i < count; i++) {
 				buf = concat_path_file(path_n, *argv);


More information about the busybox mailing list