2000-05-19 05:35:19 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Which implementation for busybox
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2006-10-11 22:16:56 +00:00
|
|
|
* Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
|
2000-05-19 05:35:19 +00:00
|
|
|
*
|
2006-09-13 16:39:19 +00:00
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2000-05-19 05:35:19 +00:00
|
|
|
*
|
2004-03-01 08:32:49 +00:00
|
|
|
* Based on which from debianutils
|
2000-05-19 05:35:19 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2006-06-14 16:17:50 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2006-03-06 20:47:33 +00:00
|
|
|
int which_main(int argc, char **argv)
|
2000-05-19 05:35:19 +00:00
|
|
|
{
|
2006-10-11 22:16:56 +00:00
|
|
|
int status = EXIT_SUCCESS;
|
|
|
|
char *p;
|
2000-05-19 05:35:19 +00:00
|
|
|
|
2006-10-05 21:10:53 +00:00
|
|
|
if (argc <= 1 || argv[1][0] == '-') {
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_show_usage();
|
2004-03-01 08:32:49 +00:00
|
|
|
}
|
2003-10-22 10:38:22 +00:00
|
|
|
|
2007-11-25 04:54:13 +00:00
|
|
|
/* This matches what is seen on e.g. ubuntu
|
|
|
|
* "which" there is a shell script */
|
2007-01-28 15:31:19 +00:00
|
|
|
if (!getenv("PATH")) {
|
2007-06-12 22:35:19 +00:00
|
|
|
putenv((char*)bb_PATH_root_path);
|
2007-01-28 15:31:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-11 22:16:56 +00:00
|
|
|
while (--argc > 0) {
|
2005-09-14 14:08:38 +00:00
|
|
|
argv++;
|
2006-10-11 22:16:56 +00:00
|
|
|
if (strchr(*argv, '/')) {
|
|
|
|
if (execable_file(*argv)) {
|
|
|
|
puts(*argv);
|
|
|
|
continue;
|
2006-10-05 21:10:53 +00:00
|
|
|
}
|
2003-10-22 10:38:22 +00:00
|
|
|
} else {
|
2006-10-11 22:16:56 +00:00
|
|
|
p = find_execable(*argv);
|
|
|
|
if (p) {
|
|
|
|
puts(p);
|
|
|
|
free(p);
|
|
|
|
continue;
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-05 21:10:53 +00:00
|
|
|
status = EXIT_FAILURE;
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|
2006-10-11 22:16:56 +00:00
|
|
|
|
2006-10-26 23:21:47 +00:00
|
|
|
fflush_stdout_and_exit(status);
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|