blob: 98876521f80906730f8b2906f7689d1ba8c10774 [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
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 Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Bernhard Reutner-Fischer66e3a222006-06-14 16:17:50 +000029
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000030int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000031int which_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen330fd2b2000-05-19 05:35:19 +000032{
Denys Vlasenkocca7c612018-01-12 13:21:33 +010033 char *env_path;
Timo Teräs1e3cce62014-05-03 16:34:36 +020034 int status = 0;
Denys Vlasenkocca7c612018-01-12 13:21:33 +010035 /* This sizeof(): bb_default_root_path is shorter than BB_PATH_ROOT_PATH */
36 char buf[sizeof(BB_PATH_ROOT_PATH)];
Timo Teräs1e3cce62014-05-03 16:34:36 +020037
38 env_path = getenv("PATH");
39 if (!env_path)
Denys Vlasenkocca7c612018-01-12 13:21:33 +010040 /* env_path must be writable, and must not alloc, so... */
41 env_path = strcpy(buf, bb_default_root_path);
Erik Andersen330fd2b2000-05-19 05:35:19 +000042
Denys Vlasenko22542ec2017-08-08 21:55:02 +020043 getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/);
Denis Vlasenkof592aa32008-06-05 13:33:59 +000044 argv += optind;
45
Denis Vlasenkof592aa32008-06-05 13:33:59 +000046 do {
Timo Teräs1e3cce62014-05-03 16:34:36 +020047 int missing = 1;
48
49 /* If file contains a slash don't use PATH */
Denis Vlasenkof592aa32008-06-05 13:33:59 +000050 if (strchr(*argv, '/')) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020051 if (file_is_executable(*argv)) {
Timo Teräs1e3cce62014-05-03 16:34:36 +020052 missing = 0;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000053 puts(*argv);
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000054 }
Eric Andersen514633b2003-10-22 10:38:22 +000055 } else {
Timo Teräs1e3cce62014-05-03 16:34:36 +020056 char *path;
Timo Teräs1e3cce62014-05-03 16:34:36 +020057 char *p;
58
Denys Vlasenkocca7c612018-01-12 13:21:33 +010059 path = env_path;
60 /* NOFORK NB: xmalloc inside find_executable(), must have no allocs above! */
61 while ((p = find_executable(*argv, &path)) != NULL) {
Timo Teräs1e3cce62014-05-03 16:34:36 +020062 missing = 0;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000063 puts(p);
64 free(p);
Timo Teräs1e3cce62014-05-03 16:34:36 +020065 if (!option_mask32) /* -a not set */
66 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +000067 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000068 }
Timo Teräs1e3cce62014-05-03 16:34:36 +020069 status |= missing;
70 } while (*++argv);
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000071
Timo Teräs1e3cce62014-05-03 16:34:36 +020072 return status;
Erik Andersen330fd2b2000-05-19 05:35:19 +000073}