1 /* Copyright (C) 1991-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
24 #include <sys/types.h>
27 #include <dirstream.h>
30 # define __READDIR_R __readdir_r
31 # define __GETDENTS __getdents
32 # define DIRENT_TYPE struct dirent
33 # define __READDIR_R_ALIAS
36 /* Read a directory entry from DIRP. */
38 __READDIR_R (DIR *dirp
, DIRENT_TYPE
*entry
, DIRENT_TYPE
**result
)
42 const int saved_errno
= errno
;
45 __libc_lock_lock (dirp
->lock
);
49 if (dirp
->offset
>= dirp
->size
)
51 /* We've emptied out our buffer. Refill it. */
56 #ifndef _DIRENT_HAVE_D_RECLEN
57 /* Fixed-size struct; must read one at a time (see below). */
60 maxread
= dirp
->allocation
;
63 bytes
= __GETDENTS (dirp
->fd
, dirp
->data
, maxread
);
66 /* On some systems getdents fails with ENOENT when the
67 open directory has been rmdir'd already. POSIX.1
68 requires that we treat this condition like normal EOF. */
69 if (bytes
< 0 && errno
== ENOENT
)
72 __set_errno (saved_errno
);
75 dirp
->errcode
= errno
;
80 dirp
->size
= (size_t) bytes
;
82 /* Reset the offset into the buffer. */
86 dp
= (DIRENT_TYPE
*) &dirp
->data
[dirp
->offset
];
88 #ifdef _DIRENT_HAVE_D_RECLEN
89 reclen
= dp
->d_reclen
;
91 /* The only version of `struct dirent*' that lacks `d_reclen'
93 assert (sizeof dp
->d_name
> 1);
95 /* The name is not terminated if it is the largest possible size.
96 Clobber the following byte to ensure proper null termination. We
97 read just one entry at a time above so we know that byte will not
99 dp
->d_name
[sizeof dp
->d_name
] = '\0';
102 dirp
->offset
+= reclen
;
104 #ifdef _DIRENT_HAVE_D_OFF
105 dirp
->filepos
= dp
->d_off
;
107 dirp
->filepos
+= reclen
;
111 if (reclen
> offsetof (DIRENT_TYPE
, d_name
) + NAME_MAX
+ 1)
113 /* The record is very long. It could still fit into the
114 caller-supplied buffer if we can skip padding at the
116 size_t namelen
= _D_EXACT_NAMLEN (dp
);
117 if (namelen
<= NAME_MAX
)
118 reclen
= offsetof (DIRENT_TYPE
, d_name
) + namelen
+ 1;
121 /* The name is too long. Ignore this file. */
122 dirp
->errcode
= ENAMETOOLONG
;
129 /* Skip deleted and ignored files. */
131 while (dp
->d_ino
== 0);
135 *result
= memcpy (entry
, dp
, reclen
);
136 #ifdef _DIRENT_HAVE_D_RECLEN
137 entry
->d_reclen
= reclen
;
147 __libc_lock_unlock (dirp
->lock
);
152 #ifdef __READDIR_R_ALIAS
153 weak_alias (__readdir_r
, readdir_r
)