readlink.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Read the contents of a symbolic link.
  2. Copyright (C) 2003-2007, 2009-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <sys/stat.h>
  19. #if !HAVE_READLINK
  20. /* readlink() substitute for systems that don't have a readlink() function,
  21. such as DJGPP 2.03 and mingw32. */
  22. ssize_t
  23. readlink (char const *file, char *buf _GL_UNUSED,
  24. size_t bufsize _GL_UNUSED)
  25. {
  26. struct stat statbuf;
  27. /* In general we should use lstat() here, not stat(). But on platforms
  28. without symbolic links, lstat() - if it exists - would be equivalent to
  29. stat(), therefore we can use stat(). This saves us a configure check. */
  30. if (stat (file, &statbuf) >= 0)
  31. errno = EINVAL;
  32. return -1;
  33. }
  34. #else /* HAVE_READLINK */
  35. # undef readlink
  36. /* readlink() wrapper that uses correct types, for systems like cygwin
  37. 1.5.x where readlink returns int, and which rejects trailing slash,
  38. for Solaris 9. */
  39. ssize_t
  40. rpl_readlink (char const *file, char *buf, size_t bufsize)
  41. {
  42. # if READLINK_TRAILING_SLASH_BUG
  43. size_t file_len = strlen (file);
  44. if (file_len && file[file_len - 1] == '/')
  45. {
  46. /* Even if FILE without the slash is a symlink to a directory,
  47. both lstat() and stat() must resolve the trailing slash to
  48. the directory rather than the symlink. We can therefore
  49. safely use stat() to distinguish between EINVAL and
  50. ENOTDIR/ENOENT, avoiding extra overhead of rpl_lstat(). */
  51. struct stat st;
  52. if (stat (file, &st) == 0 || errno == EOVERFLOW)
  53. errno = EINVAL;
  54. return -1;
  55. }
  56. # endif /* READLINK_TRAILING_SLASH_BUG */
  57. ssize_t r = readlink (file, buf, bufsize);
  58. # if READLINK_TRUNCATE_BUG
  59. if (r < 0 && errno == ERANGE)
  60. {
  61. /* Try again with a bigger buffer. This is just for test cases;
  62. real code invariably discards short reads. */
  63. char stackbuf[4032];
  64. r = readlink (file, stackbuf, sizeof stackbuf);
  65. if (r < 0)
  66. {
  67. if (errno == ERANGE)
  68. {
  69. /* Clear the buffer, which is good enough for real code.
  70. Thankfully, no test cases try short reads of enormous
  71. symlinks and what would be the point anyway? */
  72. r = bufsize;
  73. memset (buf, 0, r);
  74. }
  75. }
  76. else
  77. {
  78. if (bufsize < r)
  79. r = bufsize;
  80. memcpy (buf, stackbuf, r);
  81. }
  82. }
  83. # endif
  84. return r;
  85. }
  86. #endif /* HAVE_READLINK */