which.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. //config:config WHICH
  9. //config: bool "which (3.7 kb)"
  10. //config: default y
  11. //config: help
  12. //config: which is used to find programs in your PATH and
  13. //config: print out their pathnames.
  14. //applet:IF_WHICH(APPLET_NOFORK(which, which, BB_DIR_USR_BIN, BB_SUID_DROP, which))
  15. //kbuild:lib-$(CONFIG_WHICH) += which.o
  16. //usage:#define which_trivial_usage
  17. //usage: "[COMMAND]..."
  18. //usage:#define which_full_usage "\n\n"
  19. //usage: "Locate a COMMAND"
  20. //usage:
  21. //usage:#define which_example_usage
  22. //usage: "$ which login\n"
  23. //usage: "/bin/login\n"
  24. #include "libbb.h"
  25. int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  26. int which_main(int argc UNUSED_PARAM, char **argv)
  27. {
  28. const char *env_path;
  29. int status = 0;
  30. env_path = getenv("PATH");
  31. if (!env_path)
  32. env_path = bb_default_root_path;
  33. getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/);
  34. argv += optind;
  35. do {
  36. int missing = 1;
  37. /* If file contains a slash don't use PATH */
  38. if (strchr(*argv, '/')) {
  39. if (file_is_executable(*argv)) {
  40. missing = 0;
  41. puts(*argv);
  42. }
  43. } else {
  44. char *path;
  45. char *tmp;
  46. char *p;
  47. path = tmp = xstrdup(env_path);
  48. //NOFORK FIXME: nested xmallocs (one is inside find_executable())
  49. //can leak memory on failure
  50. while ((p = find_executable(*argv, &tmp)) != NULL) {
  51. missing = 0;
  52. puts(p);
  53. free(p);
  54. if (!option_mask32) /* -a not set */
  55. break;
  56. }
  57. free(path);
  58. }
  59. status |= missing;
  60. } while (*++argv);
  61. return status;
  62. }