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
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
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;
|
2008-07-05 09:18:54 +00:00
|
|
|
int which_main(int argc UNUSED_PARAM, char **argv)
|
2000-05-19 05:35:19 +00:00
|
|
|
{
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_DESKTOP(int opt;)
|
2006-10-11 22:16:56 +00:00
|
|
|
int status = EXIT_SUCCESS;
|
2008-06-05 13:33:59 +00:00
|
|
|
char *path;
|
2006-10-11 22:16:56 +00:00
|
|
|
char *p;
|
2000-05-19 05:35:19 +00:00
|
|
|
|
2008-06-05 13:33:59 +00:00
|
|
|
opt_complementary = "-1"; /* at least one argument */
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_DESKTOP(opt =) getopt32(argv, "a");
|
2008-06-05 13:33:59 +00:00
|
|
|
argv += optind;
|
2003-10-22 10:38:22 +00:00
|
|
|
|
2008-06-05 13:33:59 +00:00
|
|
|
/* This matches what is seen on e.g. ubuntu.
|
|
|
|
* "which" there is a shell script. */
|
|
|
|
path = getenv("PATH");
|
|
|
|
if (!path) {
|
|
|
|
path = (char*)bb_PATH_root_path;
|
|
|
|
putenv(path);
|
|
|
|
path += 5; /* skip "PATH=" */
|
2007-01-28 15:31:19 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 13:33:59 +00:00
|
|
|
do {
|
|
|
|
#if ENABLE_DESKTOP
|
|
|
|
/* Much bloat just to support -a */
|
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
|
|
|
}
|
2008-06-05 13:33:59 +00:00
|
|
|
status = EXIT_FAILURE;
|
2003-10-22 10:38:22 +00:00
|
|
|
} else {
|
2008-06-05 13:33:59 +00:00
|
|
|
char *path2 = xstrdup(path);
|
|
|
|
char *tmp = path2;
|
|
|
|
|
|
|
|
p = find_execable(*argv, &tmp);
|
|
|
|
if (!p)
|
|
|
|
status = EXIT_FAILURE;
|
|
|
|
else {
|
|
|
|
print:
|
|
|
|
puts(p);
|
|
|
|
free(p);
|
|
|
|
if (opt) {
|
|
|
|
/* -a: show matches in all PATH components */
|
|
|
|
if (tmp) {
|
|
|
|
p = find_execable(*argv, &tmp);
|
|
|
|
if (p)
|
|
|
|
goto print;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(path2);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Just ignoring -a */
|
|
|
|
if (strchr(*argv, '/')) {
|
|
|
|
if (execable_file(*argv)) {
|
|
|
|
puts(*argv);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char *path2 = xstrdup(path);
|
|
|
|
char *tmp = path2;
|
|
|
|
p = find_execable(*argv, &tmp);
|
|
|
|
free(path2);
|
2006-10-11 22:16:56 +00:00
|
|
|
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;
|
2008-06-05 13:33:59 +00:00
|
|
|
#endif
|
|
|
|
} while (*(++argv) != NULL);
|
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
|
|
|
}
|