Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Which implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 6 | * |
| 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 McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 21 | * Based on which from debianutils |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 24 | |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 25 | #include <string.h> |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 30 | #include "busybox.h" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 31 | |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 32 | |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 33 | extern int which_main(int argc, char **argv) |
| 34 | { |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 35 | char *path_list; |
| 36 | int i, count=1, status = EXIT_SUCCESS; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 37 | |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 38 | if (argc <= 1 || **(argv + 1) == '-') { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | bb_show_usage(); |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 40 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 41 | argc--; |
| 42 | |
| 43 | path_list = getenv("PATH"); |
Matt Kraai | a3181dd | 2002-01-14 18:30:10 +0000 | [diff] [blame] | 44 | if (path_list != NULL) { |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 45 | for (i=strlen(path_list); i > 0; i--) { |
Matt Kraai | a3181dd | 2002-01-14 18:30:10 +0000 | [diff] [blame] | 46 | if (path_list[i]==':') { |
| 47 | path_list[i]=0; |
| 48 | count++; |
| 49 | } |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 50 | } |
Matt Kraai | a3181dd | 2002-01-14 18:30:10 +0000 | [diff] [blame] | 51 | } else { |
| 52 | path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; |
| 53 | count = 5; |
| 54 | } |
Pavel Roskin | c389d91 | 2000-06-05 23:41:27 +0000 | [diff] [blame] | 55 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame^] | 56 | while (argc-- > 0) { |
Eric Andersen | e78fe24 | 2003-10-22 11:36:55 +0000 | [diff] [blame] | 57 | char *buf; |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 58 | char *path_n; |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 59 | char found = 0; |
Glenn L McGrath | bbf2ce3 | 2004-03-03 21:12:16 +0000 | [diff] [blame] | 60 | argv++; |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Check if we were given the full path, first. |
| 64 | * Otherwise see if the file exists in our $PATH. |
| 65 | */ |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 66 | path_n = path_list; |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 67 | buf = *argv; |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 68 | if (access(buf, X_OK) == 0) { |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 69 | found = 1; |
| 70 | } else { |
| 71 | for (i = 0; i < count; i++) { |
| 72 | buf = concat_path_file(path_n, *argv); |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 73 | if (access(buf, X_OK) == 0) { |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame] | 74 | found = 1; |
| 75 | break; |
| 76 | } |
| 77 | free(buf); |
| 78 | path_n += (strlen(path_n) + 1); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 79 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 80 | } |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 81 | if (found) { |
| 82 | puts(buf); |
| 83 | } else { |
Matt Kraai | 768a234 | 2000-11-18 01:16:43 +0000 | [diff] [blame] | 84 | status = EXIT_FAILURE; |
Glenn L McGrath | e84152e | 2004-03-01 08:32:49 +0000 | [diff] [blame] | 85 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 86 | } |
Matt Kraai | 768a234 | 2000-11-18 01:16:43 +0000 | [diff] [blame] | 87 | return status; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | Local Variables: |
| 92 | c-file-style: "linux" |
| 93 | c-basic-offset: 4 |
| 94 | tab-width: 4 |
| 95 | End: |
| 96 | */ |