readlink.c 903 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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, see file LICENSE in this tarball for details.
  8. */
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <getopt.h>
  13. #include "busybox.h"
  14. #define READLINK_FLAG_f (1 << 0)
  15. int readlink_main(int argc, char **argv)
  16. {
  17. char *buf;
  18. unsigned long opt = ENABLE_FEATURE_READLINK_FOLLOW ?
  19. bb_getopt_ulflags(argc, argv, "f") : 0;
  20. if (argc != (ENABLE_FEATURE_READLINK_FOLLOW ? optind + 1 : 2))
  21. bb_show_usage();
  22. if (opt & READLINK_FLAG_f)
  23. buf = realpath(argv[optind], bb_common_bufsiz1);
  24. else
  25. buf = xreadlink(argv[ENABLE_FEATURE_READLINK_FOLLOW ? optind : 1]);
  26. if (!buf)
  27. return EXIT_FAILURE;
  28. puts(buf);
  29. if (ENABLE_FEATURE_CLEAN_UP && buf != bb_common_bufsiz1)
  30. free(buf);
  31. return EXIT_SUCCESS;
  32. }