readlink.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. //config:config READLINK
  10. //config: bool "readlink (4.8 kb)"
  11. //config: default y
  12. //config: help
  13. //config: This program reads a symbolic link and returns the name
  14. //config: of the file it points to
  15. //config:
  16. //config:config FEATURE_READLINK_FOLLOW
  17. //config: bool "Enable canonicalization by following all symlinks (-f)"
  18. //config: default y
  19. //config: depends on READLINK
  20. //config: help
  21. //config: Enable the readlink option (-f).
  22. //applet:IF_READLINK(APPLET_NOFORK(readlink, readlink, BB_DIR_USR_BIN, BB_SUID_DROP, readlink))
  23. //kbuild:lib-$(CONFIG_READLINK) += readlink.o
  24. //usage:#define readlink_trivial_usage
  25. //usage: IF_FEATURE_READLINK_FOLLOW("[-fnv] ")
  26. //usage: IF_NOT_FEATURE_READLINK_FOLLOW("[-n] ")
  27. //usage: "FILE"
  28. //usage:#define readlink_full_usage "\n\n"
  29. //usage: "Display the value of a symlink" "\n"
  30. //usage: "\n -n Don't add newline"
  31. //usage: IF_FEATURE_READLINK_FOLLOW(
  32. //usage: "\n -f Canonicalize by following all symlinks"
  33. //usage: "\n -v Verbose"
  34. //usage: )
  35. #include "libbb.h"
  36. /*
  37. * # readlink --version
  38. * readlink (GNU coreutils) 6.10
  39. * # readlink --help
  40. * -f, --canonicalize
  41. * canonicalize by following every symlink in
  42. * every component of the given name recursively;
  43. * all but the last component must exist
  44. * -e, --canonicalize-existing
  45. * canonicalize by following every symlink in
  46. * every component of the given name recursively,
  47. * all components must exist
  48. * -m, --canonicalize-missing
  49. * canonicalize by following every symlink in
  50. * every component of the given name recursively,
  51. * without requirements on components existence
  52. * -n, --no-newline do not output the trailing newline
  53. * -q, --quiet, -s, --silent suppress most error messages
  54. * -v, --verbose report error messages
  55. *
  56. * bbox supports: -f (partially) -n -v (fully), -q -s (accepts but ignores)
  57. * Note: we export the -f flag, but our -f behaves like coreutils' -e.
  58. * Unfortunately, there isn't a C lib function we can leverage to get this
  59. * behavior which means we'd have to implement the full stack ourselves :(.
  60. */
  61. int readlink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  62. int readlink_main(int argc UNUSED_PARAM, char **argv)
  63. {
  64. char *buf;
  65. unsigned opt;
  66. /* -n must use bit 0 (see printf below) */
  67. opt = getopt32(argv, "^" "n" IF_FEATURE_READLINK_FOLLOW("fvsq")
  68. "\0" "=1");
  69. /* compat: coreutils readlink reports errors silently via exit code */
  70. if (!(opt & 4)) /* not -v */
  71. logmode = LOGMODE_NONE;
  72. /* NOFORK: only one alloc is allowed; must free */
  73. if (opt & 2) { /* -f */
  74. buf = xmalloc_realpath_coreutils(argv[optind]);
  75. } else {
  76. buf = xmalloc_readlink_or_warn(argv[optind]);
  77. }
  78. if (!buf)
  79. return EXIT_FAILURE;
  80. printf("%s%s", buf, &"\n"[opt & 1]);
  81. free(buf);
  82. fflush_stdout_and_exit_SUCCESS();
  83. }