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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 13 | //usage:#define which_trivial_usage |
| 14 | //usage: "[COMMAND]..." |
| 15 | //usage:#define which_full_usage "\n\n" |
| 16 | //usage: "Locate a COMMAND" |
| 17 | //usage: |
| 18 | //usage:#define which_example_usage |
| 19 | //usage: "$ which login\n" |
| 20 | //usage: "/bin/login\n" |
| 21 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 22 | #include "libbb.h" |
Bernhard Reutner-Fischer | 66e3a22 | 2006-06-14 16:17:50 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 24 | int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 25 | int which_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 27 | IF_DESKTOP(int opt;) |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 28 | int status = EXIT_SUCCESS; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 29 | char *path; |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 30 | char *p; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 32 | opt_complementary = "-1"; /* at least one argument */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 33 | IF_DESKTOP(opt =) getopt32(argv, "a"); |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 34 | argv += optind; |
| 35 | |
| 36 | /* This matches what is seen on e.g. ubuntu. |
| 37 | * "which" there is a shell script. */ |
| 38 | path = getenv("PATH"); |
| 39 | if (!path) { |
| 40 | path = (char*)bb_PATH_root_path; |
| 41 | putenv(path); |
| 42 | path += 5; /* skip "PATH=" */ |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 43 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 45 | do { |
| 46 | #if ENABLE_DESKTOP |
| 47 | /* Much bloat just to support -a */ |
| 48 | if (strchr(*argv, '/')) { |
| 49 | if (execable_file(*argv)) { |
| 50 | puts(*argv); |
| 51 | continue; |
| 52 | } |
| 53 | status = EXIT_FAILURE; |
| 54 | } else { |
| 55 | char *path2 = xstrdup(path); |
| 56 | char *tmp = path2; |
Denis Vlasenko | 9d93873 | 2007-01-28 15:31:19 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 58 | p = find_execable(*argv, &tmp); |
| 59 | if (!p) |
| 60 | status = EXIT_FAILURE; |
| 61 | else { |
| 62 | print: |
| 63 | puts(p); |
| 64 | free(p); |
| 65 | if (opt) { |
| 66 | /* -a: show matches in all PATH components */ |
| 67 | if (tmp) { |
| 68 | p = find_execable(*argv, &tmp); |
| 69 | if (p) |
| 70 | goto print; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | free(path2); |
| 75 | } |
| 76 | #else |
| 77 | /* Just ignoring -a */ |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 78 | if (strchr(*argv, '/')) { |
| 79 | if (execable_file(*argv)) { |
| 80 | puts(*argv); |
| 81 | continue; |
Denis Vlasenko | 01c27fc | 2006-10-05 21:10:53 +0000 | [diff] [blame] | 82 | } |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 83 | } else { |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 84 | char *path2 = xstrdup(path); |
| 85 | char *tmp = path2; |
| 86 | p = find_execable(*argv, &tmp); |
| 87 | free(path2); |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 88 | if (p) { |
| 89 | puts(p); |
| 90 | free(p); |
| 91 | continue; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 92 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 93 | } |
Denis Vlasenko | 01c27fc | 2006-10-05 21:10:53 +0000 | [diff] [blame] | 94 | status = EXIT_FAILURE; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 95 | #endif |
| 96 | } while (*(++argv) != NULL); |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 98 | fflush_stdout_and_exit(status); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 99 | } |