2000-05-19 05:35:19 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Which implementation for busybox
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2000-05-19 05:35:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
2004-03-01 08:32:49 +00:00
|
|
|
* Based on which from debianutils
|
2000-05-19 05:35:19 +00:00
|
|
|
*/
|
|
|
|
|
2004-03-01 08:32:49 +00:00
|
|
|
|
2001-01-27 08:24:39 +00:00
|
|
|
#include <string.h>
|
2000-05-19 05:35:19 +00:00
|
|
|
#include <stdio.h>
|
2001-01-27 08:24:39 +00:00
|
|
|
#include <stdlib.h>
|
2004-03-01 08:32:49 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-02-20 06:14:08 +00:00
|
|
|
#include "busybox.h"
|
2000-05-19 05:35:19 +00:00
|
|
|
|
2003-10-22 10:38:22 +00:00
|
|
|
|
2000-05-19 05:35:19 +00:00
|
|
|
extern int which_main(int argc, char **argv)
|
|
|
|
{
|
2004-03-01 08:32:49 +00:00
|
|
|
char *path_list;
|
|
|
|
int i, count=1, status = EXIT_SUCCESS;
|
2000-05-19 05:35:19 +00:00
|
|
|
|
2004-03-01 08:32:49 +00:00
|
|
|
if (argc <= 1 || **(argv + 1) == '-') {
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_show_usage();
|
2004-03-01 08:32:49 +00:00
|
|
|
}
|
2000-05-19 05:35:19 +00:00
|
|
|
argc--;
|
|
|
|
|
|
|
|
path_list = getenv("PATH");
|
2002-01-14 18:30:10 +00:00
|
|
|
if (path_list != NULL) {
|
2004-03-01 08:32:49 +00:00
|
|
|
for (i=strlen(path_list); i > 0; i--) {
|
2002-01-14 18:30:10 +00:00
|
|
|
if (path_list[i]==':') {
|
|
|
|
path_list[i]=0;
|
|
|
|
count++;
|
|
|
|
}
|
2004-03-01 08:32:49 +00:00
|
|
|
}
|
2002-01-14 18:30:10 +00:00
|
|
|
} else {
|
|
|
|
path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin";
|
|
|
|
count = 5;
|
|
|
|
}
|
2000-06-05 23:41:27 +00:00
|
|
|
|
2004-03-15 08:29:22 +00:00
|
|
|
while (argc-- > 0) {
|
2003-10-22 11:36:55 +00:00
|
|
|
char *buf;
|
2004-03-01 08:32:49 +00:00
|
|
|
char *path_n;
|
|
|
|
char found = 0;
|
2004-03-03 21:12:16 +00:00
|
|
|
argv++;
|
2003-10-22 10:38:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if we were given the full path, first.
|
|
|
|
* Otherwise see if the file exists in our $PATH.
|
|
|
|
*/
|
2004-03-01 08:32:49 +00:00
|
|
|
path_n = path_list;
|
2003-10-22 10:38:22 +00:00
|
|
|
buf = *argv;
|
2004-03-01 08:32:49 +00:00
|
|
|
if (access(buf, X_OK) == 0) {
|
2003-10-22 10:38:22 +00:00
|
|
|
found = 1;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
buf = concat_path_file(path_n, *argv);
|
2004-03-01 08:32:49 +00:00
|
|
|
if (access(buf, X_OK) == 0) {
|
2003-10-22 10:38:22 +00:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
path_n += (strlen(path_n) + 1);
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-01 08:32:49 +00:00
|
|
|
if (found) {
|
|
|
|
puts(buf);
|
|
|
|
} else {
|
2000-11-18 01:16:43 +00:00
|
|
|
status = EXIT_FAILURE;
|
2004-03-01 08:32:49 +00:00
|
|
|
}
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|
2000-11-18 01:16:43 +00:00
|
|
|
return status;
|
2000-05-19 05:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
c-file-style: "linux"
|
|
|
|
c-basic-offset: 4
|
|
|
|
tab-width: 4
|
|
|
|
End:
|
|
|
|
*/
|