svn commit: trunk/busybox: debianutils include libbb

vda at busybox.net vda at busybox.net
Thu Jun 5 13:33:59 UTC 2008


Author: vda
Date: 2008-06-05 06:33:59 -0700 (Thu, 05 Jun 2008)
New Revision: 22234

Log:
which: -a support (needed for bfin uclibc build script)

real support (with CONFIG_DESKTOP=y): 120+ bytes:
   text	   data	    bss	    dec	    hex	filename
 807958	    624	   7036	 815618	  c7202	busybox_old
 808085	    624	   7036	 815745	  c7281	busybox_unstripped

"fake" support (with CONFIG_DESKTOP unset): ~45 bytes:
   text	   data	    bss	    dec	    hex	filename
 797790	    611	   6996	 805397	  c4a15	busybox_old
 797834	    611	   6996	 805441	  c4a41	busybox_unstripped



Modified:
   trunk/busybox/debianutils/which.c
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/execable.c


Changeset:
Modified: trunk/busybox/debianutils/which.c
===================================================================
--- trunk/busybox/debianutils/which.c	2008-06-05 12:29:36 UTC (rev 22233)
+++ trunk/busybox/debianutils/which.c	2008-06-05 13:33:59 UTC (rev 22234)
@@ -13,30 +13,69 @@
 #include "libbb.h"
 
 int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int which_main(int argc, char **argv)
+int which_main(int argc ATTRIBUTE_UNUSED, char **argv)
 {
+	USE_DESKTOP(int opt;)
 	int status = EXIT_SUCCESS;
+	char *path;
 	char *p;
 
-	if (argc <= 1 || argv[1][0] == '-') {
-		bb_show_usage();
-	}
+	opt_complementary = "-1"; /* at least one argument */
+	USE_DESKTOP(opt =) getopt32(argv, "a");
+	argv += optind;
 
-	/* This matches what is seen on e.g. ubuntu
-	 * "which" there is a shell script */
-	if (!getenv("PATH")) {
-		putenv((char*)bb_PATH_root_path);
+	/* This matches what is seen on e.g. ubuntu.
+	 * "which" there is a shell script. */
+	path = getenv("PATH");
+	if (!path) {
+		path = (char*)bb_PATH_root_path;
+		putenv(path);
+		path += 5; /* skip "PATH=" */
 	}
 
-	while (--argc > 0) {
-		argv++;
+	do {
+#if ENABLE_DESKTOP
+/* Much bloat just to support -a */
 		if (strchr(*argv, '/')) {
 			if (execable_file(*argv)) {
 				puts(*argv);
 				continue;
 			}
+			status = EXIT_FAILURE;
 		} else {
-			p = find_execable(*argv);
+			char *path2 = xstrdup(path);
+			char *tmp = path2;
+
+			p = find_execable(*argv, &tmp);
+			if (!p)
+				status = EXIT_FAILURE;
+			else {
+ print:
+				puts(p);
+				free(p);
+				if (opt) {
+					/* -a: show matches in all PATH components */
+					if (tmp) {
+						p = find_execable(*argv, &tmp);
+						if (p)
+							goto print;
+					}
+				}
+			}
+			free(path2);
+		}
+#else
+/* Just ignoring -a */
+		if (strchr(*argv, '/')) {
+			if (execable_file(*argv)) {
+				puts(*argv);
+				continue;
+			}
+		} else {
+			char *path2 = xstrdup(path);
+			char *tmp = path2;
+			p = find_execable(*argv, &tmp);
+			free(path2);
 			if (p) {
 				puts(p);
 				free(p);
@@ -44,7 +83,8 @@
 			}
 		}
 		status = EXIT_FAILURE;
-	}
+#endif
+	} while (*(++argv) != NULL);
 
 	fflush_stdout_and_exit(status);
 }

Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2008-06-05 12:29:36 UTC (rev 22233)
+++ trunk/busybox/include/libbb.h	2008-06-05 13:33:59 UTC (rev 22234)
@@ -700,7 +700,7 @@
 #endif
 
 int execable_file(const char *name);
-char *find_execable(const char *filename);
+char *find_execable(const char *filename, char **PATHp);
 int exists_execable(const char *filename);
 
 /* BB_EXECxx always execs (it's not doing NOFORK/NOEXEC stuff),

Modified: trunk/busybox/libbb/execable.c
===================================================================
--- trunk/busybox/libbb/execable.c	2008-06-05 12:29:36 UTC (rev 22233)
+++ trunk/busybox/libbb/execable.c	2008-06-05 13:33:59 UTC (rev 22234)
@@ -19,15 +19,20 @@
 	return (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode));
 }
 
-/* search $PATH for an executable file;
+/* search (*PATHp) for an executable file;
  * return allocated string containing full path if found;
- * return NULL otherwise;
+ *  PATHp points to the component after the one where it was found
+ *  (or NULL),
+ *  you may call find_execable again with this PATHp to continue
+ *  (if it's not NULL).
+ * return NULL otherwise; (PATHp is undefined)
+ * in all cases (*PATHp) contents will be trashed (s/:/NUL/).
  */
-char *find_execable(const char *filename)
+char *find_execable(const char *filename, char **PATHp)
 {
-	char *path, *p, *n;
+	char *p, *n;
 
-	p = path = xstrdup(getenv("PATH"));
+	p = *PATHp;
 	while (p) {
 		n = strchr(p, ':');
 		if (n)
@@ -35,15 +40,14 @@
 		if (*p != '\0') { /* it's not a PATH="foo::bar" situation */
 			p = concat_path_file(p, filename);
 			if (execable_file(p)) {
-				free(path);
+				*PATHp = n;
 				return p;
 			}
 			free(p);
 		}
 		p = n;
-	}
-	free(path);
-	return NULL;
+	} /* on loop exit p == NULL */
+	return p;
 }
 
 /* search $PATH for an executable file;
@@ -52,7 +56,10 @@
  */
 int exists_execable(const char *filename)
 {
-	char *ret = find_execable(filename);
+	char *path = xstrdup(getenv("PATH"));
+	char *tmp = path;
+	char *ret = find_execable(filename, &tmp);
+	free(path);
 	if (ret) {
 		free(ret);
 		return 1;




More information about the busybox-cvs mailing list