blob: 999dded369dc05c14b69b04056d61358d054e1b7 [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 Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 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 *
Glenn L McGrathe84152e2004-03-01 08:32:49 +000021 * Based on which from debianutils
Erik Andersen330fd2b2000-05-19 05:35:19 +000022 */
23
Glenn L McGrathe84152e2004-03-01 08:32:49 +000024
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>
Glenn L McGrathe84152e2004-03-01 08:32:49 +000028#include <unistd.h>
29
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
Erik Andersen330fd2b2000-05-19 05:35:19 +000031
Eric Andersen514633b2003-10-22 10:38:22 +000032
Erik Andersen330fd2b2000-05-19 05:35:19 +000033extern int which_main(int argc, char **argv)
34{
Glenn L McGrathe84152e2004-03-01 08:32:49 +000035 char *path_list;
36 int i, count=1, status = EXIT_SUCCESS;
Erik Andersen330fd2b2000-05-19 05:35:19 +000037
Glenn L McGrathe84152e2004-03-01 08:32:49 +000038 if (argc <= 1 || **(argv + 1) == '-') {
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_show_usage();
Glenn L McGrathe84152e2004-03-01 08:32:49 +000040 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000041 argc--;
42
43 path_list = getenv("PATH");
Matt Kraaia3181dd2002-01-14 18:30:10 +000044 if (path_list != NULL) {
Glenn L McGrathe84152e2004-03-01 08:32:49 +000045 for (i=strlen(path_list); i > 0; i--) {
Matt Kraaia3181dd2002-01-14 18:30:10 +000046 if (path_list[i]==':') {
47 path_list[i]=0;
48 count++;
49 }
Glenn L McGrathe84152e2004-03-01 08:32:49 +000050 }
Matt Kraaia3181dd2002-01-14 18:30:10 +000051 } else {
52 path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin";
53 count = 5;
54 }
Pavel Roskinc389d912000-06-05 23:41:27 +000055
Eric Andersenc7bda1c2004-03-15 08:29:22 +000056 while (argc-- > 0) {
Eric Andersene78fe242003-10-22 11:36:55 +000057 char *buf;
Glenn L McGrathe84152e2004-03-01 08:32:49 +000058 char *path_n;
Glenn L McGrathe84152e2004-03-01 08:32:49 +000059 char found = 0;
Glenn L McGrathbbf2ce32004-03-03 21:12:16 +000060 argv++;
Eric Andersen514633b2003-10-22 10:38:22 +000061
62 /*
63 * Check if we were given the full path, first.
64 * Otherwise see if the file exists in our $PATH.
65 */
Glenn L McGrathe84152e2004-03-01 08:32:49 +000066 path_n = path_list;
Eric Andersen514633b2003-10-22 10:38:22 +000067 buf = *argv;
Glenn L McGrathe84152e2004-03-01 08:32:49 +000068 if (access(buf, X_OK) == 0) {
Eric Andersen514633b2003-10-22 10:38:22 +000069 found = 1;
70 } else {
71 for (i = 0; i < count; i++) {
72 buf = concat_path_file(path_n, *argv);
Glenn L McGrathe84152e2004-03-01 08:32:49 +000073 if (access(buf, X_OK) == 0) {
Eric Andersen514633b2003-10-22 10:38:22 +000074 found = 1;
75 break;
76 }
77 free(buf);
78 path_n += (strlen(path_n) + 1);
Erik Andersen330fd2b2000-05-19 05:35:19 +000079 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000080 }
Glenn L McGrathe84152e2004-03-01 08:32:49 +000081 if (found) {
82 puts(buf);
83 } else {
Matt Kraai768a2342000-11-18 01:16:43 +000084 status = EXIT_FAILURE;
Glenn L McGrathe84152e2004-03-01 08:32:49 +000085 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000086 }
Matt Kraai768a2342000-11-18 01:16:43 +000087 return status;
Erik Andersen330fd2b2000-05-19 05:35:19 +000088}
89
90/*
91Local Variables:
92c-file-style: "linux"
93c-basic-offset: 4
94tab-width: 4
95End:
96*/