1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
4 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //config: bool "which (3.7 kb)"
12 //config: which is used to find programs in your PATH and
13 //config: print out their pathnames.
15 //applet:IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which))
17 //kbuild:lib-$(CONFIG_WHICH) += which.o
19 //usage:#define which_trivial_usage
20 //usage: "[COMMAND]..."
21 //usage:#define which_full_usage "\n\n"
22 //usage: "Locate a COMMAND"
24 //usage:#define which_example_usage
25 //usage: "$ which login\n"
26 //usage: "/bin/login\n"
30 int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31 int which_main(int argc UNUSED_PARAM, char **argv)
36 env_path = getenv("PATH");
38 env_path = bb_default_root_path;
40 opt_complementary = "-1"; /* at least one argument */
47 /* If file contains a slash don't use PATH */
48 if (strchr(*argv, '/')) {
49 if (file_is_executable(*argv)) {
58 path = tmp = xstrdup(env_path);
59 //NOFORK FIXME: nested xmallocs (one is inside find_executable())
60 //can leak memory on failure
61 while ((p = find_executable(*argv, &tmp)) != NULL) {
65 if (!option_mask32) /* -a not set */