source: MondoRescue/branches/3.3/mindi-busybox/debianutils/which.c@ 3865

Last change on this file since 3865 was 3621, checked in by Bruno Cornec, 10 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

File size: 1.6 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
[1765]4 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
[821]5 *
[2725]6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]7 */
[3621]8//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.
[821]14
[3621]15//applet:IF_WHICH(APPLET(which, BB_DIR_USR_BIN, BB_SUID_DROP))
16
17//kbuild:lib-$(CONFIG_WHICH) += which.o
18
[3232]19//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
[1765]28#include "libbb.h"
[821]29
[2725]30int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31int which_main(int argc UNUSED_PARAM, char **argv)
[821]32{
[3621]33 const char *env_path;
34 int status = 0;
[821]35
[3621]36 env_path = getenv("PATH");
37 if (!env_path)
38 env_path = bb_default_root_path;
39
[2725]40 opt_complementary = "-1"; /* at least one argument */
[3621]41 getopt32(argv, "a");
[2725]42 argv += optind;
[821]43
[3621]44 do {
45 int missing = 1;
[821]46
[3621]47 /* If file contains a slash don't use PATH */
[1765]48 if (strchr(*argv, '/')) {
[3621]49 if (file_is_executable(*argv)) {
50 missing = 0;
[1765]51 puts(*argv);
52 }
[821]53 } else {
[3621]54 char *path;
55 char *tmp;
56 char *p;
[2725]57
[3621]58 path = tmp = xstrdup(env_path);
59 while ((p = find_executable(*argv, &tmp)) != NULL) {
60 missing = 0;
[2725]61 puts(p);
62 free(p);
[3621]63 if (!option_mask32) /* -a not set */
64 break;
[2725]65 }
[3621]66 free(path);
[2725]67 }
[3621]68 status |= missing;
69 } while (*++argv);
[1765]70
[3621]71 return status;
[821]72}
Note: See TracBrowser for help on using the repository browser.