realpath.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  4. *
  5. * Now does proper error checking on output and returns a failure exit code
  6. * if one or more paths cannot be resolved.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //config:config REALPATH
  11. //config: bool "realpath (1.6 kb)"
  12. //config: default y
  13. //config: help
  14. //config: Return the canonicalized absolute pathname.
  15. //config: This isn't provided by GNU shellutils, but where else does it belong.
  16. //applet:IF_REALPATH(APPLET_NOFORK(realpath, realpath, BB_DIR_USR_BIN, BB_SUID_DROP, realpath))
  17. //kbuild:lib-$(CONFIG_REALPATH) += realpath.o
  18. /* BB_AUDIT SUSv3 N/A -- Apparently a busybox extension. */
  19. //usage:#define realpath_trivial_usage
  20. //usage: "FILE..."
  21. //usage:#define realpath_full_usage "\n\n"
  22. //usage: "Print absolute pathnames of FILEs"
  23. #include "libbb.h"
  24. int realpath_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  25. int realpath_main(int argc UNUSED_PARAM, char **argv)
  26. {
  27. int retval = EXIT_SUCCESS;
  28. if (!*++argv) {
  29. bb_show_usage();
  30. }
  31. do {
  32. /* NOFORK: only one alloc is allowed; must free */
  33. char *resolved_path = xmalloc_realpath_coreutils(*argv);
  34. if (resolved_path != NULL) {
  35. puts(resolved_path);
  36. free(resolved_path);
  37. } else {
  38. retval = EXIT_FAILURE;
  39. bb_simple_perror_msg(*argv);
  40. }
  41. } while (*++argv);
  42. fflush_stdout_and_exit(retval);
  43. }