blob: 74b92253177b8e1e06ff45e3d0e665f8824b6968 [file] [log] [blame]
Erik Andersen330fd2b2000-05-19 05:35:19 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Which implementation for busybox
4 *
Eric Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
Erik Andersen330fd2b2000-05-19 05:35:19 +00006 *
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 */
22
Eric Andersen8d4c3972001-03-09 21:28:09 +000023/* getopt not needed */
Eric Andersened3ef502001-01-27 08:24:39 +000024#include <string.h>
Erik Andersen330fd2b2000-05-19 05:35:19 +000025#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000027#include "busybox.h"
Erik Andersen330fd2b2000-05-19 05:35:19 +000028
Eric Andersen514633b2003-10-22 10:38:22 +000029static int file_exists(char *file)
30{
31 struct stat filestat;
32
33 if (stat(file, &filestat) == 0 && filestat.st_mode & S_IXUSR)
34 return 1;
35 else
36 return 0;
37}
38
Erik Andersen330fd2b2000-05-19 05:35:19 +000039extern int which_main(int argc, char **argv)
40{
Glenn L McGrath63dde9d2000-09-18 09:37:40 +000041 char *path_list, *path_n;
Matt Kraai768a2342000-11-18 01:16:43 +000042 int i, count=1, found, status = EXIT_SUCCESS;
Erik Andersen330fd2b2000-05-19 05:35:19 +000043
Matt Kraai3bd8bd82000-07-14 23:28:47 +000044 if (argc <= 1 || **(argv + 1) == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 bb_show_usage();
Erik Andersen330fd2b2000-05-19 05:35:19 +000046 argc--;
47
48 path_list = getenv("PATH");
Matt Kraaia3181dd2002-01-14 18:30:10 +000049 if (path_list != NULL) {
50 for(i=strlen(path_list); i > 0; i--)
51 if (path_list[i]==':') {
52 path_list[i]=0;
53 count++;
54 }
55 } else {
56 path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin";
57 count = 5;
58 }
Pavel Roskinc389d912000-06-05 23:41:27 +000059
60 while(argc-- > 0) {
Glenn L McGrath63dde9d2000-09-18 09:37:40 +000061 path_n = path_list;
Pavel Roskinc389d912000-06-05 23:41:27 +000062 argv++;
Matt Kraai768a2342000-11-18 01:16:43 +000063 found = 0;
Eric Andersen514633b2003-10-22 10:38:22 +000064 char *buf;
65
66 /*
67 * Check if we were given the full path, first.
68 * Otherwise see if the file exists in our $PATH.
69 */
70 buf = *argv;
71 if (file_exists(buf)) {
72 puts(buf);
73 found = 1;
74 } else {
75 for (i = 0; i < count; i++) {
76 buf = concat_path_file(path_n, *argv);
77 if (file_exists(buf)) {
78 puts(buf);
79 found = 1;
80 break;
81 }
82 free(buf);
83 path_n += (strlen(path_n) + 1);
Erik Andersen330fd2b2000-05-19 05:35:19 +000084 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000085 }
Matt Kraai768a2342000-11-18 01:16:43 +000086 if (!found)
87 status = EXIT_FAILURE;
Erik Andersen330fd2b2000-05-19 05:35:19 +000088 }
Matt Kraai768a2342000-11-18 01:16:43 +000089 return status;
Erik Andersen330fd2b2000-05-19 05:35:19 +000090}
91
92/*
93Local Variables:
94c-file-style: "linux"
95c-basic-offset: 4
96tab-width: 4
97End:
98*/