blob: c0f897809825a9ad725b88e09b5b5af113e7ed42 [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
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 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
Denis Vlasenkof592aa32008-06-05 13:33:59 +000040 opt_complementary = "-1"; /* at least one argument */
Timo Teräs1e3cce62014-05-03 16:34:36 +020041 getopt32(argv, "a");
Denis Vlasenkof592aa32008-06-05 13:33:59 +000042 argv += optind;
43
Denis Vlasenkof592aa32008-06-05 13:33:59 +000044 do {
Timo Teräs1e3cce62014-05-03 16:34:36 +020045 int missing = 1;
46
47 /* If file contains a slash don't use PATH */
Denis Vlasenkof592aa32008-06-05 13:33:59 +000048 if (strchr(*argv, '/')) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020049 if (file_is_executable(*argv)) {
Timo Teräs1e3cce62014-05-03 16:34:36 +020050 missing = 0;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000051 puts(*argv);
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000052 }
Eric Andersen514633b2003-10-22 10:38:22 +000053 } else {
Timo Teräs1e3cce62014-05-03 16:34:36 +020054 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 Vlasenkof6f43df2006-10-11 22:16:56 +000061 puts(p);
62 free(p);
Timo Teräs1e3cce62014-05-03 16:34:36 +020063 if (!option_mask32) /* -a not set */
64 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +000065 }
Timo Teräs1e3cce62014-05-03 16:34:36 +020066 free(path);
Erik Andersen330fd2b2000-05-19 05:35:19 +000067 }
Timo Teräs1e3cce62014-05-03 16:34:36 +020068 status |= missing;
69 } while (*++argv);
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000070
Timo Teräs1e3cce62014-05-03 16:34:36 +020071 return status;
Erik Andersen330fd2b2000-05-19 05:35:19 +000072}