realpath.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* vi: set sw=4 ts=4: */
  2. /* BB_AUDIT SUSv3 N/A -- Apparently a busybox extension. */
  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 tarball for details.
  9. */
  10. #include "libbb.h"
  11. int realpath_main(int argc, char **argv);
  12. int realpath_main(int argc, char **argv)
  13. {
  14. int retval = EXIT_SUCCESS;
  15. #if PATH_MAX > (BUFSIZ+1)
  16. RESERVE_CONFIG_BUFFER(resolved_path, PATH_MAX);
  17. # define resolved_path_MUST_FREE 1
  18. #else
  19. #define resolved_path bb_common_bufsiz1
  20. # define resolved_path_MUST_FREE 0
  21. #endif
  22. if (--argc == 0) {
  23. bb_show_usage();
  24. }
  25. do {
  26. argv++;
  27. if (realpath(*argv, resolved_path) != NULL) {
  28. puts(resolved_path);
  29. } else {
  30. retval = EXIT_FAILURE;
  31. bb_perror_msg("%s", *argv);
  32. }
  33. } while (--argc);
  34. #if ENABLE_FEATURE_CLEAN_UP && resolved_path_MUST_FREE
  35. RELEASE_CONFIG_BUFFER(resolved_path);
  36. #endif
  37. fflush_stdout_and_exit(retval);
  38. }