blob: 3bd54ac422974339ac420e2d01c5be3a0574eee0 [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 Vlasenko4eed2c62017-07-18 22:01:24 +02009//config: bool "which (3.7 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{
Timo Teräs1e3cce62014-05-03 16:34:36 +020033 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 Andersen330fd2b2000-05-19 05:35:19 +000039
Denys Vlasenko22542ec2017-08-08 21:55:02 +020040 getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/);
Denis Vlasenkof592aa32008-06-05 13:33:59 +000041 argv += optind;
42
Denis Vlasenkof592aa32008-06-05 13:33:59 +000043 do {
Timo Teräs1e3cce62014-05-03 16:34:36 +020044 int missing = 1;
45
46 /* If file contains a slash don't use PATH */
Denis Vlasenkof592aa32008-06-05 13:33:59 +000047 if (strchr(*argv, '/')) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020048 if (file_is_executable(*argv)) {
Timo Teräs1e3cce62014-05-03 16:34:36 +020049 missing = 0;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000050 puts(*argv);
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000051 }
Eric Andersen514633b2003-10-22 10:38:22 +000052 } else {
Timo Teräs1e3cce62014-05-03 16:34:36 +020053 char *path;
54 char *tmp;
55 char *p;
56
57 path = tmp = xstrdup(env_path);
Denys Vlasenko819b47a2017-08-03 03:29:32 +020058//NOFORK FIXME: nested xmallocs (one is inside find_executable())
59//can leak memory on failure
Timo Teräs1e3cce62014-05-03 16:34:36 +020060 while ((p = find_executable(*argv, &tmp)) != NULL) {
61 missing = 0;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000062 puts(p);
63 free(p);
Timo Teräs1e3cce62014-05-03 16:34:36 +020064 if (!option_mask32) /* -a not set */
65 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +000066 }
Timo Teräs1e3cce62014-05-03 16:34:36 +020067 free(path);
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}