Debian Codegraph @

lib/issymlink.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 _ISSYMLINK_H
18 #define _ISSYMLINK_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 readlink */
27
28
29 _GL_INLINE_HEADER_BEGIN
30
31 #ifndef _GL_ISSYMLINK_INLINE
32 # define _GL_ISSYMLINK_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 Returns
39 1 if FILENAME is a symbolic link,
40 0 if FILENAME exists and is not a symbolic link,
41 -1 with errno set if determination failed, in particular
42 -1 with errno = ENOENT or ENOTDIR if FILENAME does not exist. */
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 _GL_ISSYMLINK_INLINE int issymlink (const char *filename)
47 _GL_ARG_NONNULL ((1));
48 _GL_ISSYMLINK_INLINE int
49 issymlink (const char *filename)
50 {
51 char linkbuf[1];
52 if (readlink (filename, linkbuf, sizeof (linkbuf)) >= 0)
53 return 1;
54 if (errno == EINVAL)
55 return 0;
56 else
57 return -1;
58 }
59 #ifdef __cplusplus
60 }
61 #endif
62
63 _GL_INLINE_HEADER_END
64
65 #endif /* _ISSYMLINK_H */