realpath.c 1.3 KB

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