Fixed to pass -Wundef
[oweals/busybox.git] / which.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Which implementation for busybox
4  *
5  * Copyright (C) 2000 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include "internal.h"
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <sys/param.h>
28
29 extern int which_main(int argc, char **argv)
30 {
31         char *path_list, *test, *tmp, *path_parsed;
32         char buf[PATH_MAX];
33         struct stat filestat;
34         int count = 0;
35
36         if (argc <= 1 || **(argv + 1) == '-')
37                 usage(which_usage);
38         argc--;
39
40         path_list = getenv("PATH");
41         if (!path_list)
42                 path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
43
44         path_parsed = malloc (strlen(path_list) + 1);
45         strcpy (path_parsed, path_list);
46
47         /* Replace colons with zeros in path_parsed and count them */
48         count = 1;
49         test = path_parsed;
50         while (1) {
51                 tmp = strchr(test, ':');
52                 if (tmp == NULL)
53                         break;
54                 *tmp = 0;
55                 test = tmp + 1;
56                 count++;
57         }
58
59
60         while(argc-- > 0) { 
61                 int i;
62                 int found = FALSE;
63                 test = path_parsed;
64                 argv++;
65                 for (i = 0; i < count; i++) {
66                         strcpy (buf, test);
67                         strcat (buf, "/");
68                         strcat (buf, *argv);
69                         if (stat (buf, &filestat) == 0
70                             && filestat.st_mode & S_IXUSR)
71                         {
72                                 found = TRUE;
73                                 break;
74                         }
75                         test += (strlen(test) + 1);
76                 }
77                 if (found == TRUE)
78                         printf ("%s\n", buf);
79                 else
80                 {
81                         printf ("which: no %s in (%s)\n", *argv, path_list);
82                         exit (FALSE);
83                 }
84         }
85         return(TRUE);
86 }
87
88 /*
89 Local Variables:
90 c-file-style: "linux"
91 c-basic-offset: 4
92 tab-width: 4
93 End:
94 */