blob: b900a0b143657d94343c54e4884582aa39577810 [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
Eric Andersen3570a342000-09-25 21:45:58 +000024#include "busybox.h"
Eric Andersened3ef502001-01-27 08:24:39 +000025#include <string.h>
Erik Andersen330fd2b2000-05-19 05:35:19 +000026#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000027#include <stdlib.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{
Glenn L McGrath63dde9d2000-09-18 09:37:40 +000031 char *path_list, *path_n;
Pavel Roskinc389d912000-06-05 23:41:27 +000032 struct stat filestat;
Matt Kraai768a2342000-11-18 01:16:43 +000033 int i, count=1, found, status = EXIT_SUCCESS;
Erik Andersen330fd2b2000-05-19 05:35:19 +000034
Matt Kraai3bd8bd82000-07-14 23:28:47 +000035 if (argc <= 1 || **(argv + 1) == '-')
36 usage(which_usage);
Erik Andersen330fd2b2000-05-19 05:35:19 +000037 argc--;
38
39 path_list = getenv("PATH");
40 if (!path_list)
41 path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
42
Pavel Roskinc389d912000-06-05 23:41:27 +000043 /* Replace colons with zeros in path_parsed and count them */
Glenn L McGrath63dde9d2000-09-18 09:37:40 +000044 for(i=strlen(path_list); i > 0; i--)
45 if (path_list[i]==':') {
46 path_list[i]=0;
47 count++;
48 }
Pavel Roskinc389d912000-06-05 23:41:27 +000049
50 while(argc-- > 0) {
Glenn L McGrath63dde9d2000-09-18 09:37:40 +000051 path_n = path_list;
Pavel Roskinc389d912000-06-05 23:41:27 +000052 argv++;
Matt Kraai768a2342000-11-18 01:16:43 +000053 found = 0;
Pavel Roskinc389d912000-06-05 23:41:27 +000054 for (i = 0; i < count; i++) {
Glenn L McGrath63dde9d2000-09-18 09:37:40 +000055 char buf[strlen(path_n)+1+strlen(*argv)];
56 strcpy (buf, path_n);
Pavel Roskinc389d912000-06-05 23:41:27 +000057 strcat (buf, "/");
58 strcat (buf, *argv);
59 if (stat (buf, &filestat) == 0
60 && filestat.st_mode & S_IXUSR)
61 {
Glenn L McGrath63dde9d2000-09-18 09:37:40 +000062 printf ("%s\n", buf);
Matt Kraai768a2342000-11-18 01:16:43 +000063 found = 1;
Pavel Roskinc389d912000-06-05 23:41:27 +000064 break;
Erik Andersen330fd2b2000-05-19 05:35:19 +000065 }
Glenn L McGrath63dde9d2000-09-18 09:37:40 +000066 path_n += (strlen(path_n) + 1);
Erik Andersen330fd2b2000-05-19 05:35:19 +000067 }
Matt Kraai768a2342000-11-18 01:16:43 +000068 if (!found)
69 status = EXIT_FAILURE;
Erik Andersen330fd2b2000-05-19 05:35:19 +000070 }
Matt Kraai768a2342000-11-18 01:16:43 +000071 return status;
Erik Andersen330fd2b2000-05-19 05:35:19 +000072}
73
74/*
75Local Variables:
76c-file-style: "linux"
77c-basic-offset: 4
78tab-width: 4
79End:
80*/