summaryrefslogtreecommitdiffhomepage
path: root/debianutils/which.c
blob: 8838130596d58e6e859fe01e038c6dd84d6343e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* vi: set sw=4 ts=4: */
/*
 * Which implementation for busybox
 *
 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
 *
 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
 *
 * Based on which from debianutils
 */

#include "libbb.h"

int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int which_main(int argc, char **argv)
{
	int status = EXIT_SUCCESS;
	char *p;

	if (argc <= 1 || argv[1][0] == '-') {
		bb_show_usage();
	}

/* We shouldn't do this. Ever. Not our business.
	if (!getenv("PATH")) {
		putenv((char*)bb_PATH_root_path);
	}
*/

	while (--argc > 0) {
		argv++;
		if (strchr(*argv, '/')) {
			if (execable_file(*argv)) {
				puts(*argv);
				continue;
			}
		} else {
			p = find_execable(*argv);
			if (p) {
				puts(p);
				free(p);
				continue;
			}
		}
		status = EXIT_FAILURE;
	}

	fflush_stdout_and_exit(status);
}