1/* Read a directory. Linux no-LFS version.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <dirent.h>
20
21#if !_DIRENT_MATCHES_DIRENT64
22#include <dirstream.h>
23
24/* Read a directory entry from DIRP. */
25struct dirent *
26__readdir_unlocked (DIR *dirp)
27{
28 struct dirent *dp;
29 int saved_errno = errno;
30
31 do
32 {
33 size_t reclen;
34
35 if (dirp->offset >= dirp->size)
36 {
37 /* We've emptied out our buffer. Refill it. */
38
39 size_t maxread = dirp->allocation;
40 ssize_t bytes;
41
42 bytes = __getdents (dirp->fd, dirp->data, maxread);
43 if (bytes <= 0)
44 {
45 /* On some systems getdents fails with ENOENT when the
46 open directory has been rmdir'd already. POSIX.1
47 requires that we treat this condition like normal EOF. */
48 if (bytes < 0 && errno == ENOENT)
49 bytes = 0;
50
51 /* Don't modifiy errno when reaching EOF. */
52 if (bytes == 0)
53 __set_errno (saved_errno);
54 dp = NULL;
55 break;
56 }
57 dirp->size = (size_t) bytes;
58
59 /* Reset the offset into the buffer. */
60 dirp->offset = 0;
61 }
62
63 dp = (struct dirent *) &dirp->data[dirp->offset];
64
65 reclen = dp->d_reclen;
66
67 dirp->offset += reclen;
68
69 dirp->filepos = dp->d_off;
70
71 /* Skip deleted files. */
72 } while (dp->d_ino == 0);
73
74 return dp;
75}
76
77struct dirent *
78__readdir (DIR *dirp)
79{
80 struct dirent *dp;
81
82#if IS_IN (libc)
83 __libc_lock_lock (dirp->lock);
84#endif
85 dp = __readdir_unlocked (dirp);
86#if IS_IN (libc)
87 __libc_lock_unlock (dirp->lock);
88#endif
89
90 return dp;
91}
92weak_alias (__readdir, readdir)
93
94#endif
95

source code of glibc/sysdeps/unix/sysv/linux/readdir.c