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 | * |
| 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 | |
| 24 | #include "internal.h" |
| 25 | #include <stdio.h> |
Pavel Roskin | c389d91 | 2000-06-05 23:41:27 +0000 | [diff] [blame] | 26 | #include <sys/stat.h> |
| 27 | #include <sys/param.h> |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 28 | |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 29 | extern int which_main(int argc, char **argv) |
| 30 | { |
Pavel Roskin | c389d91 | 2000-06-05 23:41:27 +0000 | [diff] [blame] | 31 | char *path_list, *test, *tmp, *path_parsed; |
| 32 | char buf[PATH_MAX]; |
| 33 | struct stat filestat; |
| 34 | int count = 0; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 35 | |
Matt Kraai | 3bd8bd8 | 2000-07-14 23:28:47 +0000 | [diff] [blame] | 36 | if (argc <= 1 || **(argv + 1) == '-') |
| 37 | usage(which_usage); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 38 | argc--; |
| 39 | |
| 40 | path_list = getenv("PATH"); |
| 41 | if (!path_list) |
| 42 | path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"; |
| 43 | |
Pavel Roskin | c389d91 | 2000-06-05 23:41:27 +0000 | [diff] [blame] | 44 | path_parsed = malloc (strlen(path_list) + 1); |
| 45 | strcpy (path_parsed, path_list); |
| 46 | |
| 47 | /* Replace colons with zeros in path_parsed and count them */ |
| 48 | count = 1; |
| 49 | test = path_parsed; |
| 50 | while (1) { |
| 51 | tmp = strchr(test, ':'); |
| 52 | if (tmp == NULL) |
| 53 | break; |
| 54 | *tmp = 0; |
| 55 | test = tmp + 1; |
| 56 | count++; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | while(argc-- > 0) { |
| 61 | int i; |
| 62 | int found = FALSE; |
| 63 | test = path_parsed; |
| 64 | argv++; |
| 65 | for (i = 0; i < count; i++) { |
| 66 | strcpy (buf, test); |
| 67 | strcat (buf, "/"); |
| 68 | strcat (buf, *argv); |
| 69 | if (stat (buf, &filestat) == 0 |
| 70 | && filestat.st_mode & S_IXUSR) |
| 71 | { |
| 72 | found = TRUE; |
| 73 | break; |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 74 | } |
Pavel Roskin | c389d91 | 2000-06-05 23:41:27 +0000 | [diff] [blame] | 75 | test += (strlen(test) + 1); |
| 76 | } |
| 77 | if (found == TRUE) |
| 78 | printf ("%s\n", buf); |
| 79 | else |
| 80 | { |
| 81 | printf ("which: no %s in (%s)\n", *argv, path_list); |
| 82 | exit (FALSE); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 85 | return(TRUE); |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | Local Variables: |
| 90 | c-file-style: "linux" |
| 91 | c-basic-offset: 4 |
| 92 | tab-width: 4 |
| 93 | End: |
| 94 | */ |