blob: 269530561cd301815123b26b302d1a3ec16b0d22 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080028
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070029#pragma once
30
31/**
32 * @file dirent.h
33 * @brief Directory entry iteration.
34 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <sys/cdefs.h>
Elliott Hughes414dd2d2024-10-16 14:48:30 +000037
38#include <stdint.h>
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070039#include <sys/types.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
41__BEGIN_DECLS
42
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070043/** d_type value when the type is not known. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080044#define DT_UNKNOWN 0
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070045/** d_type value for a FIFO. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080046#define DT_FIFO 1
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070047/** d_type value for a character device. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080048#define DT_CHR 2
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070049/** d_type value for a directory. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080050#define DT_DIR 4
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070051/** d_type value for a block device. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080052#define DT_BLK 6
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070053/** d_type value for a regular file. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080054#define DT_REG 8
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070055/** d_type value for a symbolic link. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080056#define DT_LNK 10
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070057/** d_type value for a socket. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080058#define DT_SOCK 12
59#define DT_WHT 14
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070061#if defined(__LP64__)
62#define __DIRENT64_INO_T ino_t
63#else
64#define __DIRENT64_INO_T uint64_t /* Historical accident. */
65#endif
66
Elliott Hughesdb1ea342014-01-17 18:42:49 -080067#define __DIRENT64_BODY \
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070068 __DIRENT64_INO_T d_ino; \
69 off64_t d_off; \
70 unsigned short d_reclen; \
71 unsigned char d_type; \
72 char d_name[256]; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080073
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070074/** The structure returned by readdir(). Identical to dirent64 on Android. */
Elliott Hughesdb1ea342014-01-17 18:42:49 -080075struct dirent { __DIRENT64_BODY };
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070076/** The structure returned by readdir64(). Identical to dirent on Android. */
Elliott Hughesdb1ea342014-01-17 18:42:49 -080077struct dirent64 { __DIRENT64_BODY };
78
Calin Juravlef963da22014-05-13 11:01:11 +010079#undef __DIRENT64_BODY
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070080#undef __DIRENT64_INO_T
Calin Juravlef963da22014-05-13 11:01:11 +010081
Elliott Hughes8c79b4e2014-11-10 14:56:49 -080082/* glibc compatibility. */
83#undef _DIRENT_HAVE_D_NAMLEN /* Linux doesn't have a d_namlen field. */
84#define _DIRENT_HAVE_D_RECLEN
85#define _DIRENT_HAVE_D_OFF
86#define _DIRENT_HAVE_D_TYPE
87
Elliott Hughesa8a31782014-01-09 12:37:12 -080088#define d_fileno d_ino
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070090/** The structure returned by opendir()/fopendir(). */
Elliott Hughes063cfb22012-10-25 20:55:23 -070091typedef struct DIR DIR;
92
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070093/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000094 * [opendir(3)](https://man7.org/linux/man-pages/man3/opendir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070095 * opens a directory stream for the directory at `__path`.
96 *
97 * Returns null and sets `errno` on failure.
98 */
zijunzhao7d2df8b2023-03-17 23:15:17 +000099DIR* _Nullable opendir(const char* _Nonnull __path);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700100
101/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000102 * [fopendir(3)](https://man7.org/linux/man-pages/man3/opendir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700103 * opens a directory stream for the directory at `__dir_fd`.
104 *
105 * Returns null and sets `errno` on failure.
106 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000107DIR* _Nullable fdopendir(int __dir_fd);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700108
109/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000110 * [readdir(3)](https://man7.org/linux/man-pages/man3/readdir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700111 * returns the next directory entry in the given directory.
112 *
113 * Returns a pointer to a directory entry on success,
114 * or returns null and leaves `errno` unchanged at the end of the directory,
115 * or returns null and sets `errno` on failure.
116 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000117struct dirent* _Nullable readdir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700118
119/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000120 * [readdir64(3)](https://man7.org/linux/man-pages/man3/readdir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700121 * returns the next directory entry in the given directory.
122 *
123 * Returns a pointer to a directory entry on success,
124 * or returns null and leaves `errno` unchanged at the end of the directory,
125 * or returns null and sets `errno` on failure.
126 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700127struct dirent64* _Nullable readdir64(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700128
zijunzhao7d2df8b2023-03-17 23:15:17 +0000129int readdir_r(DIR* _Nonnull __dir, struct dirent* _Nonnull __entry, struct dirent* _Nullable * _Nonnull __buffer) __attribute__((__deprecated__("readdir_r is deprecated; use readdir instead")));
Elliott Hughes655e4302023-06-16 12:39:33 -0700130int readdir64_r(DIR* _Nonnull __dir, struct dirent64* _Nonnull __entry, struct dirent64* _Nullable * _Nonnull __buffer) __attribute__((__deprecated__("readdir64_r is deprecated; use readdir64 instead")));
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700131
132/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000133 * [closedir(3)](https://man7.org/linux/man-pages/man3/closedir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700134 * closes a directory stream.
135 *
136 * Returns 0 on success and returns -1 and sets `errno` on failure.
137 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000138int closedir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700139
140/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000141 * [rewinddir(3)](https://man7.org/linux/man-pages/man3/rewinddir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700142 * rewinds a directory stream to the first entry.
143 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000144void rewinddir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700145
146/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000147 * [seekdir(3)](https://man7.org/linux/man-pages/man3/seekdir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700148 * seeks a directory stream to the given entry, which must be a value returned
149 * by telldir().
150 *
151 * Available since API level 23.
152 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000153void seekdir(DIR* _Nonnull __dir, long __location) __INTRODUCED_IN(23);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700154
155/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000156 * [telldir(3)](https://man7.org/linux/man-pages/man3/telldir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700157 * returns a value representing the current position in the directory
158 * for use with seekdir().
159 *
160 * Returns the current position on success and returns -1 and sets `errno` on failure.
161 *
162 * Available since API level 23.
163 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000164long telldir(DIR* _Nonnull __dir) __INTRODUCED_IN(23);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700165
166/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000167 * [dirfd(3)](https://man7.org/linux/man-pages/man3/dirfd.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700168 * returns the file descriptor backing the given directory stream.
169 *
170 * Returns a file descriptor on success and returns -1 and sets `errno` on failure.
171 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000172int dirfd(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700173
174/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000175 * [alphasort](https://man7.org/linux/man-pages/man3/alphasort.3.html) is a
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700176 * comparator for use with scandir() that uses strcoll().
177 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000178int alphasort(const struct dirent* _Nonnull * _Nonnull __lhs, const struct dirent* _Nonnull * _Nonnull __rhs);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700179
180/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000181 * [alphasort64](https://man7.org/linux/man-pages/man3/alphasort.3.html) is a
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700182 * comparator for use with scandir64() that uses strcmp().
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700183 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700184int alphasort64(const struct dirent64* _Nonnull * _Nonnull __lhs, const struct dirent64* _Nonnull * _Nonnull __rhs);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700185
186/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000187 * [scandir(3)](https://man7.org/linux/man-pages/man3/scandir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700188 * scans all the directory `__path`, filtering entries with `__filter` and
189 * sorting them with qsort() using the given `__comparator`, and storing them
190 * into `__name_list`. Passing NULL as the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000191 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700192 *
193 * Returns the number of entries returned in the list on success,
194 * and returns -1 and sets `errno` on failure.
195 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000196int scandir(const char* _Nonnull __path, struct dirent* _Nonnull * _Nonnull * _Nonnull __name_list, int (* _Nullable __filter)(const struct dirent* _Nonnull), int (* _Nullable __comparator)(const struct dirent* _Nonnull * _Nonnull, const struct dirent* _Nonnull * _Nonnull));
Elliott Hughes6331e802015-10-27 11:10:36 -0700197
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700198/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000199 * [scandir64(3)](https://man7.org/linux/man-pages/man3/scandir.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700200 * scans all the directory `__path`, filtering entries with `__filter` and
201 * sorting them with qsort() using the given `__comparator`, and storing them
202 * into `__name_list`. Passing NULL as the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000203 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700204 *
205 * Returns the number of entries returned in the list on success,
206 * and returns -1 and sets `errno` on failure.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700207 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700208int scandir64(const char* _Nonnull __path, struct dirent64* _Nonnull * _Nonnull * _Nonnull __name_list, int (* _Nullable __filter)(const struct dirent64* _Nonnull), int (* _Nullable __comparator)(const struct dirent64* _Nonnull * _Nonnull, const struct dirent64* _Nonnull * _Nonnull));
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700209
Elliott Hughes6331e802015-10-27 11:10:36 -0700210#if defined(__USE_GNU)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700211
212/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000213 * [scandirat64(3)](https://man7.org/linux/man-pages/man3/scandirat.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700214 * scans all the directory referenced by the pair of `__dir_fd` and `__path`,
215 * filtering entries with `__filter` and sorting them with qsort() using the
216 * given `__comparator`, and storing them into `__name_list`. Passing NULL as
217 * the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000218 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700219 *
220 * Returns the number of entries returned in the list on success,
221 * and returns -1 and sets `errno` on failure.
222 *
223 * Available since API level 24.
224 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000225int scandirat64(int __dir_fd, const char* _Nonnull __path, struct dirent64* _Nonnull * _Nonnull * _Nonnull __name_list, int (* _Nullable __filter)(const struct dirent64* _Nonnull), int (* _Nullable __comparator)(const struct dirent64* _Nonnull * _Nonnull, const struct dirent64* _Nonnull * _Nonnull)) __INTRODUCED_IN(24);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700226
227/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000228 * [scandirat(3)](https://man7.org/linux/man-pages/man3/scandirat.3.html)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700229 * scans all the directory referenced by the pair of `__dir_fd` and `__path`,
230 * filtering entries with `__filter` and sorting them with qsort() using the
231 * given `__comparator`, and storing them into `__name_list`. Passing NULL as
232 * the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000233 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700234 *
235 * Returns the number of entries returned in the list on success,
236 * and returns -1 and sets `errno` on failure.
237 *
238 * Available since API level 24.
239 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000240int scandirat(int __dir_fd, const char* _Nonnull __path, struct dirent* _Nonnull * _Nonnull * _Nonnull __name_list, int (* _Nullable __filter)(const struct dirent* _Nonnull), int (* _Nullable __comparator)(const struct dirent* _Nonnull * _Nonnull, const struct dirent* _Nonnull * _Nonnull)) __INTRODUCED_IN(24);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700241
Elliott Hughes6331e802015-10-27 11:10:36 -0700242#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243
244__END_DECLS