readlinkat.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Read a symlink relative to an open directory.
  2. Copyright (C) 2009-2015 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 <http://www.gnu.org/licenses/>. */
  13. /* written by Eric Blake */
  14. #include <config.h>
  15. #include <unistd.h>
  16. #if HAVE_READLINKAT
  17. # undef readlinkat
  18. ssize_t
  19. rpl_readlinkat (int fd, char const *file, char *buf, size_t len)
  20. {
  21. return readlinkat (fd, file, buf, len);
  22. }
  23. #else
  24. /* Gnulib provides a readlink stub for mingw; use it for distinction
  25. between EINVAL and ENOENT, rather than always failing with ENOSYS. */
  26. /* POSIX 2008 says that unlike readlink, readlinkat returns 0 for
  27. success instead of the buffer length. But this would render
  28. readlinkat worthless since readlink does not guarantee a
  29. NUL-terminated buffer. Assume this was a bug in POSIX. */
  30. /* Read the contents of symlink FILE into buffer BUF of size LEN, in the
  31. directory open on descriptor FD. If possible, do it without changing
  32. the working directory. Otherwise, resort to using save_cwd/fchdir,
  33. then readlink/restore_cwd. If either the save_cwd or the restore_cwd
  34. fails, then give a diagnostic and exit nonzero. */
  35. # define AT_FUNC_NAME readlinkat
  36. # define AT_FUNC_F1 readlink
  37. # define AT_FUNC_POST_FILE_PARAM_DECLS , char *buf, size_t len
  38. # define AT_FUNC_POST_FILE_ARGS , buf, len
  39. # define AT_FUNC_RESULT ssize_t
  40. # include "at-func.c"
  41. # undef AT_FUNC_NAME
  42. # undef AT_FUNC_F1
  43. # undef AT_FUNC_POST_FILE_PARAM_DECLS
  44. # undef AT_FUNC_POST_FILE_ARGS
  45. # undef AT_FUNC_RESULT
  46. #endif