dirent.in.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* A GNU-like <dirent.h>.
  2. Copyright (C) 2006-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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 General Public License for more details.
  11. You should have received a copy of the GNU 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. /* The __attribute__ feature is available in gcc versions 2.5 and later.
  49. The attribute __pure__ was added in gcc 2.96. */
  50. #ifndef _GL_ATTRIBUTE_PURE
  51. # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) || defined __clang__
  52. # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
  53. # else
  54. # define _GL_ATTRIBUTE_PURE /* empty */
  55. # endif
  56. #endif
  57. /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
  58. /* The definition of _GL_ARG_NONNULL is copied here. */
  59. /* The definition of _GL_WARN_ON_USE is copied here. */
  60. /* Declare overridden functions. */
  61. #if @GNULIB_OPENDIR@
  62. # if @REPLACE_OPENDIR@
  63. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  64. # undef opendir
  65. # define opendir rpl_opendir
  66. # define GNULIB_defined_opendir 1
  67. # endif
  68. _GL_FUNCDECL_RPL (opendir, DIR *, (const char *dir_name) _GL_ARG_NONNULL ((1)));
  69. _GL_CXXALIAS_RPL (opendir, DIR *, (const char *dir_name));
  70. # else
  71. # if !@HAVE_OPENDIR@
  72. _GL_FUNCDECL_SYS (opendir, DIR *, (const char *dir_name) _GL_ARG_NONNULL ((1)));
  73. # endif
  74. _GL_CXXALIAS_SYS (opendir, DIR *, (const char *dir_name));
  75. # endif
  76. _GL_CXXALIASWARN (opendir);
  77. #elif defined GNULIB_POSIXCHECK
  78. # undef opendir
  79. # if HAVE_RAW_DECL_OPENDIR
  80. _GL_WARN_ON_USE (opendir, "opendir is not portable - "
  81. "use gnulib module opendir for portability");
  82. # endif
  83. #endif
  84. #if @GNULIB_READDIR@
  85. # if !@HAVE_READDIR@
  86. _GL_FUNCDECL_SYS (readdir, struct dirent *, (DIR *dirp) _GL_ARG_NONNULL ((1)));
  87. # endif
  88. _GL_CXXALIAS_SYS (readdir, struct dirent *, (DIR *dirp));
  89. _GL_CXXALIASWARN (readdir);
  90. #elif defined GNULIB_POSIXCHECK
  91. # undef readdir
  92. # if HAVE_RAW_DECL_READDIR
  93. _GL_WARN_ON_USE (readdir, "readdir is not portable - "
  94. "use gnulib module readdir for portability");
  95. # endif
  96. #endif
  97. #if @GNULIB_REWINDDIR@
  98. # if !@HAVE_REWINDDIR@
  99. _GL_FUNCDECL_SYS (rewinddir, void, (DIR *dirp) _GL_ARG_NONNULL ((1)));
  100. # endif
  101. _GL_CXXALIAS_SYS (rewinddir, void, (DIR *dirp));
  102. _GL_CXXALIASWARN (rewinddir);
  103. #elif defined GNULIB_POSIXCHECK
  104. # undef rewinddir
  105. # if HAVE_RAW_DECL_REWINDDIR
  106. _GL_WARN_ON_USE (rewinddir, "rewinddir is not portable - "
  107. "use gnulib module rewinddir for portability");
  108. # endif
  109. #endif
  110. #if @GNULIB_CLOSEDIR@
  111. # if @REPLACE_CLOSEDIR@
  112. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  113. # undef closedir
  114. # define closedir rpl_closedir
  115. # define GNULIB_defined_closedir 1
  116. # endif
  117. _GL_FUNCDECL_RPL (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1)));
  118. _GL_CXXALIAS_RPL (closedir, int, (DIR *dirp));
  119. # else
  120. # if !@HAVE_CLOSEDIR@
  121. _GL_FUNCDECL_SYS (closedir, int, (DIR *dirp) _GL_ARG_NONNULL ((1)));
  122. # endif
  123. _GL_CXXALIAS_SYS (closedir, int, (DIR *dirp));
  124. # endif
  125. _GL_CXXALIASWARN (closedir);
  126. #elif defined GNULIB_POSIXCHECK
  127. # undef closedir
  128. # if HAVE_RAW_DECL_CLOSEDIR
  129. _GL_WARN_ON_USE (closedir, "closedir is not portable - "
  130. "use gnulib module closedir for portability");
  131. # endif
  132. #endif
  133. #if @GNULIB_DIRFD@
  134. /* Return the file descriptor associated with the given directory stream,
  135. or -1 if none exists. */
  136. # if @REPLACE_DIRFD@
  137. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  138. # undef dirfd
  139. # define dirfd rpl_dirfd
  140. # endif
  141. _GL_FUNCDECL_RPL (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1)));
  142. _GL_CXXALIAS_RPL (dirfd, int, (DIR *));
  143. # ifdef __KLIBC__
  144. /* Gnulib internal hooks needed to maintain the dirfd metadata. */
  145. _GL_EXTERN_C int _gl_register_dirp_fd (int fd, DIR *dirp)
  146. _GL_ARG_NONNULL ((2));
  147. _GL_EXTERN_C void _gl_unregister_dirp_fd (int fd);
  148. # endif
  149. # else
  150. # if defined __cplusplus && defined GNULIB_NAMESPACE && defined dirfd
  151. /* dirfd is defined as a macro and not as a function.
  152. Turn it into a function and get rid of the macro. */
  153. static inline int (dirfd) (DIR *dp) { return dirfd (dp); }
  154. # undef dirfd
  155. # endif
  156. # if !(@HAVE_DECL_DIRFD@ || defined dirfd)
  157. _GL_FUNCDECL_SYS (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1)));
  158. # endif
  159. _GL_CXXALIAS_SYS (dirfd, int, (DIR *));
  160. # endif
  161. _GL_CXXALIASWARN (dirfd);
  162. #elif defined GNULIB_POSIXCHECK
  163. # undef dirfd
  164. # if HAVE_RAW_DECL_DIRFD
  165. _GL_WARN_ON_USE (dirfd, "dirfd is unportable - "
  166. "use gnulib module dirfd for portability");
  167. # endif
  168. #endif
  169. #if @GNULIB_FDOPENDIR@
  170. /* Open a directory stream visiting the given directory file
  171. descriptor. Return NULL and set errno if fd is not visiting a
  172. directory. On success, this function consumes fd (it will be
  173. implicitly closed either by this function or by a subsequent
  174. closedir). */
  175. # if @REPLACE_FDOPENDIR@
  176. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  177. # undef fdopendir
  178. # define fdopendir rpl_fdopendir
  179. # endif
  180. _GL_FUNCDECL_RPL (fdopendir, DIR *, (int fd));
  181. _GL_CXXALIAS_RPL (fdopendir, DIR *, (int fd));
  182. # else
  183. # if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@
  184. _GL_FUNCDECL_SYS (fdopendir, DIR *, (int fd));
  185. # endif
  186. _GL_CXXALIAS_SYS (fdopendir, DIR *, (int fd));
  187. # endif
  188. _GL_CXXALIASWARN (fdopendir);
  189. #elif defined GNULIB_POSIXCHECK
  190. # undef fdopendir
  191. # if HAVE_RAW_DECL_FDOPENDIR
  192. _GL_WARN_ON_USE (fdopendir, "fdopendir is unportable - "
  193. "use gnulib module fdopendir for portability");
  194. # endif
  195. #endif
  196. #if @GNULIB_SCANDIR@
  197. /* Scan the directory DIR, calling FILTER on each directory entry.
  198. Entries for which FILTER returns nonzero are individually malloc'd,
  199. sorted using qsort with CMP, and collected in a malloc'd array in
  200. *NAMELIST. Returns the number of entries selected, or -1 on error. */
  201. # if !@HAVE_SCANDIR@
  202. _GL_FUNCDECL_SYS (scandir, int,
  203. (const char *dir, struct dirent ***namelist,
  204. int (*filter) (const struct dirent *),
  205. int (*cmp) (const struct dirent **, const struct dirent **))
  206. _GL_ARG_NONNULL ((1, 2, 4)));
  207. # endif
  208. /* Need to cast, because on glibc systems, the fourth parameter is
  209. int (*cmp) (const void *, const void *). */
  210. _GL_CXXALIAS_SYS_CAST (scandir, int,
  211. (const char *dir, struct dirent ***namelist,
  212. int (*filter) (const struct dirent *),
  213. int (*cmp) (const struct dirent **, const struct dirent **)));
  214. _GL_CXXALIASWARN (scandir);
  215. #elif defined GNULIB_POSIXCHECK
  216. # undef scandir
  217. # if HAVE_RAW_DECL_SCANDIR
  218. _GL_WARN_ON_USE (scandir, "scandir is unportable - "
  219. "use gnulib module scandir for portability");
  220. # endif
  221. #endif
  222. #if @GNULIB_ALPHASORT@
  223. /* Compare two 'struct dirent' entries alphabetically. */
  224. # if !@HAVE_ALPHASORT@
  225. _GL_FUNCDECL_SYS (alphasort, int,
  226. (const struct dirent **, const struct dirent **)
  227. _GL_ATTRIBUTE_PURE
  228. _GL_ARG_NONNULL ((1, 2)));
  229. # endif
  230. /* Need to cast, because on glibc systems, the parameters are
  231. (const void *, const void *). */
  232. _GL_CXXALIAS_SYS_CAST (alphasort, int,
  233. (const struct dirent **, const struct dirent **));
  234. _GL_CXXALIASWARN (alphasort);
  235. #elif defined GNULIB_POSIXCHECK
  236. # undef alphasort
  237. # if HAVE_RAW_DECL_ALPHASORT
  238. _GL_WARN_ON_USE (alphasort, "alphasort is unportable - "
  239. "use gnulib module alphasort for portability");
  240. # endif
  241. #endif
  242. #endif /* _@GUARD_PREFIX@_DIRENT_H */
  243. #endif /* _@GUARD_PREFIX@_DIRENT_H */