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