1 1.1 christos /* A GNU-like <dirent.h>. 2 1.1.1.2 christos Copyright (C) 2006-2022 Free Software Foundation, Inc. 3 1.1 christos 4 1.1.1.2 christos This file is free software: you can redistribute it and/or modify 5 1.1.1.2 christos it under the terms of the GNU Lesser General Public License as 6 1.1.1.2 christos published by the Free Software Foundation; either version 2.1 of the 7 1.1.1.2 christos License, or (at your option) any later version. 8 1.1 christos 9 1.1.1.2 christos This file is distributed in the hope that it will be useful, 10 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 11 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 1.1.1.2 christos GNU Lesser General Public License for more details. 13 1.1 christos 14 1.1.1.2 christos You should have received a copy of the GNU Lesser General Public License 15 1.1 christos along with this program. If not, see <https://www.gnu.org/licenses/>. */ 16 1.1 christos 17 1.1 christos #ifndef _@GUARD_PREFIX@_DIRENT_H 18 1.1 christos 19 1.1 christos #if __GNUC__ >= 3 20 1.1 christos @PRAGMA_SYSTEM_HEADER@ 21 1.1 christos #endif 22 1.1 christos @PRAGMA_COLUMNS@ 23 1.1 christos 24 1.1 christos /* The include_next requires a split double-inclusion guard. */ 25 1.1 christos #if @HAVE_DIRENT_H@ 26 1.1 christos # @INCLUDE_NEXT@ @NEXT_DIRENT_H@ 27 1.1 christos #endif 28 1.1 christos 29 1.1 christos #ifndef _@GUARD_PREFIX@_DIRENT_H 30 1.1 christos #define _@GUARD_PREFIX@_DIRENT_H 31 1.1 christos 32 1.1 christos /* Get ino_t. Needed on some systems, including glibc 2.8. */ 33 1.1 christos #include <sys/types.h> 34 1.1 christos 35 1.1 christos #if !@HAVE_DIRENT_H@ 36 1.1 christos /* Define types DIR and 'struct dirent'. */ 37 1.1 christos # if !GNULIB_defined_struct_dirent 38 1.1 christos struct dirent 39 1.1 christos { 40 1.1 christos char d_type; 41 1.1 christos char d_name[1]; 42 1.1 christos }; 43 1.1 christos /* Possible values for 'd_type'. */ 44 1.1 christos # define DT_UNKNOWN 0 45 1.1 christos # define DT_FIFO 1 /* FIFO */ 46 1.1 christos # define DT_CHR 2 /* character device */ 47 1.1 christos # define DT_DIR 4 /* directory */ 48 1.1 christos # define DT_BLK 6 /* block device */ 49 1.1 christos # define DT_REG 8 /* regular file */ 50 1.1 christos # define DT_LNK 10 /* symbolic link */ 51 1.1 christos # define DT_SOCK 12 /* socket */ 52 1.1 christos # define DT_WHT 14 /* whiteout */ 53 1.1 christos typedef struct gl_directory DIR; 54 1.1 christos # define GNULIB_defined_struct_dirent 1 55 1.1 christos # endif 56 1.1 christos #endif 57 1.1 christos 58 1.1.1.2 christos /* _GL_ATTRIBUTE_DEALLOC (F, I) declares that the function returns pointers 59 1.1.1.2 christos that can be freed by passing them as the Ith argument to the 60 1.1.1.2 christos function F. */ 61 1.1.1.2 christos #ifndef _GL_ATTRIBUTE_DEALLOC 62 1.1.1.2 christos # if __GNUC__ >= 11 63 1.1.1.2 christos # define _GL_ATTRIBUTE_DEALLOC(f, i) __attribute__ ((__malloc__ (f, i))) 64 1.1.1.2 christos # else 65 1.1.1.2 christos # define _GL_ATTRIBUTE_DEALLOC(f, i) 66 1.1.1.2 christos # endif 67 1.1.1.2 christos #endif 68 1.1.1.2 christos 69 1.1.1.2 christos /* _GL_ATTRIBUTE_MALLOC declares that the function returns a pointer to freshly 70 1.1.1.2 christos allocated memory. */ 71 1.1.1.2 christos /* Applies to: functions. */ 72 1.1.1.2 christos #ifndef _GL_ATTRIBUTE_MALLOC 73 1.1.1.2 christos # if __GNUC__ >= 3 || defined __clang__ 74 1.1.1.2 christos # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) 75 1.1.1.2 christos # else 76 1.1.1.2 christos # define _GL_ATTRIBUTE_MALLOC 77 1.1.1.2 christos # endif 78 1.1.1.2 christos #endif 79 1.1.1.2 christos 80 1.1 christos /* The __attribute__ feature is available in gcc versions 2.5 and later. 81 1.1 christos The attribute __pure__ was added in gcc 2.96. */ 82 1.1 christos #ifndef _GL_ATTRIBUTE_PURE 83 1.1.1.2 christos # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) || defined __clang__ 84 1.1 christos # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__)) 85 1.1 christos # else 86 1.1 christos # define _GL_ATTRIBUTE_PURE /* empty */ 87 1.1 christos # endif 88 1.1 christos #endif 89 1.1 christos 90 1.1 christos /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */ 91 1.1 christos 92 1.1 christos /* The definition of _GL_ARG_NONNULL is copied here. */ 93 1.1 christos 94 1.1 christos /* The definition of _GL_WARN_ON_USE is copied here. */ 95 1.1 christos 96 1.1 christos 97 1.1 christos /* Declare overridden functions. */ 98 1.1 christos 99 1.1.1.2 christos #if @GNULIB_CLOSEDIR@ 100 1.1.1.2 christos # if @REPLACE_CLOSEDIR@ 101 1.1.1.2 christos # if !(defined __cplusplus && defined GNULIB_NAMESPACE) 102 1.1.1.2 christos # undef closedir 103 1.1.1.2 christos # define closedir rpl_closedir 104 1.1.1.2 christos # define GNULIB_defined_closedir 1 105 1.1.1.2 christos # endif 106 1.1.1.2 christos _GL_FUNCDECL_RPL (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1))); 107 1.1.1.2 christos _GL_CXXALIAS_RPL (closedir, int, (DIR *dirp)); 108 1.1.1.2 christos # else 109 1.1.1.2 christos # if !@HAVE_CLOSEDIR@ 110 1.1.1.2 christos _GL_FUNCDECL_SYS (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1))); 111 1.1.1.2 christos # endif 112 1.1.1.2 christos _GL_CXXALIAS_SYS (closedir, int, (DIR *dirp)); 113 1.1.1.2 christos # endif 114 1.1.1.2 christos _GL_CXXALIASWARN (closedir); 115 1.1.1.2 christos #elif defined GNULIB_POSIXCHECK 116 1.1.1.2 christos # undef closedir 117 1.1.1.2 christos # if HAVE_RAW_DECL_CLOSEDIR 118 1.1.1.2 christos _GL_WARN_ON_USE (closedir, "closedir is not portable - " 119 1.1.1.2 christos "use gnulib module closedir for portability"); 120 1.1.1.2 christos # endif 121 1.1.1.2 christos #endif 122 1.1.1.2 christos 123 1.1 christos #if @GNULIB_OPENDIR@ 124 1.1 christos # if @REPLACE_OPENDIR@ 125 1.1 christos # if !(defined __cplusplus && defined GNULIB_NAMESPACE) 126 1.1 christos # undef opendir 127 1.1 christos # define opendir rpl_opendir 128 1.1 christos # define GNULIB_defined_opendir 1 129 1.1 christos # endif 130 1.1.1.2 christos _GL_FUNCDECL_RPL (opendir, DIR *, 131 1.1.1.2 christos (const char *dir_name) 132 1.1.1.2 christos _GL_ARG_NONNULL ((1)) 133 1.1.1.2 christos _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); 134 1.1 christos _GL_CXXALIAS_RPL (opendir, DIR *, (const char *dir_name)); 135 1.1 christos # else 136 1.1.1.2 christos # if !@HAVE_OPENDIR@ || __GNUC__ >= 11 137 1.1.1.2 christos _GL_FUNCDECL_SYS (opendir, DIR *, 138 1.1.1.2 christos (const char *dir_name) 139 1.1.1.2 christos _GL_ARG_NONNULL ((1)) 140 1.1.1.2 christos _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); 141 1.1 christos # endif 142 1.1 christos _GL_CXXALIAS_SYS (opendir, DIR *, (const char *dir_name)); 143 1.1 christos # endif 144 1.1 christos _GL_CXXALIASWARN (opendir); 145 1.1.1.2 christos #else 146 1.1.1.2 christos # if @GNULIB_CLOSEDIR@ && __GNUC__ >= 11 && !defined opendir 147 1.1.1.2 christos /* For -Wmismatched-dealloc: Associate opendir with closedir or 148 1.1.1.2 christos rpl_closedir. */ 149 1.1.1.2 christos _GL_FUNCDECL_SYS (opendir, DIR *, 150 1.1.1.2 christos (const char *dir_name) 151 1.1.1.2 christos _GL_ARG_NONNULL ((1)) 152 1.1.1.2 christos _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); 153 1.1.1.2 christos # endif 154 1.1.1.2 christos # if defined GNULIB_POSIXCHECK 155 1.1.1.2 christos # undef opendir 156 1.1.1.2 christos # if HAVE_RAW_DECL_OPENDIR 157 1.1 christos _GL_WARN_ON_USE (opendir, "opendir is not portable - " 158 1.1 christos "use gnulib module opendir for portability"); 159 1.1.1.2 christos # endif 160 1.1 christos # endif 161 1.1 christos #endif 162 1.1 christos 163 1.1 christos #if @GNULIB_READDIR@ 164 1.1 christos # if !@HAVE_READDIR@ 165 1.1 christos _GL_FUNCDECL_SYS (readdir, struct dirent *, (DIR *dirp) _GL_ARG_NONNULL ((1))); 166 1.1 christos # endif 167 1.1 christos _GL_CXXALIAS_SYS (readdir, struct dirent *, (DIR *dirp)); 168 1.1 christos _GL_CXXALIASWARN (readdir); 169 1.1 christos #elif defined GNULIB_POSIXCHECK 170 1.1 christos # undef readdir 171 1.1 christos # if HAVE_RAW_DECL_READDIR 172 1.1 christos _GL_WARN_ON_USE (readdir, "readdir is not portable - " 173 1.1 christos "use gnulib module readdir for portability"); 174 1.1 christos # endif 175 1.1 christos #endif 176 1.1 christos 177 1.1 christos #if @GNULIB_REWINDDIR@ 178 1.1 christos # if !@HAVE_REWINDDIR@ 179 1.1 christos _GL_FUNCDECL_SYS (rewinddir, void, (DIR *dirp) _GL_ARG_NONNULL ((1))); 180 1.1 christos # endif 181 1.1 christos _GL_CXXALIAS_SYS (rewinddir, void, (DIR *dirp)); 182 1.1 christos _GL_CXXALIASWARN (rewinddir); 183 1.1 christos #elif defined GNULIB_POSIXCHECK 184 1.1 christos # undef rewinddir 185 1.1 christos # if HAVE_RAW_DECL_REWINDDIR 186 1.1 christos _GL_WARN_ON_USE (rewinddir, "rewinddir is not portable - " 187 1.1 christos "use gnulib module rewinddir for portability"); 188 1.1 christos # endif 189 1.1 christos #endif 190 1.1 christos 191 1.1 christos #if @GNULIB_DIRFD@ 192 1.1 christos /* Return the file descriptor associated with the given directory stream, 193 1.1 christos or -1 if none exists. */ 194 1.1 christos # if @REPLACE_DIRFD@ 195 1.1.1.2 christos /* On kLIBC, dirfd() is a macro that does not work. Undefine it. */ 196 1.1.1.2 christos # if !(defined __cplusplus && defined GNULIB_NAMESPACE) || defined dirfd 197 1.1 christos # undef dirfd 198 1.1 christos # define dirfd rpl_dirfd 199 1.1 christos # endif 200 1.1 christos _GL_FUNCDECL_RPL (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1))); 201 1.1 christos _GL_CXXALIAS_RPL (dirfd, int, (DIR *)); 202 1.1 christos 203 1.1 christos # ifdef __KLIBC__ 204 1.1 christos /* Gnulib internal hooks needed to maintain the dirfd metadata. */ 205 1.1 christos _GL_EXTERN_C int _gl_register_dirp_fd (int fd, DIR *dirp) 206 1.1 christos _GL_ARG_NONNULL ((2)); 207 1.1 christos _GL_EXTERN_C void _gl_unregister_dirp_fd (int fd); 208 1.1 christos # endif 209 1.1 christos # else 210 1.1 christos # if defined __cplusplus && defined GNULIB_NAMESPACE && defined dirfd 211 1.1 christos /* dirfd is defined as a macro and not as a function. 212 1.1 christos Turn it into a function and get rid of the macro. */ 213 1.1 christos static inline int (dirfd) (DIR *dp) { return dirfd (dp); } 214 1.1 christos # undef dirfd 215 1.1 christos # endif 216 1.1 christos # if !(@HAVE_DECL_DIRFD@ || defined dirfd) 217 1.1 christos _GL_FUNCDECL_SYS (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1))); 218 1.1 christos # endif 219 1.1 christos _GL_CXXALIAS_SYS (dirfd, int, (DIR *)); 220 1.1 christos # endif 221 1.1 christos _GL_CXXALIASWARN (dirfd); 222 1.1 christos #elif defined GNULIB_POSIXCHECK 223 1.1 christos # undef dirfd 224 1.1 christos # if HAVE_RAW_DECL_DIRFD 225 1.1 christos _GL_WARN_ON_USE (dirfd, "dirfd is unportable - " 226 1.1 christos "use gnulib module dirfd for portability"); 227 1.1 christos # endif 228 1.1 christos #endif 229 1.1 christos 230 1.1 christos #if @GNULIB_FDOPENDIR@ 231 1.1 christos /* Open a directory stream visiting the given directory file 232 1.1 christos descriptor. Return NULL and set errno if fd is not visiting a 233 1.1 christos directory. On success, this function consumes fd (it will be 234 1.1 christos implicitly closed either by this function or by a subsequent 235 1.1 christos closedir). */ 236 1.1 christos # if @REPLACE_FDOPENDIR@ 237 1.1 christos # if !(defined __cplusplus && defined GNULIB_NAMESPACE) 238 1.1 christos # undef fdopendir 239 1.1 christos # define fdopendir rpl_fdopendir 240 1.1 christos # endif 241 1.1.1.2 christos _GL_FUNCDECL_RPL (fdopendir, DIR *, 242 1.1.1.2 christos (int fd) 243 1.1.1.2 christos _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); 244 1.1 christos _GL_CXXALIAS_RPL (fdopendir, DIR *, (int fd)); 245 1.1 christos # else 246 1.1.1.2 christos # if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@ || __GNUC__ >= 11 247 1.1.1.2 christos _GL_FUNCDECL_SYS (fdopendir, DIR *, 248 1.1.1.2 christos (int fd) 249 1.1.1.2 christos _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); 250 1.1 christos # endif 251 1.1 christos _GL_CXXALIAS_SYS (fdopendir, DIR *, (int fd)); 252 1.1 christos # endif 253 1.1 christos _GL_CXXALIASWARN (fdopendir); 254 1.1.1.2 christos #else 255 1.1.1.2 christos # if @GNULIB_CLOSEDIR@ && __GNUC__ >= 11 && !defined fdopendir 256 1.1.1.2 christos /* For -Wmismatched-dealloc: Associate fdopendir with closedir or 257 1.1.1.2 christos rpl_closedir. */ 258 1.1.1.2 christos _GL_FUNCDECL_SYS (fdopendir, DIR *, 259 1.1.1.2 christos (int fd) 260 1.1.1.2 christos _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC (closedir, 1)); 261 1.1.1.2 christos # endif 262 1.1.1.2 christos # if defined GNULIB_POSIXCHECK 263 1.1.1.2 christos # undef fdopendir 264 1.1.1.2 christos # if HAVE_RAW_DECL_FDOPENDIR 265 1.1 christos _GL_WARN_ON_USE (fdopendir, "fdopendir is unportable - " 266 1.1 christos "use gnulib module fdopendir for portability"); 267 1.1.1.2 christos # endif 268 1.1 christos # endif 269 1.1 christos #endif 270 1.1 christos 271 1.1 christos #if @GNULIB_SCANDIR@ 272 1.1 christos /* Scan the directory DIR, calling FILTER on each directory entry. 273 1.1 christos Entries for which FILTER returns nonzero are individually malloc'd, 274 1.1 christos sorted using qsort with CMP, and collected in a malloc'd array in 275 1.1 christos *NAMELIST. Returns the number of entries selected, or -1 on error. */ 276 1.1 christos # if !@HAVE_SCANDIR@ 277 1.1 christos _GL_FUNCDECL_SYS (scandir, int, 278 1.1 christos (const char *dir, struct dirent ***namelist, 279 1.1 christos int (*filter) (const struct dirent *), 280 1.1 christos int (*cmp) (const struct dirent **, const struct dirent **)) 281 1.1 christos _GL_ARG_NONNULL ((1, 2, 4))); 282 1.1 christos # endif 283 1.1 christos /* Need to cast, because on glibc systems, the fourth parameter is 284 1.1 christos int (*cmp) (const void *, const void *). */ 285 1.1 christos _GL_CXXALIAS_SYS_CAST (scandir, int, 286 1.1 christos (const char *dir, struct dirent ***namelist, 287 1.1 christos int (*filter) (const struct dirent *), 288 1.1 christos int (*cmp) (const struct dirent **, const struct dirent **))); 289 1.1 christos _GL_CXXALIASWARN (scandir); 290 1.1 christos #elif defined GNULIB_POSIXCHECK 291 1.1 christos # undef scandir 292 1.1 christos # if HAVE_RAW_DECL_SCANDIR 293 1.1 christos _GL_WARN_ON_USE (scandir, "scandir is unportable - " 294 1.1 christos "use gnulib module scandir for portability"); 295 1.1 christos # endif 296 1.1 christos #endif 297 1.1 christos 298 1.1 christos #if @GNULIB_ALPHASORT@ 299 1.1 christos /* Compare two 'struct dirent' entries alphabetically. */ 300 1.1 christos # if !@HAVE_ALPHASORT@ 301 1.1 christos _GL_FUNCDECL_SYS (alphasort, int, 302 1.1 christos (const struct dirent **, const struct dirent **) 303 1.1 christos _GL_ATTRIBUTE_PURE 304 1.1 christos _GL_ARG_NONNULL ((1, 2))); 305 1.1 christos # endif 306 1.1 christos /* Need to cast, because on glibc systems, the parameters are 307 1.1 christos (const void *, const void *). */ 308 1.1 christos _GL_CXXALIAS_SYS_CAST (alphasort, int, 309 1.1 christos (const struct dirent **, const struct dirent **)); 310 1.1 christos _GL_CXXALIASWARN (alphasort); 311 1.1 christos #elif defined GNULIB_POSIXCHECK 312 1.1 christos # undef alphasort 313 1.1 christos # if HAVE_RAW_DECL_ALPHASORT 314 1.1 christos _GL_WARN_ON_USE (alphasort, "alphasort is unportable - " 315 1.1 christos "use gnulib module alphasort for portability"); 316 1.1 christos # endif 317 1.1 christos #endif 318 1.1 christos 319 1.1 christos 320 1.1 christos #endif /* _@GUARD_PREFIX@_DIRENT_H */ 321 1.1 christos #endif /* _@GUARD_PREFIX@_DIRENT_H */ 322