blob: 943e10a5744dba68acf23aec5111c3e6a9b45ba7 [file] [log] [blame]
Erik Andersen330fd2b2000-05-19 05:35:19 +00001/* 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>
Pavel Roskinc389d912000-06-05 23:41:27 +000026#include <sys/stat.h>
27#include <sys/param.h>
Erik Andersen330fd2b2000-05-19 05:35:19 +000028
Erik Andersen330fd2b2000-05-19 05:35:19 +000029extern int which_main(int argc, char **argv)
30{
Pavel Roskinc389d912000-06-05 23:41:27 +000031 char *path_list, *test, *tmp, *path_parsed;
32 char buf[PATH_MAX];
33 struct stat filestat;
34 int count = 0;
Erik Andersen330fd2b2000-05-19 05:35:19 +000035
Matt Kraai3bd8bd82000-07-14 23:28:47 +000036 if (argc <= 1 || **(argv + 1) == '-')
37 usage(which_usage);
Erik Andersen330fd2b2000-05-19 05:35:19 +000038 argc--;
39
40 path_list = getenv("PATH");
41 if (!path_list)
42 path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
43
Pavel Roskinc389d912000-06-05 23:41:27 +000044 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;
Erik Andersen330fd2b2000-05-19 05:35:19 +000074 }
Pavel Roskinc389d912000-06-05 23:41:27 +000075 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);
Erik Andersen330fd2b2000-05-19 05:35:19 +000083 }
84 }
Eric Andersenb6106152000-06-19 17:25:40 +000085 return(TRUE);
Erik Andersen330fd2b2000-05-19 05:35:19 +000086}
87
88/*
89Local Variables:
90c-file-style: "linux"
91c-basic-offset: 4
92tab-width: 4
93End:
94*/