Debian Codegraph @

lib/issymlinkat.h

raw
1 /* Test whether a file is a symbolic link.
2 Copyright (C) 2025-2026 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #ifndef _ISSYMLINKAT_H
18 #define _ISSYMLINKAT_H
19
20 /* This file uses _GL_ARG_NONNULL, _GL_INLINE. */
21 #if !_GL_CONFIG_H_INCLUDED
22 #error "Please include config.h first."
23 #endif
24
25 #include <errno.h>
26 #include <unistd.h> /* for readlinkat */
27
28
29 _GL_INLINE_HEADER_BEGIN
30
31 #ifndef _GL_ISSYMLINKAT_INLINE
32 # define _GL_ISSYMLINKAT_INLINE _GL_INLINE
33 #endif
34
35 /* Tests whether FILENAME represents a symbolic link.
36 This function is more reliable than lstat() / fstatat() followed by S_ISLNK,
37 because it avoids possible EOVERFLOW errors.
38 If FILENAME is a relative file name, it is interpreted as relative to the
39 directory referred to by FD (where FD = AT_FDCWD denotes the current
40 directory).
41 Returns
42 1 if FILENAME is a symbolic link,
43 0 if FILENAME exists and is not a symbolic link,
44 -1 with errno set if determination failed, in particular
45 -1 with errno = ENOENT or ENOTDIR if FILENAME does not exist. */
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 _GL_ISSYMLINKAT_INLINE int issymlinkat (int fd, const char *filename)
50 _GL_ARG_NONNULL ((2));
51 _GL_ISSYMLINKAT_INLINE int
52 issymlinkat (int fd, const char *filename)
53 {
54 char linkbuf[1];
55 if (readlinkat (fd, filename, linkbuf, sizeof (linkbuf)) >= 0)
56 return 1;
57 if (errno == EINVAL)
58 return 0;
59 else
60 return -1;
61 }
62 #ifdef __cplusplus
63 }
64 #endif
65
66 _GL_INLINE_HEADER_END
67
68 #endif /* _ISSYMLINKAT_H */