benchtests: Add random() benchmark
[glibc.git] / sysdeps / unix / sysv / linux / readdir64.c
blobe6f5108c0a8093536cdb8fae51d8c6075d426962
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
25 #include <dirent.h>
26 #undef __readdir
27 #undef readdir
29 /* Read a directory entry from DIRP. */
30 struct dirent64 *
31 __readdir64 (DIR *dirp)
33 struct dirent64 *dp;
34 int saved_errno = errno;
36 #if IS_IN (libc)
37 __libc_lock_lock (dirp->lock);
38 #endif
40 if (dirp->offset >= dirp->size)
42 /* We've emptied out our buffer. Refill it. */
44 size_t maxread = dirp->allocation;
45 ssize_t bytes;
47 bytes = __getdents64 (dirp->fd, dirp->data, maxread);
48 if (bytes <= 0)
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);
56 #if IS_IN (libc)
57 __libc_lock_unlock (dirp->lock);
58 #endif
59 return NULL;
61 dirp->size = (size_t) bytes;
63 /* Reset the offset into the buffer. */
64 dirp->offset = 0;
67 dp = (struct dirent64 *) &dirp->data[dirp->offset];
68 dirp->offset += dp->d_reclen;
69 dirp->filepos = dp->d_off;
71 #if IS_IN (libc)
72 __libc_lock_unlock (dirp->lock);
73 #endif
75 return dp;
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)
83 #else
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>
87 # if IS_IN(rtld)
88 weak_alias (__readdir64, readdir64)
89 # else
90 versioned_symbol (libc, __readdir64, readdir64, GLIBC_2_2);
91 # endif
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;
102 #if IS_IN (libc)
103 __libc_lock_lock (dirp->lock);
104 #endif
106 if (dirp->offset >= dirp->size)
108 /* We've emptied out our buffer. Refill it. */
110 size_t maxread = dirp->allocation;
111 ssize_t bytes;
113 bytes = __old_getdents64 (dirp->fd, dirp->data, maxread);
114 if (bytes <= 0)
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);
122 #if IS_IN (libc)
123 __libc_lock_unlock (dirp->lock);
124 #endif
125 return NULL;
127 dirp->size = (size_t) bytes;
129 /* Reset the offset into the buffer. */
130 dirp->offset = 0;
133 dp = (struct __old_dirent64 *) &dirp->data[dirp->offset];
134 dirp->offset += dp->d_reclen;
135 dirp->filepos = dp->d_off;
137 #if IS_IN (libc)
138 __libc_lock_unlock (dirp->lock);
139 #endif
141 return dp;
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 */