readlink.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /*
  11. * # readlink --version
  12. * readlink (GNU coreutils) 6.10
  13. * # readlink --help
  14. * -f, --canonicalize
  15. * canonicalize by following every symlink in
  16. * every component of the given name recursively;
  17. * all but the last component must exist
  18. * -e, --canonicalize-existing
  19. * canonicalize by following every symlink in
  20. * every component of the given name recursively,
  21. * all components must exist
  22. * -m, --canonicalize-missing
  23. * canonicalize by following every symlink in
  24. * every component of the given name recursively,
  25. * without requirements on components existence
  26. * -n, --no-newline do not output the trailing newline
  27. * -q, --quiet, -s, --silent suppress most error messages
  28. * -v, --verbose report error messages
  29. *
  30. * bbox supports: -f -n -v (fully), -q -s (accepts but ignores)
  31. */
  32. int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  33. int readlink_main(int argc UNUSED_PARAM, char **argv)
  34. {
  35. char *buf;
  36. char *fname;
  37. char pathbuf[PATH_MAX];
  38. IF_FEATURE_READLINK_FOLLOW(
  39. unsigned opt;
  40. /* We need exactly one non-option argument. */
  41. opt_complementary = "=1";
  42. opt = getopt32(argv, "fnvsq");
  43. fname = argv[optind];
  44. )
  45. IF_NOT_FEATURE_READLINK_FOLLOW(
  46. const unsigned opt = 0;
  47. if (argc != 2) bb_show_usage();
  48. fname = argv[1];
  49. )
  50. /* compat: coreutils readlink reports errors silently via exit code */
  51. if (!(opt & 4)) /* not -v */
  52. logmode = LOGMODE_NONE;
  53. if (opt & 1) { /* -f */
  54. buf = realpath(fname, pathbuf);
  55. } else {
  56. buf = xmalloc_readlink_or_warn(fname);
  57. }
  58. if (!buf)
  59. return EXIT_FAILURE;
  60. printf((opt & 2) ? "%s" : "%s\n", buf);
  61. if (ENABLE_FEATURE_CLEAN_UP && !opt)
  62. free(buf);
  63. fflush_stdout_and_exit(EXIT_SUCCESS);
  64. }