1/* Read a directory in reentrant mode. Linux no-LFS version.
2 Copyright (C) 2018-2024 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/* Read a directory entry from DIRP. */
23int
24__readdir_r (DIR *dirp, struct dirent *entry, struct dirent **result)
25{
26 struct dirent *dp;
27 size_t reclen;
28
29 __libc_lock_lock (dirp->lock);
30
31 while (1)
32 {
33 dp = __readdir_unlocked (dirp);
34 if (dp == NULL)
35 break;
36
37 reclen = dp->d_reclen;
38 if (reclen <= offsetof (struct dirent, d_name) + NAME_MAX + 1)
39 break;
40
41 /* The record is very long. It could still fit into the caller-supplied
42 buffer if we can skip padding at the end. */
43 size_t namelen = _D_EXACT_NAMLEN (dp);
44 if (namelen <= NAME_MAX)
45 {
46 reclen = offsetof (struct dirent, d_name) + namelen + 1;
47 break;
48 }
49
50 /* The name is too long. Ignore this file. */
51 dirp->errcode = ENAMETOOLONG;
52 }
53
54 if (dp != NULL)
55 {
56 *result = memcpy (entry, dp, reclen);
57 entry->d_reclen = reclen;
58 }
59 else
60 *result = NULL;
61
62 __libc_lock_unlock (dirp->lock);
63
64 return dp != NULL ? 0 : dirp->errcode;
65}
66
67weak_alias (__readdir_r, readdir_r)
68#endif /* _DIRENT_MATCHES_DIRENT64 */
69

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