2000-05-19 05:35:19 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2006-10-11 22:16:56 +00:00
|
|
|
* Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
|
2000-05-19 05:35:19 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2000-05-19 05:35:19 +00:00
|
|
|
*/
|
2015-10-18 22:52:26 +00:00
|
|
|
//config:config WHICH
|
|
|
|
//config: bool "which"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: which is used to find programs in your PATH and
|
|
|
|
//config: print out their pathnames.
|
|
|
|
|
|
|
|
//applet:IF_WHICH(APPLET(which, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_WHICH) += which.o
|
2000-05-19 05:35:19 +00:00
|
|
|
|
2011-04-01 20:56:30 +00:00
|
|
|
//usage:#define which_trivial_usage
|
|
|
|
//usage: "[COMMAND]..."
|
|
|
|
//usage:#define which_full_usage "\n\n"
|
|
|
|
//usage: "Locate a COMMAND"
|
|
|
|
//usage:
|
|
|
|
//usage:#define which_example_usage
|
|
|
|
//usage: "$ which login\n"
|
|
|
|
//usage: "/bin/login\n"
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2006-06-14 16:17:50 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int which_main(int argc UNUSED_PARAM, char **argv)
|
2000-05-19 05:35:19 +00:00
|
|
|
{
|
2014-05-03 14:34:36 +00:00
|
|
|
const char *env_path;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
env_path = getenv("PATH");
|
|
|
|
if (!env_path)
|
|
|
|
env_path = bb_default_root_path;
|
2000-05-19 05:35:19 +00:00
|
|
|
|
2008-06-05 13:33:59 +00:00
|
|
|
opt_complementary = "-1"; /* at least one argument */
|
2014-05-03 14:34:36 +00:00
|
|
|
getopt32(argv, "a");
|
2008-06-05 13:33:59 +00:00
|
|
|
argv += optind;
|
2003-10-22 10:38:22 +00:00
|
|
|
|
2008-06-05 13:33:59 +00:00
|
|
|
do {
|
2014-05-03 14:34:36 +00:00
|
|
|
int missing = 1;
|
2008-06-05 13:33:59 +00:00
|
|
|
|
2014-05-03 14:34:36 +00:00
|
|
|
/* If file contains a slash don't use PATH */
|
2008-06-05 13:33:59 +00:00
|
|
|
if (strchr(*argv, '/')) {
|
2014-05-02 15:15:58 +00:00
|
|
|
if (file_is_executable(*argv)) {
|
2014-05-03 14:34:36 +00:00
|
|
|
missing = 0;
|
2008-06-05 13:33:59 +00:00
|
|
|
puts(*argv);
|
|
|
|
}
|
|
|
|
} else {
|
2014-05-03 14:34:36 +00:00
|
|
|
char *path;
|
|
|
|
char *tmp;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
path = tmp = xstrdup(env_path);
|
|
|
|
while ((p = find_executable(*argv, &tmp)) != NULL) {
|
|
|
|
missing = 0;
|
2006-10-11 22:16:56 +00:00
|
|
|
puts(p);
|
|
|
|
free(p);
|
2014-05-03 14:34:36 +00:00
|
|
|
if (!option_mask32) /* -a not set */
|
|
|
|
break;
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|
2014-05-03 14:34:36 +00:00
|
|
|
free(path);
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|
2014-05-03 14:34:36 +00:00
|
|
|
status |= missing;
|
|
|
|
} while (*++argv);
|
2006-10-11 22:16:56 +00:00
|
|
|
|
2014-05-03 14:34:36 +00:00
|
|
|
return status;
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|