1 /* Read a directory. Linux no-LFS version.
2 Copyright (C) 2018-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #if !_DIRENT_MATCHES_DIRENT64
22 #include <dirstream.h>
24 /* Read a directory entry from DIRP. */
26 __readdir_unlocked (DIR *dirp
)
29 int saved_errno
= errno
;
31 if (dirp
->offset
>= dirp
->size
)
33 /* We've emptied out our buffer. Refill it. */
35 size_t maxread
= dirp
->allocation
;
38 bytes
= __getdents (dirp
->fd
, dirp
->data
, maxread
);
41 /* Linux may fail with ENOENT on some file systems if the
42 directory inode is marked as dead (deleted). POSIX
43 treats this as a regular end-of-directory condition, so
44 do not set errno in that case, to indicate success. */
45 if (bytes
== 0 || errno
== ENOENT
)
46 __set_errno (saved_errno
);
49 dirp
->size
= (size_t) bytes
;
51 /* Reset the offset into the buffer. */
55 dp
= (struct dirent
*) &dirp
->data
[dirp
->offset
];
56 dirp
->offset
+= dp
->d_reclen
;
57 dirp
->filepos
= dp
->d_off
;
68 __libc_lock_lock (dirp
->lock
);
70 dp
= __readdir_unlocked (dirp
);
72 __libc_lock_unlock (dirp
->lock
);
77 weak_alias (__readdir
, readdir
)