1 /* vi: set sw=4 ts=4: */
3 * Which implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
9 * Based on which from debianutils
20 static int is_executable_file(char *a, struct stat *b)
22 return (!access(a,X_OK) && !stat(a, b) && S_ISREG(b->st_mode));
25 int which_main(int argc, char **argv)
31 if (argc <= 1 || argv[1][0] == '-') {
36 path_list = getenv("PATH");
37 if (path_list != NULL) {
40 while ((p = strchr(p, ':')) != NULL) {
45 path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin";
49 status = EXIT_SUCCESS;
57 /* If filename is either absolute or contains slashes,
59 if (strchr(buf, '/')) {
60 if (is_executable_file(buf, &stat_b)) {
65 /* File doesn't contain slashes */
67 for (i = 0; i < count; i++) {
68 /* Empty component in PATH is treated as . */
69 buf = concat_path_file(p[0] ? p : ".", argv[0]);
70 if (is_executable_file(buf, &stat_b)) {
79 status = EXIT_FAILURE;
82 bb_fflush_stdout_and_exit(status);