which.c 768 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Which implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
  7. *
  8. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  9. *
  10. * Based on which from debianutils
  11. */
  12. #include "busybox.h"
  13. int which_main(int argc, char **argv)
  14. {
  15. int status = EXIT_SUCCESS;
  16. char *p;
  17. if (argc <= 1 || argv[1][0] == '-') {
  18. bb_show_usage();
  19. }
  20. while (--argc > 0) {
  21. argv++;
  22. if (strchr(*argv, '/')) {
  23. if (execable_file(*argv)) {
  24. puts(*argv);
  25. continue;
  26. }
  27. } else {
  28. p = find_execable(*argv);
  29. if (p) {
  30. puts(p);
  31. free(p);
  32. continue;
  33. }
  34. }
  35. status = EXIT_FAILURE;
  36. }
  37. fflush_stdout_and_exit(status);
  38. }