Fix calls to {m,c,re}alloc so that they use x{m,c,re}alloc instead of
[oweals/busybox.git] / findutils / 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/param.h>
27
28 extern int which_main(int argc, char **argv)
29 {
30         char *path_list, *test, *tmp, *path_parsed;
31         char buf[PATH_MAX];
32         struct stat filestat;
33         int count = 0;
34
35         if (argc <= 1 || **(argv + 1) == '-')
36                 usage(which_usage);
37         argc--;
38
39         path_list = getenv("PATH");
40         if (!path_list)
41                 path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
42
43         path_parsed = xmalloc (strlen(path_list) + 1);
44         strcpy (path_parsed, path_list);
45
46         /* Replace colons with zeros in path_parsed and count them */
47         count = 1;
48         test = path_parsed;
49         while (1) {
50                 tmp = strchr(test, ':');
51                 if (tmp == NULL)
52                         break;
53                 *tmp = 0;
54                 test = tmp + 1;
55                 count++;
56         }
57
58
59         while(argc-- > 0) { 
60                 int i;
61                 int found = FALSE;
62                 test = path_parsed;
63                 argv++;
64                 for (i = 0; i < count; i++) {
65                         strcpy (buf, test);
66                         strcat (buf, "/");
67                         strcat (buf, *argv);
68                         if (stat (buf, &filestat) == 0
69                             && filestat.st_mode & S_IXUSR)
70                         {
71                                 found = TRUE;
72                                 break;
73                         }
74                         test += (strlen(test) + 1);
75                 }
76                 if (found == TRUE)
77                         printf ("%s\n", buf);
78                 else
79                 {
80                         printf ("which: no %s in (%s)\n", *argv, path_list);
81                         exit (FALSE);
82                 }
83         }
84         return(TRUE);
85 }
86
87 /*
88 Local Variables:
89 c-file-style: "linux"
90 c-basic-offset: 4
91 tab-width: 4
92 End:
93 */