readlink.c 1.1 KB

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