Merge from mainline.
[bpt/emacs.git] / lib / careadlinkat.h
1 /* Read symbolic links into a buffer without size limitation, relative to fd.
2
3 Copyright (C) 2011 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
19
20 #ifndef _GL_CAREADLINKAT_H
21
22 #include <fcntl.h>
23 #include <unistd.h>
24
25 struct allocator;
26
27 /* Assuming the current directory is FD, get the symbolic link value
28 of FILENAME as a null-terminated string and put it into a buffer.
29 If FD is AT_FDCWD, FILENAME is interpreted relative to the current
30 working directory, as in openat.
31
32 If the link is small enough to fit into BUFFER put it there.
33 BUFFER's size is BUFFER_SIZE, and BUFFER can be null
34 if BUFFER_SIZE is zero.
35
36 If the link is not small, put it into a dynamically allocated
37 buffer managed by ALLOC. It is the caller's responsibility to free
38 the returned value if it is nonnull and is not BUFFER.
39
40 The PREADLINKAT function specifies how to read links.
41
42 If successful, return the buffer address; otherwise return NULL and
43 set errno. */
44
45 char *careadlinkat (int fd, char const *filename,
46 char *buffer, size_t buffer_size,
47 struct allocator const *alloc,
48 ssize_t (*preadlinkat) (int, char const *,
49 char *, size_t));
50
51 /* Suitable values for careadlinkat's FD and PREADLINKAT arguments,
52 when doing a plain readlink. */
53 #if HAVE_READLINKAT
54 # define careadlinkatcwd readlinkat
55 #else
56 /* Define AT_FDCWD independently, so that the careadlinkat module does
57 not depend on the fcntl-h module. The value does not matter, since
58 careadlinkatcwd ignores it, but we might as well use the same value
59 as fcntl-h. */
60 # ifndef AT_FDCWD
61 # define AT_FDCWD (-3041965)
62 # endif
63 ssize_t careadlinkatcwd (int fd, char const *filename,
64 char *buffer, size_t buffer_size);
65 #endif
66
67 #endif /* _GL_CAREADLINKAT_H */