Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu> |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 9 | //usage:#define which_trivial_usage |
| 10 | //usage: "[COMMAND]..." |
| 11 | //usage:#define which_full_usage "\n\n" |
| 12 | //usage: "Locate a COMMAND" |
| 13 | //usage: |
| 14 | //usage:#define which_example_usage |
| 15 | //usage: "$ which login\n" |
| 16 | //usage: "/bin/login\n" |
| 17 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Bernhard Reutner-Fischer | 66e3a22 | 2006-06-14 16:17:50 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 20 | int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 21 | int which_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 22 | { |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 23 | const char *env_path; |
| 24 | int status = 0; |
| 25 | |
| 26 | env_path = getenv("PATH"); |
| 27 | if (!env_path) |
| 28 | env_path = bb_default_root_path; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 30 | opt_complementary = "-1"; /* at least one argument */ |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 31 | getopt32(argv, "a"); |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 32 | argv += optind; |
| 33 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 34 | do { |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 35 | int missing = 1; |
| 36 | |
| 37 | /* If file contains a slash don't use PATH */ |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 38 | if (strchr(*argv, '/')) { |
Denys Vlasenko | e765b5a | 2014-05-02 17:15:58 +0200 | [diff] [blame] | 39 | if (file_is_executable(*argv)) { |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 40 | missing = 0; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 41 | puts(*argv); |
Denis Vlasenko | 01c27fc | 2006-10-05 21:10:53 +0000 | [diff] [blame] | 42 | } |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 43 | } else { |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 44 | char *path; |
| 45 | char *tmp; |
| 46 | char *p; |
| 47 | |
| 48 | path = tmp = xstrdup(env_path); |
| 49 | while ((p = find_executable(*argv, &tmp)) != NULL) { |
| 50 | missing = 0; |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 51 | puts(p); |
| 52 | free(p); |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 53 | if (!option_mask32) /* -a not set */ |
| 54 | break; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 55 | } |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 56 | free(path); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 57 | } |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 58 | status |= missing; |
| 59 | } while (*++argv); |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 60 | |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 61 | return status; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 62 | } |