3
0

readlink.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 "busybox.h"
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <getopt.h>
  14. int readlink_main(int argc, char **argv)
  15. {
  16. char *buf;
  17. char *fname;
  18. USE_FEATURE_READLINK_FOLLOW(
  19. unsigned opt;
  20. /* We need exactly one non-option argument. */
  21. opt_complementary = "=1";
  22. opt = getopt32(argc, argv, "f");
  23. fname = argv[optind];
  24. )
  25. SKIP_FEATURE_READLINK_FOLLOW(
  26. const unsigned opt = 0;
  27. if (argc != 2) bb_show_usage();
  28. fname = argv[1];
  29. )
  30. /* compat: coreutils readlink reports errors silently via exit code */
  31. logmode = LOGMODE_NONE;
  32. if (opt) {
  33. buf = realpath(fname, bb_common_bufsiz1);
  34. } else {
  35. buf = xreadlink(fname);
  36. }
  37. if (!buf)
  38. return EXIT_FAILURE;
  39. puts(buf);
  40. if (ENABLE_FEATURE_CLEAN_UP && buf != bb_common_bufsiz1)
  41. free(buf);
  42. fflush_stdout_and_exit(EXIT_SUCCESS);
  43. }