dirent.in.h 11 KB

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