blob: a715a2664409cddd0bba1c8aa68af19e437c8fec [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 Vlasenkof6f43df2006-10-11 22:16:56 +000024 while (--argc > 0) {
Paul Foxdb485cf2005-09-14 14:08:38 +000025 argv++;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000026 if (strchr(*argv, '/')) {
27 if (execable_file(*argv)) {
28 puts(*argv);
29 continue;
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000030 }
Eric Andersen514633b2003-10-22 10:38:22 +000031 } else {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000032 p = find_execable(*argv);
33 if (p) {
34 puts(p);
35 free(p);
36 continue;
Erik Andersen330fd2b2000-05-19 05:35:19 +000037 }
Erik Andersen330fd2b2000-05-19 05:35:19 +000038 }
Denis Vlasenko01c27fc2006-10-05 21:10:53 +000039 status = EXIT_FAILURE;
Erik Andersen330fd2b2000-05-19 05:35:19 +000040 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +000041
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000042 fflush_stdout_and_exit(status);
Erik Andersen330fd2b2000-05-19 05:35:19 +000043}