blob: 79b2a4f830c76cbf5a55562be0be207699dc5123 [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
Rob Landleydfba7412006-03-06 20:47:33 +000015int which_main(int argc, char **argv)
Erik Andersen330fd2b2000-05-19 05:35:19 +000016{
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000017 int status = EXIT_SUCCESS;
18 char *p;
Erik Andersen330fd2b2000-05-19 05:35:19 +000019
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000020 if (argc <= 1 || argv[1][0] == '-') {
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 bb_show_usage();
Glenn L McGrathe84152e2004-03-01 08:32:49 +000022 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000023
Denis Vlasenko9d938732007-01-28 15:31:19 +000024 if (!getenv("PATH")) {
25 setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin", 1);
26 }
27
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000028 while (--argc > 0) {
Paul Foxdb485cf2005-09-14 14:08:38 +000029 argv++;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000030 if (strchr(*argv, '/')) {
31 if (execable_file(*argv)) {
32 puts(*argv);
33 continue;
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000034 }
Eric Andersen514633b2003-10-22 10:38:22 +000035 } else {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000036 p = find_execable(*argv);
37 if (p) {
38 puts(p);
39 free(p);
40 continue;
Erik Andersen330fd2b2000-05-19 05:35:19 +000041 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000042 }
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000043 status = EXIT_FAILURE;
Erik Andersen330fd2b2000-05-19 05:35:19 +000044 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000045
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000046 fflush_stdout_and_exit(status);
Erik Andersen330fd2b2000-05-19 05:35:19 +000047}