careadlinkat.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Read symbolic links into a buffer without size limitation, relative to fd.
  2. Copyright (C) 2011 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 Paul Eggert, Bruno Haible, and Jim Meyering. */
  14. #ifndef _GL_CAREADLINKAT_H
  15. #define _GL_CAREADLINKAT_H
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. struct allocator;
  19. /* Assuming the current directory is FD, get the symbolic link value
  20. of FILENAME as a null-terminated string and put it into a buffer.
  21. If FD is AT_FDCWD, FILENAME is interpreted relative to the current
  22. working directory, as in openat.
  23. If the link is small enough to fit into BUFFER put it there.
  24. BUFFER's size is BUFFER_SIZE, and BUFFER can be null
  25. if BUFFER_SIZE is zero.
  26. If the link is not small, put it into a dynamically allocated
  27. buffer managed by ALLOC. It is the caller's responsibility to free
  28. the returned value if it is nonnull and is not BUFFER.
  29. The PREADLINKAT function specifies how to read links. It operates
  30. like POSIX readlinkat()
  31. <http://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
  32. but can assume that its first argument is the same as FD.
  33. If successful, return the buffer address; otherwise return NULL and
  34. set errno. */
  35. char *careadlinkat (int fd, char const *filename,
  36. char *buffer, size_t buffer_size,
  37. struct allocator const *alloc,
  38. ssize_t (*preadlinkat) (int, char const *,
  39. char *, size_t));
  40. /* Suitable values for careadlinkat's FD and PREADLINKAT arguments,
  41. when doing a plain readlink:
  42. Pass FD = AT_FDCWD and PREADLINKAT = careadlinkatcwd. */
  43. #if HAVE_READLINKAT
  44. /* AT_FDCWD is declared in <fcntl.h>. */
  45. #else
  46. /* Define AT_FDCWD independently, so that the careadlinkat module does
  47. not depend on the fcntl-h module. The value does not matter, since
  48. careadlinkatcwd ignores it, but we might as well use the same value
  49. as fcntl-h. */
  50. # ifndef AT_FDCWD
  51. # define AT_FDCWD (-3041965)
  52. # endif
  53. #endif
  54. ssize_t careadlinkatcwd (int fd, char const *filename,
  55. char *buffer, size_t buffer_size);
  56. #endif /* _GL_CAREADLINKAT_H */