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