blob: 4f5d0fba9ce1c1dec81a5aef708adc7f1c252fb8 [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
36#include <stdint.h>
37#include <sys/cdefs.h>
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070038#include <sys/types.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039
40__BEGIN_DECLS
41
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070042/** d_type value when the type is not known. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080043#define DT_UNKNOWN 0
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070044/** d_type value for a FIFO. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080045#define DT_FIFO 1
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070046/** d_type value for a character device. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080047#define DT_CHR 2
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070048/** d_type value for a directory. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080049#define DT_DIR 4
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070050/** d_type value for a block device. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080051#define DT_BLK 6
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070052/** d_type value for a regular file. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080053#define DT_REG 8
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070054/** d_type value for a symbolic link. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080055#define DT_LNK 10
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070056/** d_type value for a socket. */
Elliott Hughes38f0ef32014-01-08 16:31:36 -080057#define DT_SOCK 12
58#define DT_WHT 14
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070060#if defined(__LP64__)
61#define __DIRENT64_INO_T ino_t
62#else
63#define __DIRENT64_INO_T uint64_t /* Historical accident. */
64#endif
65
Elliott Hughesdb1ea342014-01-17 18:42:49 -080066#define __DIRENT64_BODY \
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070067 __DIRENT64_INO_T d_ino; \
68 off64_t d_off; \
69 unsigned short d_reclen; \
70 unsigned char d_type; \
71 char d_name[256]; \
Elliott Hughesdb1ea342014-01-17 18:42:49 -080072
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070073/** The structure returned by readdir(). Identical to dirent64 on Android. */
Elliott Hughesdb1ea342014-01-17 18:42:49 -080074struct dirent { __DIRENT64_BODY };
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070075/** The structure returned by readdir64(). Identical to dirent on Android. */
Elliott Hughesdb1ea342014-01-17 18:42:49 -080076struct dirent64 { __DIRENT64_BODY };
77
Calin Juravlef963da22014-05-13 11:01:11 +010078#undef __DIRENT64_BODY
Elliott Hughes0af3e8f2017-07-05 12:34:29 -070079#undef __DIRENT64_INO_T
Calin Juravlef963da22014-05-13 11:01:11 +010080
Elliott Hughes8c79b4e2014-11-10 14:56:49 -080081/* glibc compatibility. */
82#undef _DIRENT_HAVE_D_NAMLEN /* Linux doesn't have a d_namlen field. */
83#define _DIRENT_HAVE_D_RECLEN
84#define _DIRENT_HAVE_D_OFF
85#define _DIRENT_HAVE_D_TYPE
86
Elliott Hughesa8a31782014-01-09 12:37:12 -080087#define d_fileno d_ino
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070089/** The structure returned by opendir()/fopendir(). */
Elliott Hughes063cfb22012-10-25 20:55:23 -070090typedef struct DIR DIR;
91
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070092/**
93 * [opendir(3)](http://man7.org/linux/man-pages/man3/opendir.3.html)
94 * opens a directory stream for the directory at `__path`.
95 *
96 * Returns null and sets `errno` on failure.
97 */
zijunzhao7d2df8b2023-03-17 23:15:17 +000098DIR* _Nullable opendir(const char* _Nonnull __path);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -070099
100/**
101 * [fopendir(3)](http://man7.org/linux/man-pages/man3/opendir.3.html)
102 * opens a directory stream for the directory at `__dir_fd`.
103 *
104 * Returns null and sets `errno` on failure.
105 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000106DIR* _Nullable fdopendir(int __dir_fd);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700107
108/**
109 * [readdir(3)](http://man7.org/linux/man-pages/man3/readdir.3.html)
110 * returns the next directory entry in the given directory.
111 *
112 * Returns a pointer to a directory entry on success,
113 * or returns null and leaves `errno` unchanged at the end of the directory,
114 * or returns null and sets `errno` on failure.
115 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000116struct dirent* _Nullable readdir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700117
118/**
119 * [readdir64(3)](http://man7.org/linux/man-pages/man3/readdir.3.html)
120 * returns the next directory entry in the given directory.
121 *
122 * Returns a pointer to a directory entry on success,
123 * or returns null and leaves `errno` unchanged at the end of the directory,
124 * or returns null and sets `errno` on failure.
125 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700126struct dirent64* _Nullable readdir64(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700127
zijunzhao7d2df8b2023-03-17 23:15:17 +0000128int 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 -0700129int 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 -0700130
131/**
132 * [closedir(3)](http://man7.org/linux/man-pages/man3/closedir.3.html)
133 * closes a directory stream.
134 *
135 * Returns 0 on success and returns -1 and sets `errno` on failure.
136 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000137int closedir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700138
139/**
140 * [rewinddir(3)](http://man7.org/linux/man-pages/man3/rewinddir.3.html)
141 * rewinds a directory stream to the first entry.
142 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000143void rewinddir(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700144
145/**
146 * [seekdir(3)](http://man7.org/linux/man-pages/man3/seekdir.3.html)
147 * seeks a directory stream to the given entry, which must be a value returned
148 * by telldir().
149 *
150 * Available since API level 23.
151 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000152void seekdir(DIR* _Nonnull __dir, long __location) __INTRODUCED_IN(23);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700153
154/**
155 * [telldir(3)](http://man7.org/linux/man-pages/man3/telldir.3.html)
156 * returns a value representing the current position in the directory
157 * for use with seekdir().
158 *
159 * Returns the current position on success and returns -1 and sets `errno` on failure.
160 *
161 * Available since API level 23.
162 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000163long telldir(DIR* _Nonnull __dir) __INTRODUCED_IN(23);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700164
165/**
166 * [dirfd(3)](http://man7.org/linux/man-pages/man3/dirfd.3.html)
167 * returns the file descriptor backing the given directory stream.
168 *
169 * Returns a file descriptor on success and returns -1 and sets `errno` on failure.
170 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000171int dirfd(DIR* _Nonnull __dir);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700172
173/**
174 * [alphasort](http://man7.org/linux/man-pages/man3/alphasort.3.html) is a
175 * comparator for use with scandir() that uses strcoll().
176 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000177int alphasort(const struct dirent* _Nonnull * _Nonnull __lhs, const struct dirent* _Nonnull * _Nonnull __rhs);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700178
179/**
180 * [alphasort64](http://man7.org/linux/man-pages/man3/alphasort.3.html) is a
181 * comparator for use with scandir64() that uses strcmp().
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700182 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700183int alphasort64(const struct dirent64* _Nonnull * _Nonnull __lhs, const struct dirent64* _Nonnull * _Nonnull __rhs);
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700184
185/**
186 * [scandir(3)](http://man7.org/linux/man-pages/man3/scandir.3.html)
187 * scans all the directory `__path`, filtering entries with `__filter` and
188 * sorting them with qsort() using the given `__comparator`, and storing them
189 * into `__name_list`. Passing NULL as the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000190 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700191 *
192 * Returns the number of entries returned in the list on success,
193 * and returns -1 and sets `errno` on failure.
194 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000195int 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 -0700196
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700197/**
198 * [scandir64(3)](http://man7.org/linux/man-pages/man3/scandir.3.html)
199 * scans all the directory `__path`, filtering entries with `__filter` and
200 * sorting them with qsort() using the given `__comparator`, and storing them
201 * into `__name_list`. Passing NULL as the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000202 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700203 *
204 * Returns the number of entries returned in the list on success,
205 * and returns -1 and sets `errno` on failure.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700206 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700207int 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 -0700208
Elliott Hughes6331e802015-10-27 11:10:36 -0700209#if defined(__USE_GNU)
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700210
211/**
212 * [scandirat64(3)](http://man7.org/linux/man-pages/man3/scandirat.3.html)
213 * scans all the directory referenced by the pair of `__dir_fd` and `__path`,
214 * filtering entries with `__filter` and sorting them with qsort() using the
215 * given `__comparator`, and storing them into `__name_list`. Passing NULL as
216 * the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000217 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700218 *
219 * Returns the number of entries returned in the list on success,
220 * and returns -1 and sets `errno` on failure.
221 *
222 * Available since API level 24.
223 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000224int 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 -0700225
226/**
227 * [scandirat(3)](http://man7.org/linux/man-pages/man3/scandirat.3.html)
228 * scans all the directory referenced by the pair of `__dir_fd` and `__path`,
229 * filtering entries with `__filter` and sorting them with qsort() using the
230 * given `__comparator`, and storing them into `__name_list`. Passing NULL as
231 * the filter accepts all entries.
zijunzhao7d2df8b2023-03-17 23:15:17 +0000232 * Passing NULL as the comparator skips sorting.
Elliott Hughes02f9f4c2020-03-27 16:28:34 -0700233 *
234 * Returns the number of entries returned in the list on success,
235 * and returns -1 and sets `errno` on failure.
236 *
237 * Available since API level 24.
238 */
zijunzhao7d2df8b2023-03-17 23:15:17 +0000239int 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 -0700240
Elliott Hughes6331e802015-10-27 11:10:36 -0700241#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242
243__END_DECLS