readlink.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #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. IF_FEATURE_READLINK_FOLLOW(
  38. unsigned opt;
  39. /* We need exactly one non-option argument. */
  40. opt_complementary = "=1";
  41. opt = getopt32(argv, "fnvsq");
  42. fname = argv[optind];
  43. )
  44. IF_NOT_FEATURE_READLINK_FOLLOW(
  45. const unsigned opt = 0;
  46. if (argc != 2) bb_show_usage();
  47. fname = argv[1];
  48. )
  49. /* compat: coreutils readlink reports errors silently via exit code */
  50. if (!(opt & 4)) /* not -v */
  51. logmode = LOGMODE_NONE;
  52. if (opt & 1) { /* -f */
  53. buf = xmalloc_realpath(fname);
  54. } else {
  55. buf = xmalloc_readlink_or_warn(fname);
  56. }
  57. if (!buf)
  58. return EXIT_FAILURE;
  59. printf((opt & 2) ? "%s" : "%s\n", buf);
  60. if (ENABLE_FEATURE_CLEAN_UP)
  61. free(buf);
  62. fflush_stdout_and_exit(EXIT_SUCCESS);
  63. }