aboutsummaryrefslogtreecommitdiff
path: root/debianutils
diff options
context:
space:
mode:
authorGravatar Denis Vlasenko <vda.linux@googlemail.com>2008-06-05 13:33:59 +0000
committerGravatar Denis Vlasenko <vda.linux@googlemail.com>2008-06-05 13:33:59 +0000
commitf592aa36f33430b866e9d7c975c6b9c356100d4b (patch)
treef7a8c565817ce331c6e18a2a399074c6c615cd79 /debianutils
parentf0d6068086b701522b92a7dd941739cf4fbb71e8 (diff)
downloadbusybox-f592aa36f33430b866e9d7c975c6b9c356100d4b.tar.gz
busybox-f592aa36f33430b866e9d7c975c6b9c356100d4b.tar.bz2
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
Diffstat (limited to 'debianutils')
-rw-r--r--debianutils/which.c64
1 files changed, 52 insertions, 12 deletions
diff --git a/debianutils/which.c b/debianutils/which.c
index 5ab67194d..41a864cfa 100644
--- a/debianutils/which.c
+++ b/debianutils/which.c
@@ -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 @@ int which_main(int argc, char **argv)
}
}
status = EXIT_FAILURE;
- }
+#endif
+ } while (*(++argv) != NULL);
fflush_stdout_and_exit(status);
}