1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/*
* Copyright (C) 2008 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#pragma once
/**
* @file dirent.h
* @brief Directory entry iteration.
*/
#include <stdint.h>
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
/** d_type value when the type is not known. */
#define DT_UNKNOWN 0
/** d_type value for a FIFO. */
#define DT_FIFO 1
/** d_type value for a character device. */
#define DT_CHR 2
/** d_type value for a directory. */
#define DT_DIR 4
/** d_type value for a block device. */
#define DT_BLK 6
/** d_type value for a regular file. */
#define DT_REG 8
/** d_type value for a symbolic link. */
#define DT_LNK 10
/** d_type value for a socket. */
#define DT_SOCK 12
#define DT_WHT 14
#if defined(__LP64__)
#define __DIRENT64_INO_T ino_t
#else
#define __DIRENT64_INO_T uint64_t /* Historical accident. */
#endif
#define __DIRENT64_BODY \
__DIRENT64_INO_T d_ino; \
off64_t d_off; \
unsigned short d_reclen; \
unsigned char d_type; \
char d_name[256]; \
/** The structure returned by readdir(). Identical to dirent64 on Android. */
struct dirent { __DIRENT64_BODY };
/** The structure returned by readdir64(). Identical to dirent on Android. */
struct dirent64 { __DIRENT64_BODY };
#undef __DIRENT64_BODY
#undef __DIRENT64_INO_T
/* glibc compatibility. */
#undef _DIRENT_HAVE_D_NAMLEN /* Linux doesn't have a d_namlen field. */
#define _DIRENT_HAVE_D_RECLEN
#define _DIRENT_HAVE_D_OFF
#define _DIRENT_HAVE_D_TYPE
#define d_fileno d_ino
/** The structure returned by opendir()/fopendir(). */
typedef struct DIR DIR;
/**
* [opendir(3)](http://man7.org/linux/man-pages/man3/opendir.3.html)
* opens a directory stream for the directory at `__path`.
*
* Returns null and sets `errno` on failure.
*/
DIR* opendir(const char* __path);
/**
* [fopendir(3)](http://man7.org/linux/man-pages/man3/opendir.3.html)
* opens a directory stream for the directory at `__dir_fd`.
*
* Returns null and sets `errno` on failure.
*/
DIR* fdopendir(int __dir_fd);
/**
* [readdir(3)](http://man7.org/linux/man-pages/man3/readdir.3.html)
* returns the next directory entry in the given directory.
*
* Returns a pointer to a directory entry on success,
* or returns null and leaves `errno` unchanged at the end of the directory,
* or returns null and sets `errno` on failure.
*/
struct dirent* readdir(DIR* __dir);
/**
* [readdir64(3)](http://man7.org/linux/man-pages/man3/readdir.3.html)
* returns the next directory entry in the given directory.
*
* Returns a pointer to a directory entry on success,
* or returns null and leaves `errno` unchanged at the end of the directory,
* or returns null and sets `errno` on failure.
*/
struct dirent64* readdir64(DIR* __dir) __INTRODUCED_IN(21);
int readdir_r(DIR* __dir, struct dirent* __entry, struct dirent** __buffer) __attribute__((__deprecated__("readdir_r is deprecated; use readdir instead")));
int readdir64_r(DIR* __dir, struct dirent64* __entry, struct dirent64** __buffer) __INTRODUCED_IN(21) __attribute__((__deprecated__("readdir64_r is deprecated; use readdir64 instead")));
/**
* [closedir(3)](http://man7.org/linux/man-pages/man3/closedir.3.html)
* closes a directory stream.
*
* Returns 0 on success and returns -1 and sets `errno` on failure.
*/
int closedir(DIR* __dir);
/**
* [rewinddir(3)](http://man7.org/linux/man-pages/man3/rewinddir.3.html)
* rewinds a directory stream to the first entry.
*/
void rewinddir(DIR* __dir);
/**
* [seekdir(3)](http://man7.org/linux/man-pages/man3/seekdir.3.html)
* seeks a directory stream to the given entry, which must be a value returned
* by telldir().
*
* Available since API level 23.
*/
void seekdir(DIR* __dir, long __location) __INTRODUCED_IN(23);
/**
* [telldir(3)](http://man7.org/linux/man-pages/man3/telldir.3.html)
* returns a value representing the current position in the directory
* for use with seekdir().
*
* Returns the current position on success and returns -1 and sets `errno` on failure.
*
* Available since API level 23.
*/
long telldir(DIR* __dir) __INTRODUCED_IN(23);
/**
* [dirfd(3)](http://man7.org/linux/man-pages/man3/dirfd.3.html)
* returns the file descriptor backing the given directory stream.
*
* Returns a file descriptor on success and returns -1 and sets `errno` on failure.
*/
int dirfd(DIR* __dir);
/**
* [alphasort](http://man7.org/linux/man-pages/man3/alphasort.3.html) is a
* comparator for use with scandir() that uses strcoll().
*/
int alphasort(const struct dirent** __lhs, const struct dirent** __rhs);
/**
* [alphasort64](http://man7.org/linux/man-pages/man3/alphasort.3.html) is a
* comparator for use with scandir64() that uses strcmp().
*
* Available since API level 21.
*/
int alphasort64(const struct dirent64** __lhs, const struct dirent64** __rhs) __INTRODUCED_IN(21);
/**
* [scandir(3)](http://man7.org/linux/man-pages/man3/scandir.3.html)
* scans all the directory `__path`, filtering entries with `__filter` and
* sorting them with qsort() using the given `__comparator`, and storing them
* into `__name_list`. Passing NULL as the filter accepts all entries.
*
* Returns the number of entries returned in the list on success,
* and returns -1 and sets `errno` on failure.
*/
int scandir(const char* __path, struct dirent*** __name_list, int (*__filter)(const struct dirent*), int (*__comparator)(const struct dirent**, const struct dirent**));
/**
* [scandir64(3)](http://man7.org/linux/man-pages/man3/scandir.3.html)
* scans all the directory `__path`, filtering entries with `__filter` and
* sorting them with qsort() using the given `__comparator`, and storing them
* into `__name_list`. Passing NULL as the filter accepts all entries.
*
* Returns the number of entries returned in the list on success,
* and returns -1 and sets `errno` on failure.
*
* Available since API level 21.
*/
int scandir64(const char* __path, struct dirent64*** __name_list, int (*__filter)(const struct dirent64*), int (*__comparator)(const struct dirent64**, const struct dirent64**)) __INTRODUCED_IN(21);
#if defined(__USE_GNU)
/**
* [scandirat64(3)](http://man7.org/linux/man-pages/man3/scandirat.3.html)
* scans all the directory referenced by the pair of `__dir_fd` and `__path`,
* filtering entries with `__filter` and sorting them with qsort() using the
* given `__comparator`, and storing them into `__name_list`. Passing NULL as
* the filter accepts all entries.
*
* Returns the number of entries returned in the list on success,
* and returns -1 and sets `errno` on failure.
*
* Available since API level 24.
*/
int scandirat64(int __dir_fd, const char* __path, struct dirent64*** __name_list, int (*__filter)(const struct dirent64*), int (*__comparator)(const struct dirent64**, const struct dirent64**)) __INTRODUCED_IN(24);
/**
* [scandirat(3)](http://man7.org/linux/man-pages/man3/scandirat.3.html)
* scans all the directory referenced by the pair of `__dir_fd` and `__path`,
* filtering entries with `__filter` and sorting them with qsort() using the
* given `__comparator`, and storing them into `__name_list`. Passing NULL as
* the filter accepts all entries.
*
* Returns the number of entries returned in the list on success,
* and returns -1 and sets `errno` on failure.
*
* Available since API level 24.
*/
int scandirat(int __dir_fd, const char* __path, struct dirent*** __name_list, int (*__filter)(const struct dirent*), int (*__comparator)(const struct dirent**, const struct dirent**)) __INTRODUCED_IN(24);
#endif
__END_DECLS
|