blob: 23692dc6f489fe305a8e7d3385b279239cdfb2e5 [file] [log] [blame]
Erik Andersen330fd2b2000-05-19 05:35:19 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Denis Vlasenkof6f43df2006-10-11 22:16:56 +00004 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
Erik Andersen330fd2b2000-05-19 05:35:19 +00005 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen330fd2b2000-05-19 05:35:19 +00007 */
Denys Vlasenko28826ac2015-10-19 00:52:26 +02008//config:config WHICH
Denys Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "which (3.8 kb)"
Denys Vlasenko28826ac2015-10-19 00:52:26 +020010//config: default y
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: which is used to find programs in your PATH and
13//config: print out their pathnames.
Denys Vlasenko28826ac2015-10-19 00:52:26 +020014
Denys Vlasenko819b47a2017-08-03 03:29:32 +020015//applet:IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which))
Denys Vlasenko28826ac2015-10-19 00:52:26 +020016
17//kbuild:lib-$(CONFIG_WHICH) += which.o
Erik Andersen330fd2b2000-05-19 05:35:19 +000018
Pere Orga6a3e01d2011-04-01 22:56:30 +020019//usage:#define which_trivial_usage
Denys Vlasenko15f7d612021-11-09 13:51:22 +010020//usage: "[-a] COMMAND..."
Pere Orga6a3e01d2011-04-01 22:56:30 +020021//usage:#define which_full_usage "\n\n"
Denys Vlasenko15f7d612021-11-09 13:51:22 +010022//usage: "Locate COMMAND\n"
23//usage: "\n -a Show all matches"
Pere Orga6a3e01d2011-04-01 22:56:30 +020024//usage:
25//usage:#define which_example_usage
26//usage: "$ which login\n"
27//usage: "/bin/login\n"
28
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000029#include "libbb.h"
Bernhard Reutner-Fischer66e3a222006-06-14 16:17:50 +000030
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000031int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000032int which_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen330fd2b2000-05-19 05:35:19 +000033{
Denys Vlasenkocca7c612018-01-12 13:21:33 +010034 char *env_path;
Timo Teräs1e3cce62014-05-03 16:34:36 +020035 int status = 0;
Denys Vlasenkocca7c612018-01-12 13:21:33 +010036 /* This sizeof(): bb_default_root_path is shorter than BB_PATH_ROOT_PATH */
37 char buf[sizeof(BB_PATH_ROOT_PATH)];
Timo Teräs1e3cce62014-05-03 16:34:36 +020038
39 env_path = getenv("PATH");
40 if (!env_path)
Denys Vlasenkocca7c612018-01-12 13:21:33 +010041 /* env_path must be writable, and must not alloc, so... */
42 env_path = strcpy(buf, bb_default_root_path);
Erik Andersen330fd2b2000-05-19 05:35:19 +000043
Denys Vlasenko22542ec2017-08-08 21:55:02 +020044 getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/);
Denis Vlasenkof592aa32008-06-05 13:33:59 +000045 argv += optind;
46
Denis Vlasenkof592aa32008-06-05 13:33:59 +000047 do {
Timo Teräs1e3cce62014-05-03 16:34:36 +020048 int missing = 1;
49
50 /* If file contains a slash don't use PATH */
Denis Vlasenkof592aa32008-06-05 13:33:59 +000051 if (strchr(*argv, '/')) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020052 if (file_is_executable(*argv)) {
Timo Teräs1e3cce62014-05-03 16:34:36 +020053 missing = 0;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000054 puts(*argv);
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000055 }
Eric Andersen514633b2003-10-22 10:38:22 +000056 } else {
Timo Teräs1e3cce62014-05-03 16:34:36 +020057 char *path;
Timo Teräs1e3cce62014-05-03 16:34:36 +020058 char *p;
59
Denys Vlasenkocca7c612018-01-12 13:21:33 +010060 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äs1e3cce62014-05-03 16:34:36 +020063 missing = 0;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000064 puts(p);
65 free(p);
Timo Teräs1e3cce62014-05-03 16:34:36 +020066 if (!option_mask32) /* -a not set */
67 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +000068 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000069 }
Timo Teräs1e3cce62014-05-03 16:34:36 +020070 status |= missing;
71 } while (*++argv);
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000072
Timo Teräs1e3cce62014-05-03 16:34:36 +020073 return status;
Erik Andersen330fd2b2000-05-19 05:35:19 +000074}