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 | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 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 | * |
| 21 | */ |
| 22 | |
Eric Andersen | 8d4c397 | 2001-03-09 21:28:09 +0000 | [diff] [blame] | 23 | /* getopt not needed */ |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 24 | #include <string.h> |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 28 | |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame^] | 29 | static 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 Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 39 | extern int which_main(int argc, char **argv) |
| 40 | { |
Glenn L McGrath | 63dde9d | 2000-09-18 09:37:40 +0000 | [diff] [blame] | 41 | char *path_list, *path_n; |
Matt Kraai | 768a234 | 2000-11-18 01:16:43 +0000 | [diff] [blame] | 42 | int i, count=1, found, status = EXIT_SUCCESS; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 43 | |
Matt Kraai | 3bd8bd8 | 2000-07-14 23:28:47 +0000 | [diff] [blame] | 44 | if (argc <= 1 || **(argv + 1) == '-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | bb_show_usage(); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 46 | argc--; |
| 47 | |
| 48 | path_list = getenv("PATH"); |
Matt Kraai | a3181dd | 2002-01-14 18:30:10 +0000 | [diff] [blame] | 49 | 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 Roskin | c389d91 | 2000-06-05 23:41:27 +0000 | [diff] [blame] | 59 | |
| 60 | while(argc-- > 0) { |
Glenn L McGrath | 63dde9d | 2000-09-18 09:37:40 +0000 | [diff] [blame] | 61 | path_n = path_list; |
Pavel Roskin | c389d91 | 2000-06-05 23:41:27 +0000 | [diff] [blame] | 62 | argv++; |
Matt Kraai | 768a234 | 2000-11-18 01:16:43 +0000 | [diff] [blame] | 63 | found = 0; |
Eric Andersen | 514633b | 2003-10-22 10:38:22 +0000 | [diff] [blame^] | 64 | 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 Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 84 | } |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 85 | } |
Matt Kraai | 768a234 | 2000-11-18 01:16:43 +0000 | [diff] [blame] | 86 | if (!found) |
| 87 | status = EXIT_FAILURE; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 88 | } |
Matt Kraai | 768a234 | 2000-11-18 01:16:43 +0000 | [diff] [blame] | 89 | return status; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
| 93 | Local Variables: |
| 94 | c-file-style: "linux" |
| 95 | c-basic-offset: 4 |
| 96 | tab-width: 4 |
| 97 | End: |
| 98 | */ |