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 | */ |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame^] | 8 | //config:config WHICH |
| 9 | //config: bool "which" |
| 10 | //config: default y |
| 11 | //config: help |
| 12 | //config: which is used to find programs in your PATH and |
| 13 | //config: print out their pathnames. |
| 14 | |
| 15 | //applet:IF_WHICH(APPLET(which, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 16 | |
| 17 | //kbuild:lib-$(CONFIG_WHICH) += which.o |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 18 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 19 | //usage:#define which_trivial_usage |
| 20 | //usage: "[COMMAND]..." |
| 21 | //usage:#define which_full_usage "\n\n" |
| 22 | //usage: "Locate a COMMAND" |
| 23 | //usage: |
| 24 | //usage:#define which_example_usage |
| 25 | //usage: "$ which login\n" |
| 26 | //usage: "/bin/login\n" |
| 27 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 28 | #include "libbb.h" |
Bernhard Reutner-Fischer | 66e3a22 | 2006-06-14 16:17:50 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 30 | int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 31 | int which_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 32 | { |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 33 | const char *env_path; |
| 34 | int status = 0; |
| 35 | |
| 36 | env_path = getenv("PATH"); |
| 37 | if (!env_path) |
| 38 | env_path = bb_default_root_path; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 40 | opt_complementary = "-1"; /* at least one argument */ |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 41 | getopt32(argv, "a"); |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 42 | argv += optind; |
| 43 | |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 44 | do { |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 45 | int missing = 1; |
| 46 | |
| 47 | /* If file contains a slash don't use PATH */ |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 48 | if (strchr(*argv, '/')) { |
Denys Vlasenko | e765b5a | 2014-05-02 17:15:58 +0200 | [diff] [blame] | 49 | if (file_is_executable(*argv)) { |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 50 | missing = 0; |
Denis Vlasenko | f592aa3 | 2008-06-05 13:33:59 +0000 | [diff] [blame] | 51 | puts(*argv); |
Denis Vlasenko | 01c27fc | 2006-10-05 21:10:53 +0000 | [diff] [blame] | 52 | } |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 53 | } else { |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 54 | char *path; |
| 55 | char *tmp; |
| 56 | char *p; |
| 57 | |
| 58 | path = tmp = xstrdup(env_path); |
| 59 | while ((p = find_executable(*argv, &tmp)) != NULL) { |
| 60 | missing = 0; |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 61 | puts(p); |
| 62 | free(p); |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 63 | if (!option_mask32) /* -a not set */ |
| 64 | break; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 65 | } |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 66 | free(path); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 67 | } |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 68 | status |= missing; |
| 69 | } while (*++argv); |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 70 | |
Timo Teräs | 1e3cce6 | 2014-05-03 16:34:36 +0200 | [diff] [blame] | 71 | return status; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 72 | } |