3
0

which.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  4. * Copyright (C) 2006 Gabriel Somlo <somlo at cmu.edu>
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //usage:#define which_trivial_usage
  9. //usage: "[COMMAND]..."
  10. //usage:#define which_full_usage "\n\n"
  11. //usage: "Locate a COMMAND"
  12. //usage:
  13. //usage:#define which_example_usage
  14. //usage: "$ which login\n"
  15. //usage: "/bin/login\n"
  16. #include "libbb.h"
  17. int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  18. int which_main(int argc UNUSED_PARAM, char **argv)
  19. {
  20. const char *env_path;
  21. int status = 0;
  22. env_path = getenv("PATH");
  23. if (!env_path)
  24. env_path = bb_default_root_path;
  25. opt_complementary = "-1"; /* at least one argument */
  26. getopt32(argv, "a");
  27. argv += optind;
  28. do {
  29. int missing = 1;
  30. /* If file contains a slash don't use PATH */
  31. if (strchr(*argv, '/')) {
  32. if (file_is_executable(*argv)) {
  33. missing = 0;
  34. puts(*argv);
  35. }
  36. } else {
  37. char *path;
  38. char *tmp;
  39. char *p;
  40. path = tmp = xstrdup(env_path);
  41. while ((p = find_executable(*argv, &tmp)) != NULL) {
  42. missing = 0;
  43. puts(p);
  44. free(p);
  45. if (!option_mask32) /* -a not set */
  46. break;
  47. }
  48. free(path);
  49. }
  50. status |= missing;
  51. } while (*++argv);
  52. return status;
  53. }