which.c 945 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 "libbb.h"
  13. int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  14. int which_main(int argc, char **argv)
  15. {
  16. int status = EXIT_SUCCESS;
  17. char *p;
  18. if (argc <= 1 || argv[1][0] == '-') {
  19. bb_show_usage();
  20. }
  21. /* We shouldn't do this. Ever. Not our business.
  22. if (!getenv("PATH")) {
  23. putenv((char*)bb_PATH_root_path);
  24. }
  25. */
  26. while (--argc > 0) {
  27. argv++;
  28. if (strchr(*argv, '/')) {
  29. if (execable_file(*argv)) {
  30. puts(*argv);
  31. continue;
  32. }
  33. } else {
  34. p = find_execable(*argv);
  35. if (p) {
  36. puts(p);
  37. free(p);
  38. continue;
  39. }
  40. }
  41. status = EXIT_FAILURE;
  42. }
  43. fflush_stdout_and_exit(status);
  44. }