diff options
Diffstat (limited to 'busybox/debianutils/which.c')
-rw-r--r-- | busybox/debianutils/which.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/busybox/debianutils/which.c b/busybox/debianutils/which.c new file mode 100644 index 000000000..999dded36 --- /dev/null +++ b/busybox/debianutils/which.c | |||
@@ -0,0 +1,96 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Which implementation for busybox | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation; either version 2 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
20 | * | ||
21 | * Based on which from debianutils | ||
22 | */ | ||
23 | |||
24 | |||
25 | #include <string.h> | ||
26 | #include <stdio.h> | ||
27 | #include <stdlib.h> | ||
28 | #include <unistd.h> | ||
29 | |||
30 | #include "busybox.h" | ||
31 | |||
32 | |||
33 | extern int which_main(int argc, char **argv) | ||
34 | { | ||
35 | char *path_list; | ||
36 | int i, count=1, status = EXIT_SUCCESS; | ||
37 | |||
38 | if (argc <= 1 || **(argv + 1) == '-') { | ||
39 | bb_show_usage(); | ||
40 | } | ||
41 | argc--; | ||
42 | |||
43 | path_list = getenv("PATH"); | ||
44 | if (path_list != NULL) { | ||
45 | for (i=strlen(path_list); i > 0; i--) { | ||
46 | if (path_list[i]==':') { | ||
47 | path_list[i]=0; | ||
48 | count++; | ||
49 | } | ||
50 | } | ||
51 | } else { | ||
52 | path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; | ||
53 | count = 5; | ||
54 | } | ||
55 | |||
56 | while (argc-- > 0) { | ||
57 | char *buf; | ||
58 | char *path_n; | ||
59 | char found = 0; | ||
60 | argv++; | ||
61 | |||
62 | /* | ||
63 | * Check if we were given the full path, first. | ||
64 | * Otherwise see if the file exists in our $PATH. | ||
65 | */ | ||
66 | path_n = path_list; | ||
67 | buf = *argv; | ||
68 | if (access(buf, X_OK) == 0) { | ||
69 | found = 1; | ||
70 | } else { | ||
71 | for (i = 0; i < count; i++) { | ||
72 | buf = concat_path_file(path_n, *argv); | ||
73 | if (access(buf, X_OK) == 0) { | ||
74 | found = 1; | ||
75 | break; | ||
76 | } | ||
77 | free(buf); | ||
78 | path_n += (strlen(path_n) + 1); | ||
79 | } | ||
80 | } | ||
81 | if (found) { | ||
82 | puts(buf); | ||
83 | } else { | ||
84 | status = EXIT_FAILURE; | ||
85 | } | ||
86 | } | ||
87 | return status; | ||
88 | } | ||
89 | |||
90 | /* | ||
91 | Local Variables: | ||
92 | c-file-style: "linux" | ||
93 | c-basic-offset: 4 | ||
94 | tab-width: 4 | ||
95 | End: | ||
96 | */ | ||