• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @defgroup dirent Dirent
3  * @ingroup libc
4  */
5 
6 #ifndef	_DIRENT_H
7 #define	_DIRENT_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #include <features.h>
14 
15 #define __NEED_ino_t
16 #define __NEED_off_t
17 #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
18 #define __NEED_size_t
19 #endif
20 #ifdef __LITEOS__
21 #define __NEED_int64_t
22 #endif
23 
24 #include <bits/alltypes.h>
25 
26 #include <bits/dirent.h>
27 
28 #ifndef __LITEOS__
29 typedef struct __dirstream DIR;
30 #else
31 typedef struct DIR DIR;
32 #endif
33 
34 #define d_fileno d_ino
35 
36 /**
37  * @ingroup dirent
38  *
39  * @par Description:
40  * The closedir() function shall close the directory stream referred to by the argument dirp.
41  *
42  * @param  dirp [IN] An instance of type DIR created by a previous call to opendir().
43  *
44  * @attention
45  * <ul>
46  * <li>None.</li>
47  * </ul>
48  *
49  * @retval #0  Close directory successful.
50  * @retval #-1 An error is encountered and close the directory failed.
51  *
52  * @par Errors
53  * <ul>
54  * <li><b>EBADF</b>: The dirp argument does not refer to an open directory stream.</li>
55  * <li><b>EEXIST</b> : The 1st member in the FIL/DIR object is invalid of fat.</li>
56  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
57  * <li><b>ENOENT</b>: The internal node pointed to by DIR doesn't exist in jffs2 and romfs.</li>
58  * <li><b>EPERM</b>: Other errors.</li>
59  * </ul>
60  *
61  * @par Dependency:
62  * <ul><li>dirent.h</li></ul>
63  * @see None
64  */
65 int            closedir(DIR *);
66 DIR           *fdopendir(int);
67 
68 /**
69  * @ingroup dirent
70  *
71  * @par Description:
72  * The opendir() function shall open a directory stream corresponding to the directory named by the path argument.
73  * The directory stream is positioned at the first entry. If the type DIR is implemented using a file descriptor,
74  * applications shall only be able to open up to a total of {OPEN_MAX} files and directories.
75  *
76  * @param  path [IN] path of the diretory which need to open.
77  *
78  * @attention
79  * <ul>
80  * <li>None.</li>
81  * </ul>
82  *
83  * @retval #DIR* Upon successful completion, these functions shall return a pointer to an object of type DIR.
84  * @retval #NULL An error is encountered and open the directory failed.
85  *
86  * @par Errors
87  * <ul>
88  * <li><b>EINVAL</b>: The path is an empty string.</li>
89  * <li><b>ENAMETOOLONG</b>: The length of a component of a pathname is longer than {NAME_MAX}.</li>
90  * <li><b>ENOENT</b>: A component of the path does not exist.</li>
91  * <li><b>ENOMEM</b>: Out of memory.</li>
92  * <li><b>ENOTDIR</b>: A component of the path is not a directory.</li>
93  * <li><b>EMFILE</b>: Too many open file.</li>
94  * <li><b>EROFS</b>: The physical drive is write protected with fat filesystem.</li>
95  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
96  * <li><b>ENODEV</b>: The mount is not healthy.</li>
97  * <li><b>EPERM</b>: Other errors.</li>
98  * </ul>
99  *
100  * @par Dependency:
101  * <ul><li>dirent.h</li></ul>
102  * @see open | readdir | rewinddir | closedir
103  */
104 DIR           *opendir(const char *);
105 
106 /**
107  * @ingroup dirent
108  *
109  * @par Description:
110  * The readdir() function shall return a pointer to a structure representing the directory entry
111  * at the current position in the directory stream specified by the argument dirp,
112  * and position the directory stream at the next entry.
113  * It shall return a null pointer upon reaching the end of the directory stream.
114  *
115  * @param  dirp [IN] a directory stream of current directory.
116  *
117  * @attention
118  * <ul>
119  * <li>None.</li>
120  * </ul>
121  *
122  * @retval "struct dirent*"  Upon successful completion, readdir() shall return a pointer to
123  *                           an object of type struct dirent.
124  * @retval NULL              An error is encountered or the end of the directory is encountered.
125  *
126  * @par Errors
127  * <ul>
128  * <li><b>EINVAL</b>: dirp argument is invalid.</li>
129  * <li><b>ENAMETOOLONG</b>: The length of the pathname of the current directory entry is longer than {NAME_MAX}.</li>
130  * <li><b>ENOENT</b>: The current position of the directory stream is invalid.</li>
131  * <li><b>ENOMEM</b>: Out of memory.</li>
132  * <li><b>ENOSPC</b>: No free cluster of fat filesystem.</li>
133  * <li><b>EBADF</b>: The dirp argument does not refer to an open directory stream.</li>
134  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
135  * <li><b>ENODEV</b>: The mount is not healthy.</li>
136  * <li><b>EACCES</b>: The current file system doesn't support readdir.</li>
137  * <li><b>EPERM</b>: Other errors.</li>
138  * </ul>
139  *
140  * @par Dependency:
141  * <ul><li>dirent.h</li></ul>
142  * @see closedir | rewinddir
143  */
144 struct dirent *readdir(DIR *);
145 int            readdir_r(DIR *__restrict, struct dirent *__restrict, struct dirent **__restrict);
146 
147 /**
148  * @ingroup dirent
149  *
150  * @par Description:
151  * The rewinddir() function shall reset the position of the directory stream to
152  * which dirp refers to the beginning of the directory.
153  *
154  * @param  dirp [IN] An instance of type DIR created by a previous call to opendir().
155  *
156  * @attention
157  * <ul>
158  * <li>None.</li>
159  * </ul>
160  *
161  * @retval #void None.
162  *
163  * @par Errors
164  * <ul>
165  * <li><b>EBADF</b>: The dirp argument does not refer to an open directory stream.</li>
166  * </ul>
167  *
168  * @par Dependency:
169  * <ul><li>dirent.h</li></ul>
170  * @see  readdir | closedir
171  */
172 void           rewinddir(DIR *);
173 int            dirfd(DIR *);
174 
175 /**
176  * @ingroup dirent
177  *
178  * @par Description:
179  * The alphasort() function can be used as the comparison function for the scandir() function
180  * to sort the directory entries, d1 and d2, into alphabetical order.
181  *
182  * @attention
183  * <ul>
184  * <li>None.</li>
185  * </ul>
186  *
187  * @retval #int Upon successful completion, the alphasort() function shall return an integer greater than,
188  * equal to, or less than 0, according to whether the name of the directory entry pointed to by d1
189  * is lexically greater than, equal to, or less than the directory pointed to by d2 when both are interpreted
190  * as appropriate to the current locale. There is no return value reserved to indicate an error.
191  *
192  * @par Dependency:
193  * <ul><li>dirent.h</li></ul>
194  * @see  strcoll
195  */
196 int alphasort(const struct dirent **, const struct dirent **);
197 
198 /**
199  * @ingroup dirent
200  *
201  * @par Description:
202  * The scandir() function shall scan the directory dir, calling the function referenced by filter on each directory
203  * entry. Entries for which the function referenced by filter returns non-zero shall be stored in strings allocated
204  * as if by a call to malloc(), and sorted as if by a call to qsort() with the comparison function compar,
205  * except that compar need not provide total ordering. The strings are collected in array namelist
206  * which shall be allocated as if by a call to malloc(). If filter is a null pointer, all entries shall be selected.
207  * If the comparison function compar does not provide total ordering, the order in which the directory entries
208  * are stored is unspecified.
209  *
210  * @param  dir      [IN]  The path of the directory need to scan.
211  * @param  namelist [OUT] Return the pointer of type struct dirent* to store the dirent scanned in the directory.
212  * @param  filter   [IN]  Refer to the function to select the entries.
213  * @param  compar   [IN]  The comparison function used in qsort() to sort the entries.
214  *
215  * @attention
216  * <ul>
217  * <li>None.</li>
218  * </ul>
219  *
220  * @retval #int the number of entries in the array and a pointer to the array through the parameter namelist.
221  * @retval #-1 An error is encountered.
222  *
223  * @par Errors
224  * <ul>
225  * <li><b>EINVAL</b>: The path is an empty string.</li>
226  * <li><b>ENAMETOOLONG</b>: The length of a component of a pathname is longer than {NAME_MAX}.</li>
227  * <li><b>ENOENT</b>: A component of the path does not exist.</li>
228  * <li><b>ENOMEM</b>: Out of memory.</li>
229  * <li><b>ENOSPC</b>: Out of memory in ramfs filesystem.</li>
230  * <li><b>ENOTDIR</b>: A component of the path is not a directory.</li>
231  * <li><b>EMFILE</b>: Too many open file.</li>
232  * <li><b>EROFS</b>: The physical drive is write protected with fat filesystem.</li>
233  * <li><b>EIO</b>: A hard error occurred in the low level disk I/O layer or the physical drive cannot work.</li>
234  * <li><b>ENODEV</b>: The mount is not healthy.</li>
235  * <li><b>EPERM</b>: Other errors.</li>
236  * </ul>
237  *
238  * @par Dependency:
239  * <ul><li>dirent.h</li></ul>
240  * @see  qsort
241  */
242 int scandir(const char *, struct dirent ***, int (*)(const struct dirent *), int (*)(const struct dirent **, const struct dirent **));
243 
244 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
245 /**
246  * @ingroup dirent
247  *
248  * @par Description:
249  * The seekdir() function shall set the position of the next readdir() operation on the directory stream specified
250  * by dirp to the position specified by offset. The value of offset should have been returned from
251  * an earlier call to telldir() using the same directory stream.
252  *
253  * @param  dirp   [IN] An instance of type DIR created by a previous call to opendir().
254  * @param  offset [IN] offset to seek to
255  *
256  * @attention
257  * <ul>
258  * <li>None.</li>
259  * </ul>
260  *
261  * @retval #void None.
262  *
263  * @par Errors
264  * <ul>
265  * <li><b>EBADF</b>: The dirp argument does not refer to an open directory stream.</li>
266  * <li><b>EINVAL</b>: offset is less than zero.</li>
267  * </ul>
268  *
269  * @par Dependency:
270  * <ul><li>dirent.h</li></ul>
271  * @see  readdir | telldir
272  */
273 void           seekdir(DIR *, long);
274 
275 /**
276  * @ingroup dirent
277  *
278  * @par Description:
279  * The telldir() function shall obtain the current location associated with the directory stream specified by dirp.
280  *
281  * @param  dirp [IN] An instance of type DIR created by a previous call to opendir().
282  *
283  * @attention
284  * <ul>
285  * <li>None.</li>
286  * </ul>
287  *
288  * @retval #long Upon successful completion, telldir() shall return the current location
289  * of the specified directory stream.
290  *
291  * @par Errors
292  * <ul>
293  * <li><b>EBADF</b>: The dirp argument does not refer to an open directory stream.</li>
294  * </ul>
295  *
296  * @par Dependency:
297  * <ul><li>dirent.h</li></ul>
298  * @see  readdir | seekdir
299  */
300 long           telldir(DIR *);
301 #endif
302 
303 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
304 #define DT_UNKNOWN 0
305 #define DT_FIFO 1
306 #define DT_CHR 2
307 #define DT_DIR 4
308 #define DT_BLK 6
309 #define DT_REG 8
310 #define DT_LNK 10
311 #define DT_SOCK 12
312 #define DT_WHT 14
313 #define IFTODT(x) ((x)>>12 & 017)
314 #define DTTOIF(x) ((x)<<12)
315 int getdents(int, struct dirent *, size_t);
316 #endif
317 
318 #ifdef _GNU_SOURCE
319 int versionsort(const struct dirent **, const struct dirent **);
320 #endif
321 
322 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
323 #define dirent64 dirent
324 #define readdir64 readdir
325 #define readdir64_r readdir_r
326 #define scandir64 scandir
327 #define alphasort64 alphasort
328 #define versionsort64 versionsort
329 #ifdef __LITEOS__
330 #define off64_t int64_t
331 #else
332 #define off64_t off_t
333 #endif
334 #define ino64_t ino_t
335 #define getdents64 getdents
336 #endif
337 
338 #ifdef __cplusplus
339 }
340 #endif
341 
342 #endif
343