Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Which implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu> |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 9 | * |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 10 | * Based on which from debianutils |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Bernhard Reutner-Fischer | 66e3a22 | 2006-06-14 16:17:50 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 15 | int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 16 | int which_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 17 | { |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 18 | USE_DESKTOP(int opt;) |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 19 | int status = EXIT_SUCCESS; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 20 | char *path; |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 21 | char *p; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 23 | opt_complementary = "-1"; /* at least one argument */ |
| 24 | USE_DESKTOP(opt =) getopt32(argv, "a"); |
| 25 | argv += optind; |
| 26 | |
| 27 | /* This matches what is seen on e.g. ubuntu. |
| 28 | * "which" there is a shell script. */ |
| 29 | path = getenv("PATH"); |
| 30 | if (!path) { |
| 31 | path = (char*)bb_PATH_root_path; |
| 32 | putenv(path); |
| 33 | path += 5; /* skip "PATH=" */ |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 34 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 36 | do { |
| 37 | #if ENABLE_DESKTOP |
| 38 | /* Much bloat just to support -a */ |
| 39 | if (strchr(*argv, '/')) { |
| 40 | if (execable_file(*argv)) { |
| 41 | puts(*argv); |
| 42 | continue; |
| 43 | } |
| 44 | status = EXIT_FAILURE; |
| 45 | } else { |
| 46 | char *path2 = xstrdup(path); |
| 47 | char *tmp = path2; |
Denis Vlasenko | 9d93873 | 2007-01-28 15:31:19 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 49 | p = find_execable(*argv, &tmp); |
| 50 | if (!p) |
| 51 | status = EXIT_FAILURE; |
| 52 | else { |
| 53 | print: |
| 54 | puts(p); |
| 55 | free(p); |
| 56 | if (opt) { |
| 57 | /* -a: show matches in all PATH components */ |
| 58 | if (tmp) { |
| 59 | p = find_execable(*argv, &tmp); |
| 60 | if (p) |
| 61 | goto print; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | free(path2); |
| 66 | } |
| 67 | #else |
| 68 | /* Just ignoring -a */ |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 69 | if (strchr(*argv, '/')) { |
| 70 | if (execable_file(*argv)) { |
| 71 | puts(*argv); |
| 72 | continue; |
Denis Vlasenko | 01c27fc | 2006-10-05 21:10:53 +0000 | [diff] [blame] | 73 | } |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 74 | } else { |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 75 | char *path2 = xstrdup(path); |
| 76 | char *tmp = path2; |
| 77 | p = find_execable(*argv, &tmp); |
| 78 | free(path2); |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 79 | if (p) { |
| 80 | puts(p); |
| 81 | free(p); |
| 82 | continue; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 83 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 84 | } |
Denis Vlasenko | 01c27fc | 2006-10-05 21:10:53 +0000 | [diff] [blame] | 85 | status = EXIT_FAILURE; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 86 | #endif |
| 87 | } while (*(++argv) != NULL); |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 89 | fflush_stdout_and_exit(status); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 90 | } |