2000-05-19 11:05:19 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Which implementation for busybox
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2006-10-12 03:46:56 +05:30
|
|
|
* Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
|
2000-05-19 11:05:19 +05:30
|
|
|
*
|
2006-09-13 22:09:19 +05:30
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2000-05-19 11:05:19 +05:30
|
|
|
*
|
2004-03-01 14:02:49 +05:30
|
|
|
* Based on which from debianutils
|
2000-05-19 11:05:19 +05:30
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2006-06-14 21:47:50 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int which_main(int argc UNUSED_PARAM, char **argv)
|
2000-05-19 11:05:19 +05:30
|
|
|
{
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_DESKTOP(int opt;)
|
2006-10-12 03:46:56 +05:30
|
|
|
int status = EXIT_SUCCESS;
|
2008-06-05 19:03:59 +05:30
|
|
|
char *path;
|
2006-10-12 03:46:56 +05:30
|
|
|
char *p;
|
2000-05-19 11:05:19 +05:30
|
|
|
|
2008-06-05 19:03:59 +05:30
|
|
|
opt_complementary = "-1"; /* at least one argument */
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_DESKTOP(opt =) getopt32(argv, "a");
|
2008-06-05 19:03:59 +05:30
|
|
|
argv += optind;
|
2003-10-22 16:08:22 +05:30
|
|
|
|
2008-06-05 19:03:59 +05:30
|
|
|
/* 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 21:01:19 +05:30
|
|
|
}
|
|
|
|
|
2008-06-05 19:03:59 +05:30
|
|
|
do {
|
|
|
|
#if ENABLE_DESKTOP
|
|
|
|
/* Much bloat just to support -a */
|
2006-10-12 03:46:56 +05:30
|
|
|
if (strchr(*argv, '/')) {
|
|
|
|
if (execable_file(*argv)) {
|
|
|
|
puts(*argv);
|
|
|
|
continue;
|
2006-10-06 02:40:53 +05:30
|
|
|
}
|
2008-06-05 19:03:59 +05:30
|
|
|
status = EXIT_FAILURE;
|
2003-10-22 16:08:22 +05:30
|
|
|
} else {
|
2008-06-05 19:03:59 +05:30
|
|
|
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-12 03:46:56 +05:30
|
|
|
if (p) {
|
|
|
|
puts(p);
|
|
|
|
free(p);
|
|
|
|
continue;
|
2000-05-19 11:05:19 +05:30
|
|
|
}
|
|
|
|
}
|
2006-10-06 02:40:53 +05:30
|
|
|
status = EXIT_FAILURE;
|
2008-06-05 19:03:59 +05:30
|
|
|
#endif
|
|
|
|
} while (*(++argv) != NULL);
|
2006-10-12 03:46:56 +05:30
|
|
|
|
2006-10-27 04:51:47 +05:30
|
|
|
fflush_stdout_and_exit(status);
|
2000-05-19 11:05:19 +05:30
|
|
|
}
|