The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 29 | #pragma once |
| 30 | |
| 31 | /** |
| 32 | * @file dirent.h |
| 33 | * @brief Directory entry iteration. |
| 34 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
| 36 | #include <stdint.h> |
| 37 | #include <sys/cdefs.h> |
Elliott Hughes | 0af3e8f | 2017-07-05 12:34:29 -0700 | [diff] [blame] | 38 | #include <sys/types.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 39 | |
| 40 | __BEGIN_DECLS |
| 41 | |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 42 | /** d_type value when the type is not known. */ |
Elliott Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 43 | #define DT_UNKNOWN 0 |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 44 | /** d_type value for a FIFO. */ |
Elliott Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 45 | #define DT_FIFO 1 |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 46 | /** d_type value for a character device. */ |
Elliott Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 47 | #define DT_CHR 2 |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 48 | /** d_type value for a directory. */ |
Elliott Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 49 | #define DT_DIR 4 |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 50 | /** d_type value for a block device. */ |
Elliott Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 51 | #define DT_BLK 6 |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 52 | /** d_type value for a regular file. */ |
Elliott Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 53 | #define DT_REG 8 |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 54 | /** d_type value for a symbolic link. */ |
Elliott Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 55 | #define DT_LNK 10 |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 56 | /** d_type value for a socket. */ |
Elliott Hughes | 38f0ef3 | 2014-01-08 16:31:36 -0800 | [diff] [blame] | 57 | #define DT_SOCK 12 |
| 58 | #define DT_WHT 14 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 59 | |
Elliott Hughes | 0af3e8f | 2017-07-05 12:34:29 -0700 | [diff] [blame] | 60 | #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 Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 66 | #define __DIRENT64_BODY \ |
Elliott Hughes | 0af3e8f | 2017-07-05 12:34:29 -0700 | [diff] [blame] | 67 | __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 Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 72 | |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 73 | /** The structure returned by readdir(). Identical to dirent64 on Android. */ |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 74 | struct dirent { __DIRENT64_BODY }; |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 75 | /** The structure returned by readdir64(). Identical to dirent on Android. */ |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 76 | struct dirent64 { __DIRENT64_BODY }; |
| 77 | |
Calin Juravle | f963da2 | 2014-05-13 11:01:11 +0100 | [diff] [blame] | 78 | #undef __DIRENT64_BODY |
Elliott Hughes | 0af3e8f | 2017-07-05 12:34:29 -0700 | [diff] [blame] | 79 | #undef __DIRENT64_INO_T |
Calin Juravle | f963da2 | 2014-05-13 11:01:11 +0100 | [diff] [blame] | 80 | |
Elliott Hughes | 8c79b4e | 2014-11-10 14:56:49 -0800 | [diff] [blame] | 81 | /* 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 Hughes | a8a3178 | 2014-01-09 12:37:12 -0800 | [diff] [blame] | 87 | #define d_fileno d_ino |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 89 | /** The structure returned by opendir()/fopendir(). */ |
Elliott Hughes | 063cfb2 | 2012-10-25 20:55:23 -0700 | [diff] [blame] | 90 | typedef struct DIR DIR; |
| 91 | |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 92 | /** |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 98 | DIR* _Nullable opendir(const char* _Nonnull __path); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 99 | |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 106 | DIR* _Nullable fdopendir(int __dir_fd); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 107 | |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 116 | struct dirent* _Nullable readdir(DIR* _Nonnull __dir); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 117 | |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 126 | struct dirent64* _Nullable readdir64(DIR* _Nonnull __dir) __INTRODUCED_IN(21); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 127 | |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 128 | int readdir_r(DIR* _Nonnull __dir, struct dirent* _Nonnull __entry, struct dirent* _Nullable * _Nonnull __buffer) __attribute__((__deprecated__("readdir_r is deprecated; use readdir instead"))); |
| 129 | int readdir64_r(DIR* _Nonnull __dir, struct dirent64* _Nonnull __entry, struct dirent64* _Nullable * _Nonnull __buffer) __INTRODUCED_IN(21) __attribute__((__deprecated__("readdir64_r is deprecated; use readdir64 instead"))); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 130 | |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 137 | int closedir(DIR* _Nonnull __dir); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * [rewinddir(3)](http://man7.org/linux/man-pages/man3/rewinddir.3.html) |
| 141 | * rewinds a directory stream to the first entry. |
| 142 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 143 | void rewinddir(DIR* _Nonnull __dir); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 144 | |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 152 | void seekdir(DIR* _Nonnull __dir, long __location) __INTRODUCED_IN(23); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 153 | |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 163 | long telldir(DIR* _Nonnull __dir) __INTRODUCED_IN(23); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 164 | |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 171 | int dirfd(DIR* _Nonnull __dir); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 172 | |
| 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 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 177 | int alphasort(const struct dirent* _Nonnull * _Nonnull __lhs, const struct dirent* _Nonnull * _Nonnull __rhs); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * [alphasort64](http://man7.org/linux/man-pages/man3/alphasort.3.html) is a |
| 181 | * comparator for use with scandir64() that uses strcmp(). |
| 182 | * |
| 183 | * Available since API level 21. |
| 184 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 185 | int alphasort64(const struct dirent64* _Nonnull * _Nonnull __lhs, const struct dirent64* _Nonnull * _Nonnull __rhs) __INTRODUCED_IN(21); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * [scandir(3)](http://man7.org/linux/man-pages/man3/scandir.3.html) |
| 189 | * scans all the directory `__path`, filtering entries with `__filter` and |
| 190 | * sorting them with qsort() using the given `__comparator`, and storing them |
| 191 | * into `__name_list`. Passing NULL as the filter accepts all entries. |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 192 | * Passing NULL as the comparator skips sorting. |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 193 | * |
| 194 | * Returns the number of entries returned in the list on success, |
| 195 | * and returns -1 and sets `errno` on failure. |
| 196 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 197 | int 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 Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 198 | |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 199 | /** |
| 200 | * [scandir64(3)](http://man7.org/linux/man-pages/man3/scandir.3.html) |
| 201 | * scans all the directory `__path`, filtering entries with `__filter` and |
| 202 | * sorting them with qsort() using the given `__comparator`, and storing them |
| 203 | * into `__name_list`. Passing NULL as the filter accepts all entries. |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 204 | * Passing NULL as the comparator skips sorting. |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 205 | * |
| 206 | * Returns the number of entries returned in the list on success, |
| 207 | * and returns -1 and sets `errno` on failure. |
| 208 | * |
| 209 | * Available since API level 21. |
| 210 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 211 | int 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)) __INTRODUCED_IN(21); |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 212 | |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 213 | #if defined(__USE_GNU) |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * [scandirat64(3)](http://man7.org/linux/man-pages/man3/scandirat.3.html) |
| 217 | * scans all the directory referenced by the pair of `__dir_fd` and `__path`, |
| 218 | * filtering entries with `__filter` and sorting them with qsort() using the |
| 219 | * given `__comparator`, and storing them into `__name_list`. Passing NULL as |
| 220 | * the filter accepts all entries. |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 221 | * Passing NULL as the comparator skips sorting. |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 222 | * |
| 223 | * Returns the number of entries returned in the list on success, |
| 224 | * and returns -1 and sets `errno` on failure. |
| 225 | * |
| 226 | * Available since API level 24. |
| 227 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 228 | int 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 Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * [scandirat(3)](http://man7.org/linux/man-pages/man3/scandirat.3.html) |
| 232 | * scans all the directory referenced by the pair of `__dir_fd` and `__path`, |
| 233 | * filtering entries with `__filter` and sorting them with qsort() using the |
| 234 | * given `__comparator`, and storing them into `__name_list`. Passing NULL as |
| 235 | * the filter accepts all entries. |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 236 | * Passing NULL as the comparator skips sorting. |
Elliott Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 237 | * |
| 238 | * Returns the number of entries returned in the list on success, |
| 239 | * and returns -1 and sets `errno` on failure. |
| 240 | * |
| 241 | * Available since API level 24. |
| 242 | */ |
zijunzhao | 7d2df8b | 2023-03-17 23:15:17 +0000 | [diff] [blame^] | 243 | int 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 Hughes | 02f9f4c | 2020-03-27 16:28:34 -0700 | [diff] [blame] | 244 | |
Elliott Hughes | 6331e80 | 2015-10-27 11:10:36 -0700 | [diff] [blame] | 245 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 246 | |
| 247 | __END_DECLS |