readlink.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini readlink implementation for busybox
  4. *
  5. * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. *
  7. * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  11. int readlink_main(int argc ATTRIBUTE_UNUSED, char **argv)
  12. {
  13. char *buf;
  14. char *fname;
  15. char pathbuf[PATH_MAX];
  16. USE_FEATURE_READLINK_FOLLOW(
  17. unsigned opt;
  18. /* We need exactly one non-option argument. */
  19. opt_complementary = "=1";
  20. opt = getopt32(argv, "f");
  21. fname = argv[optind];
  22. )
  23. SKIP_FEATURE_READLINK_FOLLOW(
  24. const unsigned opt = 0;
  25. if (argc != 2) bb_show_usage();
  26. fname = argv[1];
  27. )
  28. /* compat: coreutils readlink reports errors silently via exit code */
  29. logmode = LOGMODE_NONE;
  30. if (opt) {
  31. buf = realpath(fname, pathbuf);
  32. } else {
  33. buf = xmalloc_readlink_or_warn(fname);
  34. }
  35. if (!buf)
  36. return EXIT_FAILURE;
  37. puts(buf);
  38. if (ENABLE_FEATURE_CLEAN_UP && !opt)
  39. free(buf);
  40. fflush_stdout_and_exit(EXIT_SUCCESS);
  41. }