2000-05-19 11:05:19 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2006-10-12 03:46:56 +05:30
|
|
|
* Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
|
2000-05-19 11:05:19 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2000-05-19 11:05:19 +05:30
|
|
|
*/
|
2015-10-19 04:22:26 +05:30
|
|
|
//config:config WHICH
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "which (3.7 kb)"
|
2015-10-19 04:22:26 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: which is used to find programs in your PATH and
|
|
|
|
//config: print out their pathnames.
|
2015-10-19 04:22:26 +05:30
|
|
|
|
2017-08-03 06:59:32 +05:30
|
|
|
//applet:IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which))
|
2015-10-19 04:22:26 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_WHICH) += which.o
|
2000-05-19 11:05:19 +05:30
|
|
|
|
2011-04-02 02:26:30 +05:30
|
|
|
//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-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-06-14 21:47:50 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int which_main(int argc UNUSED_PARAM, char **argv)
|
2000-05-19 11:05:19 +05:30
|
|
|
{
|
2014-05-03 20:04:36 +05:30
|
|
|
const char *env_path;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
env_path = getenv("PATH");
|
|
|
|
if (!env_path)
|
|
|
|
env_path = bb_default_root_path;
|
2000-05-19 11:05:19 +05:30
|
|
|
|
2017-08-09 01:25:02 +05:30
|
|
|
getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/);
|
2008-06-05 19:03:59 +05:30
|
|
|
argv += optind;
|
2003-10-22 16:08:22 +05:30
|
|
|
|
2008-06-05 19:03:59 +05:30
|
|
|
do {
|
2014-05-03 20:04:36 +05:30
|
|
|
int missing = 1;
|
2008-06-05 19:03:59 +05:30
|
|
|
|
2014-05-03 20:04:36 +05:30
|
|
|
/* If file contains a slash don't use PATH */
|
2008-06-05 19:03:59 +05:30
|
|
|
if (strchr(*argv, '/')) {
|
2014-05-02 20:45:58 +05:30
|
|
|
if (file_is_executable(*argv)) {
|
2014-05-03 20:04:36 +05:30
|
|
|
missing = 0;
|
2008-06-05 19:03:59 +05:30
|
|
|
puts(*argv);
|
|
|
|
}
|
|
|
|
} else {
|
2014-05-03 20:04:36 +05:30
|
|
|
char *path;
|
|
|
|
char *tmp;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
path = tmp = xstrdup(env_path);
|
2017-08-03 06:59:32 +05:30
|
|
|
//NOFORK FIXME: nested xmallocs (one is inside find_executable())
|
|
|
|
//can leak memory on failure
|
2014-05-03 20:04:36 +05:30
|
|
|
while ((p = find_executable(*argv, &tmp)) != NULL) {
|
|
|
|
missing = 0;
|
2006-10-12 03:46:56 +05:30
|
|
|
puts(p);
|
|
|
|
free(p);
|
2014-05-03 20:04:36 +05:30
|
|
|
if (!option_mask32) /* -a not set */
|
|
|
|
break;
|
2000-05-19 11:05:19 +05:30
|
|
|
}
|
2014-05-03 20:04:36 +05:30
|
|
|
free(path);
|
2000-05-19 11:05:19 +05:30
|
|
|
}
|
2014-05-03 20:04:36 +05:30
|
|
|
status |= missing;
|
|
|
|
} while (*++argv);
|
2006-10-12 03:46:56 +05:30
|
|
|
|
2014-05-03 20:04:36 +05:30
|
|
|
return status;
|
2000-05-19 11:05:19 +05:30
|
|
|
}
|