1 /* Read a directory. Linux LFS version.
2 Copyright (C) 1997-2024 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/>. */
19 /* When _DIRENT_MATCHES_DIRENT64 is defined we can alias 'readdir64' to
20 'readdir'. However the function signatures are not equal due
21 different return types, so we need to suppress {__}readdir so weak
22 and strong alias do not throw conflicting types errors. */
23 #define readdir __no_readdir_decl
24 #define __readdir __no___readdir_decl
29 /* Read a directory entry from DIRP. */
31 __readdir64 (DIR *dirp
)
34 int saved_errno
= errno
;
37 __libc_lock_lock (dirp
->lock
);
40 if (dirp
->offset
>= dirp
->size
)
42 /* We've emptied out our buffer. Refill it. */
44 size_t maxread
= dirp
->allocation
;
47 bytes
= __getdents64 (dirp
->fd
, dirp
->data
, maxread
);
50 /* Linux may fail with ENOENT on some file systems if the
51 directory inode is marked as dead (deleted). POSIX
52 treats this as a regular end-of-directory condition, so
53 do not set errno in that case, to indicate success. */
54 if (bytes
== 0 || errno
== ENOENT
)
55 __set_errno (saved_errno
);
57 __libc_lock_unlock (dirp
->lock
);
61 dirp
->size
= (size_t) bytes
;
63 /* Reset the offset into the buffer. */
67 dp
= (struct dirent64
*) &dirp
->data
[dirp
->offset
];
68 dirp
->offset
+= dp
->d_reclen
;
69 dirp
->filepos
= dp
->d_off
;
72 __libc_lock_unlock (dirp
->lock
);
77 libc_hidden_def (__readdir64
)
79 #if _DIRENT_MATCHES_DIRENT64
80 strong_alias (__readdir64
, __readdir
)
81 weak_alias (__readdir64
, readdir64
)
82 weak_alias (__readdir64
, readdir
)
84 /* The compat code expects the 'struct direct' with d_ino being a __ino_t
85 instead of __ino64_t. */
86 # include <shlib-compat.h>
88 weak_alias (__readdir64
, readdir64
)
90 versioned_symbol (libc
, __readdir64
, readdir64
, GLIBC_2_2
);
92 # if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2)
93 # include <olddirent.h>
95 attribute_compat_text_section
96 struct __old_dirent64
*
97 __old_readdir64 (DIR *dirp
)
99 struct __old_dirent64
*dp
;
100 int saved_errno
= errno
;
103 __libc_lock_lock (dirp
->lock
);
106 if (dirp
->offset
>= dirp
->size
)
108 /* We've emptied out our buffer. Refill it. */
110 size_t maxread
= dirp
->allocation
;
113 bytes
= __old_getdents64 (dirp
->fd
, dirp
->data
, maxread
);
116 /* Linux may fail with ENOENT on some file systems if the
117 directory inode is marked as dead (deleted). POSIX
118 treats this as a regular end-of-directory condition, so
119 do not set errno in that case, to indicate success. */
120 if (bytes
== 0 || errno
== ENOENT
)
121 __set_errno (saved_errno
);
123 __libc_lock_unlock (dirp
->lock
);
127 dirp
->size
= (size_t) bytes
;
129 /* Reset the offset into the buffer. */
133 dp
= (struct __old_dirent64
*) &dirp
->data
[dirp
->offset
];
134 dirp
->offset
+= dp
->d_reclen
;
135 dirp
->filepos
= dp
->d_off
;
138 __libc_lock_unlock (dirp
->lock
);
143 libc_hidden_def (__old_readdir64
)
144 compat_symbol (libc
, __old_readdir64
, readdir64
, GLIBC_2_1
);
145 # endif /* SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) */
146 #endif /* _DIRENT_MATCHES_DIRENT64 */