usage.c: remove reference to busybox.h
[oweals/busybox.git] / debianutils / which.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Which implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
7  *
8  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9  *
10  * Based on which from debianutils
11  */
12
13 #include "libbb.h"
14
15 int which_main(int argc, char **argv);
16 int which_main(int argc, char **argv)
17 {
18         int status = EXIT_SUCCESS;
19         char *p;
20
21         if (argc <= 1 || argv[1][0] == '-') {
22                 bb_show_usage();
23         }
24
25         if (!getenv("PATH")) {
26                 setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 1);
27         }
28
29         while (--argc > 0) {
30                 argv++;
31                 if (strchr(*argv, '/')) {
32                         if (execable_file(*argv)) {
33                                 puts(*argv);
34                                 continue;
35                         }
36                 } else {
37                         p = find_execable(*argv);
38                         if (p) {
39                                 puts(p);
40                                 free(p);
41                                 continue;
42                         }
43                 }
44                 status = EXIT_FAILURE;
45         }
46
47         fflush_stdout_and_exit(status);
48 }