1 /* A GNU-like <dirent.h>.
2 Copyright (C) 2006-2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 @PRAGMA_SYSTEM_HEADER@
24 /* The include_next requires a split double-inclusion guard. */
25 #@INCLUDE_NEXT@ @NEXT_DIRENT_H@
30 /* Get ino_t. Needed on some systems, including glibc 2.8. */
31 #include <sys/types.h>
33 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
35 /* The definition of _GL_ARG_NONNULL is copied here. */
37 /* The definition of _GL_WARN_ON_USE is copied here. */
40 /* Declare overridden functions. */
42 #if @REPLACE_CLOSEDIR@
43 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
44 # define closedir rpl_closedir
46 _GL_FUNCDECL_RPL (closedir, int, (DIR *) _GL_ARG_NONNULL ((1)));
47 _GL_CXXALIAS_RPL (closedir, int, (DIR *));
49 _GL_CXXALIAS_SYS (closedir, int, (DIR *));
51 _GL_CXXALIASWARN (closedir);
54 /* Return the file descriptor associated with the given directory stream,
55 or -1 if none exists. */
57 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
59 # define dirfd rpl_dirfd
61 _GL_FUNCDECL_RPL (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1)));
62 _GL_CXXALIAS_RPL (dirfd, int, (DIR *));
64 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined dirfd
65 /* dirfd is defined as a macro and not as a function.
66 Turn it into a function and get rid of the macro. */
67 static inline int (dirfd) (DIR *dp) { return dirfd (dp); }
70 # if !(@HAVE_DECL_DIRFD@ || defined dirfd)
71 _GL_FUNCDECL_SYS (dirfd, int, (DIR *) _GL_ARG_NONNULL ((1)));
73 _GL_CXXALIAS_SYS (dirfd, int, (DIR *));
75 _GL_CXXALIASWARN (dirfd);
76 #elif defined GNULIB_POSIXCHECK
78 # if HAVE_RAW_DECL_DIRFD
79 _GL_WARN_ON_USE (dirfd, "dirfd is unportable - "
80 "use gnulib module dirfd for portability");
84 #if @GNULIB_FDOPENDIR@
85 /* Open a directory stream visiting the given directory file
86 descriptor. Return NULL and set errno if fd is not visiting a
87 directory. On success, this function consumes fd (it will be
88 implicitly closed either by this function or by a subsequent
90 # if @REPLACE_FDOPENDIR@
91 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
93 # define fdopendir rpl_fdopendir
95 _GL_FUNCDECL_RPL (fdopendir, DIR *, (int fd));
96 _GL_CXXALIAS_RPL (fdopendir, DIR *, (int fd));
98 # if !@HAVE_FDOPENDIR@ || !@HAVE_DECL_FDOPENDIR@
99 _GL_FUNCDECL_SYS (fdopendir, DIR *, (int fd));
101 _GL_CXXALIAS_SYS (fdopendir, DIR *, (int fd));
103 _GL_CXXALIASWARN (fdopendir);
104 #elif defined GNULIB_POSIXCHECK
106 # if HAVE_RAW_DECL_FDOPENDIR
107 _GL_WARN_ON_USE (fdopendir, "fdopendir is unportable - "
108 "use gnulib module fdopendir for portability");
112 #if @REPLACE_OPENDIR@
113 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
114 # define opendir rpl_opendir
116 _GL_FUNCDECL_RPL (opendir, DIR *, (const char *) _GL_ARG_NONNULL ((1)));
117 _GL_CXXALIAS_RPL (opendir, DIR *, (const char *));
119 _GL_CXXALIAS_SYS (opendir, DIR *, (const char *));
121 _GL_CXXALIASWARN (opendir);
124 /* Scan the directory DIR, calling FILTER on each directory entry.
125 Entries for which FILTER returns nonzero are individually malloc'd,
126 sorted using qsort with CMP, and collected in a malloc'd array in
127 *NAMELIST. Returns the number of entries selected, or -1 on error. */
129 _GL_FUNCDECL_SYS (scandir, int,
130 (const char *dir, struct dirent ***namelist,
131 int (*filter) (const struct dirent *),
132 int (*cmp) (const struct dirent **, const struct dirent **))
133 _GL_ARG_NONNULL ((1, 2, 4)));
135 /* Need to cast, because on glibc systems, the fourth parameter is
136 int (*cmp) (const void *, const void *). */
137 _GL_CXXALIAS_SYS_CAST (scandir, int,
138 (const char *dir, struct dirent ***namelist,
139 int (*filter) (const struct dirent *),
140 int (*cmp) (const struct dirent **, const struct dirent **)));
141 _GL_CXXALIASWARN (scandir);
142 #elif defined GNULIB_POSIXCHECK
144 # if HAVE_RAW_DECL_SCANDIR
145 _GL_WARN_ON_USE (scandir, "scandir is unportable - "
146 "use gnulib module scandir for portability");
150 #if @GNULIB_ALPHASORT@
151 /* Compare two 'struct dirent' entries alphabetically. */
152 # if !@HAVE_ALPHASORT@
153 _GL_FUNCDECL_SYS (alphasort, int,
154 (const struct dirent **, const struct dirent **)
155 _GL_ARG_NONNULL ((1, 2)));
157 /* Need to cast, because on glibc systems, the parameters are
158 (const void *, const void *). */
159 _GL_CXXALIAS_SYS_CAST (alphasort, int,
160 (const struct dirent **, const struct dirent **));
161 _GL_CXXALIASWARN (alphasort);
162 #elif defined GNULIB_POSIXCHECK
164 # if HAVE_RAW_DECL_ALPHASORT
165 _GL_WARN_ON_USE (alphasort, "alphasort is unportable - "
166 "use gnulib module alphasort for portability");
171 #endif /* _GL_DIRENT_H */
172 #endif /* _GL_DIRENT_H */