malloc/Makefile: Split and sort tests
[glibc.git] / sysdeps / unix / sysv / linux / readdir_r.c
blobffd5262cf5a6f8857cb7b7112f0aa8d70a8ede04
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.
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 #include <dirent.h>
21 #if !_DIRENT_MATCHES_DIRENT64
22 /* Read a directory entry from DIRP. */
23 int
24 __readdir_r (DIR *dirp, struct dirent *entry, struct dirent **result)
26 struct dirent *dp;
27 size_t reclen;
29 __libc_lock_lock (dirp->lock);
31 while (1)
33 dp = __readdir_unlocked (dirp);
34 if (dp == NULL)
35 break;
37 reclen = dp->d_reclen;
38 if (reclen <= offsetof (struct dirent, d_name) + NAME_MAX + 1)
39 break;
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)
46 reclen = offsetof (struct dirent, d_name) + namelen + 1;
47 break;
50 /* The name is too long. Ignore this file. */
51 dirp->errcode = ENAMETOOLONG;
54 if (dp != NULL)
56 *result = memcpy (entry, dp, reclen);
57 entry->d_reclen = reclen;
59 else
60 *result = NULL;
62 __libc_lock_unlock (dirp->lock);
64 return dp != NULL ? 0 : dirp->errcode;
67 weak_alias (__readdir_r, readdir_r)
68 #endif /* _DIRENT_MATCHES_DIRENT64 */