3
0

readlink.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //usage:#define readlink_trivial_usage
  10. //usage: IF_FEATURE_READLINK_FOLLOW("[-fnv] ") "FILE"
  11. //usage:#define readlink_full_usage "\n\n"
  12. //usage: "Display the value of a symlink"
  13. //usage: IF_FEATURE_READLINK_FOLLOW( "\n"
  14. //usage: "\n -f Canonicalize by following all symlinks"
  15. //usage: "\n -n Don't add newline"
  16. //usage: "\n -v Verbose"
  17. //usage: )
  18. #include "libbb.h"
  19. /*
  20. * # readlink --version
  21. * readlink (GNU coreutils) 6.10
  22. * # readlink --help
  23. * -f, --canonicalize
  24. * canonicalize by following every symlink in
  25. * every component of the given name recursively;
  26. * all but the last component must exist
  27. * -e, --canonicalize-existing
  28. * canonicalize by following every symlink in
  29. * every component of the given name recursively,
  30. * all components must exist
  31. * -m, --canonicalize-missing
  32. * canonicalize by following every symlink in
  33. * every component of the given name recursively,
  34. * without requirements on components existence
  35. * -n, --no-newline do not output the trailing newline
  36. * -q, --quiet, -s, --silent suppress most error messages
  37. * -v, --verbose report error messages
  38. *
  39. * bbox supports: -f (partially) -n -v (fully), -q -s (accepts but ignores)
  40. * Note: we export the -f flag, but our -f behaves like coreutils' -e.
  41. * Unfortunately, there isn't a C lib function we can leverage to get this
  42. * behavior which means we'd have to implement the full stack ourselves :(.
  43. */
  44. int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  45. int readlink_main(int argc UNUSED_PARAM, char **argv)
  46. {
  47. char *buf;
  48. char *fname;
  49. IF_FEATURE_READLINK_FOLLOW(
  50. unsigned opt;
  51. /* We need exactly one non-option argument. */
  52. opt_complementary = "=1";
  53. opt = getopt32(argv, "fnvsq");
  54. fname = argv[optind];
  55. )
  56. IF_NOT_FEATURE_READLINK_FOLLOW(
  57. const unsigned opt = 0;
  58. if (argc != 2) bb_show_usage();
  59. fname = argv[1];
  60. )
  61. /* compat: coreutils readlink reports errors silently via exit code */
  62. if (!(opt & 4)) /* not -v */
  63. logmode = LOGMODE_NONE;
  64. if (opt & 1) { /* -f */
  65. buf = xmalloc_realpath(fname);
  66. } else {
  67. buf = xmalloc_readlink_or_warn(fname);
  68. }
  69. if (!buf)
  70. return EXIT_FAILURE;
  71. printf((opt & 2) ? "%s" : "%s\n", buf);
  72. if (ENABLE_FEATURE_CLEAN_UP)
  73. free(buf);
  74. fflush_stdout_and_exit(EXIT_SUCCESS);
  75. }