blob: 4fcd1cc72b77a8351e99302f5362c1f450249428 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3 *
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5 */
6
7#include <features.h>
8
9#include <errno.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13#include <dirent.h>
14#include "dirstream.h"
15
16
17struct dirent *readdir(DIR * dir)
18{
19 ssize_t bytes;
20 struct dirent *de;
21
22 if (!dir) {
23 __set_errno(EBADF);
24 return NULL;
25 }
26
27 __UCLIBC_MUTEX_LOCK(dir->dd_lock);
28
29 do {
30 if (dir->dd_size <= dir->dd_nextloc) {
31 /* read dir->dd_max bytes of directory entries. */
32 bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
33 if (bytes <= 0) {
34 de = NULL;
35 goto all_done;
36 }
37 dir->dd_size = bytes;
38 dir->dd_nextloc = 0;
39 }
40
41 de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
42
43 /* Am I right? H.J. */
44 dir->dd_nextloc += de->d_reclen;
45
46 /* We have to save the next offset here. */
47 dir->dd_nextoff = de->d_off;
48
49 /* Skip deleted files. */
50 } while (de->d_ino == 0);
51
52all_done:
53 __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
54 return de;
55}
56libc_hidden_def(readdir)