which.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 (4 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: "[-a] COMMAND..."
  18. //usage:#define which_full_usage "\n\n"
  19. //usage: "Locate COMMAND\n"
  20. //usage: "\n -a Show all matches"
  21. //usage:
  22. //usage:#define which_example_usage
  23. //usage: "$ which login\n"
  24. //usage: "/bin/login\n"
  25. #include "libbb.h"
  26. int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int which_main(int argc UNUSED_PARAM, char **argv)
  28. {
  29. const char *env_path;
  30. int status = 0;
  31. env_path = getenv("PATH");
  32. if (!env_path)
  33. env_path = bb_default_root_path;
  34. getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/);
  35. argv += optind;
  36. do {
  37. int missing = 1;
  38. /* If file contains a slash don't use PATH */
  39. if (strchr(*argv, '/')) {
  40. if (file_is_executable(*argv)) {
  41. missing = 0;
  42. puts(*argv);
  43. }
  44. } else {
  45. const char *path;
  46. char *p;
  47. path = env_path;
  48. /* NOFORK NB: xmalloc inside find_executable(), must have no allocs above! */
  49. while ((p = find_executable(*argv, &path)) != NULL) {
  50. missing = 0;
  51. puts(p);
  52. free(p);
  53. if (!option_mask32) /* -a not set */
  54. break;
  55. }
  56. }
  57. status |= missing;
  58. } while (*++argv);
  59. return status;
  60. }