blob: d50e7a0d31ea60e5d17dfc1c02293645a0562659 [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 */
8
Pere Orga6a3e01d2011-04-01 22:56:30 +02009//usage:#define which_trivial_usage
10//usage: "[COMMAND]..."
11//usage:#define which_full_usage "\n\n"
12//usage: "Locate a COMMAND"
13//usage:
14//usage:#define which_example_usage
15//usage: "$ which login\n"
16//usage: "/bin/login\n"
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Bernhard Reutner-Fischer66e3a222006-06-14 16:17:50 +000019
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000020int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000021int which_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen330fd2b2000-05-19 05:35:19 +000022{
Timo Teräs1e3cce62014-05-03 16:34:36 +020023 const char *env_path;
24 int status = 0;
25
26 env_path = getenv("PATH");
27 if (!env_path)
28 env_path = bb_default_root_path;
Erik Andersen330fd2b2000-05-19 05:35:19 +000029
Denis Vlasenkof592aa32008-06-05 13:33:59 +000030 opt_complementary = "-1"; /* at least one argument */
Timo Teräs1e3cce62014-05-03 16:34:36 +020031 getopt32(argv, "a");
Denis Vlasenkof592aa32008-06-05 13:33:59 +000032 argv += optind;
33
Denis Vlasenkof592aa32008-06-05 13:33:59 +000034 do {
Timo Teräs1e3cce62014-05-03 16:34:36 +020035 int missing = 1;
36
37 /* If file contains a slash don't use PATH */
Denis Vlasenkof592aa32008-06-05 13:33:59 +000038 if (strchr(*argv, '/')) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +020039 if (file_is_executable(*argv)) {
Timo Teräs1e3cce62014-05-03 16:34:36 +020040 missing = 0;
Denis Vlasenkof592aa32008-06-05 13:33:59 +000041 puts(*argv);
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000042 }
Eric Andersen514633b2003-10-22 10:38:22 +000043 } else {
Timo Teräs1e3cce62014-05-03 16:34:36 +020044 char *path;
45 char *tmp;
46 char *p;
47
48 path = tmp = xstrdup(env_path);
49 while ((p = find_executable(*argv, &tmp)) != NULL) {
50 missing = 0;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000051 puts(p);
52 free(p);
Timo Teräs1e3cce62014-05-03 16:34:36 +020053 if (!option_mask32) /* -a not set */
54 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +000055 }
Timo Teräs1e3cce62014-05-03 16:34:36 +020056 free(path);
Erik Andersen330fd2b2000-05-19 05:35:19 +000057 }
Timo Teräs1e3cce62014-05-03 16:34:36 +020058 status |= missing;
59 } while (*++argv);
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000060
Timo Teräs1e3cce62014-05-03 16:34:36 +020061 return status;
Erik Andersen330fd2b2000-05-19 05:35:19 +000062}