1 /* Read a directory in reentrant mode. 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 /* Read a directory entry from DIRP. */
24 __readdir_r (DIR *dirp
, struct dirent
*entry
, struct dirent
**result
)
29 __libc_lock_lock (dirp
->lock
);
33 dp
= __readdir_unlocked (dirp
);
37 reclen
= dp
->d_reclen
;
38 if (reclen
<= offsetof (struct dirent
, d_name
) + NAME_MAX
+ 1)
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;
50 /* The name is too long. Ignore this file. */
51 dirp
->errcode
= ENAMETOOLONG
;
56 *result
= memcpy (entry
, dp
, reclen
);
57 entry
->d_reclen
= reclen
;
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 */