busybox/debianutils/which.c

51 lines
977 B
C
Raw Normal View History

/* 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"
2006-06-14 21:47:50 +05:30
int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int which_main(int argc, char **argv)
{
int status = EXIT_SUCCESS;
char *p;
2006-10-06 02:40:53 +05:30
if (argc <= 1 || argv[1][0] == '-') {
2003-03-19 14:43:01 +05:30
bb_show_usage();
}
/* This matches what is seen on e.g. ubuntu
* "which" there is a shell script */
2007-01-28 21:01:19 +05:30
if (!getenv("PATH")) {
putenv((char*)bb_PATH_root_path);
2007-01-28 21:01:19 +05:30
}
while (--argc > 0) {
argv++;
if (strchr(*argv, '/')) {
if (execable_file(*argv)) {
puts(*argv);
continue;
2006-10-06 02:40:53 +05:30
}
} else {
p = find_execable(*argv);
if (p) {
puts(p);
free(p);
continue;
}
}
2006-10-06 02:40:53 +05:30
status = EXIT_FAILURE;
}
2006-10-27 04:51:47 +05:30
fflush_stdout_and_exit(status);
}