blob: 02992d0f6235692f3121dfb9899bea6477e0bf50 [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>
Denis Vlasenkof6f43df2006-10-11 22:16:56 +00006 * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
Erik Andersen330fd2b2000-05-19 05:35:19 +00007 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Erik Andersen330fd2b2000-05-19 05:35:19 +00009 *
Glenn L McGrathe84152e2004-03-01 08:32:49 +000010 * Based on which from debianutils
Erik Andersen330fd2b2000-05-19 05:35:19 +000011 */
12
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000013#include "busybox.h"
Bernhard Reutner-Fischer66e3a222006-06-14 16:17:50 +000014
Denis Vlasenko06af2162007-02-03 17:28:39 +000015int which_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000016int which_main(int argc, char **argv)
Erik Andersen330fd2b2000-05-19 05:35:19 +000017{
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000018 int status = EXIT_SUCCESS;
19 char *p;
Erik Andersen330fd2b2000-05-19 05:35:19 +000020
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000021 if (argc <= 1 || argv[1][0] == '-') {
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 bb_show_usage();
Glenn L McGrathe84152e2004-03-01 08:32:49 +000023 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000024
Denis Vlasenko9d938732007-01-28 15:31:19 +000025 if (!getenv("PATH")) {
26 setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 1);
27 }
28
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000029 while (--argc > 0) {
Paul Foxdb485cf2005-09-14 14:08:38 +000030 argv++;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000031 if (strchr(*argv, '/')) {
32 if (execable_file(*argv)) {
33 puts(*argv);
34 continue;
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000035 }
Eric Andersen514633b2003-10-22 10:38:22 +000036 } else {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000037 p = find_execable(*argv);
38 if (p) {
39 puts(p);
40 free(p);
41 continue;
Erik Andersen330fd2b2000-05-19 05:35:19 +000042 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000043 }
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000044 status = EXIT_FAILURE;
Erik Andersen330fd2b2000-05-19 05:35:19 +000045 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000046
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000047 fflush_stdout_and_exit(status);
Erik Andersen330fd2b2000-05-19 05:35:19 +000048}