1 /* Copyright (C) 1991-1997,1999,2000,2002,2007 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <sys/types.h>
28 #include <dirstream.h>
31 # define __READDIR __readdir
32 # define __GETDENTS __getdents
33 # define DIRENT_TYPE struct dirent
34 # define __READDIR_ALIAS
37 /* Read a directory entry from DIRP. */
42 int saved_errno
= errno
;
45 __libc_lock_lock (dirp
->lock
);
52 if (dirp
->offset
>= dirp
->size
)
54 /* We've emptied out our buffer. Refill it. */
59 #ifndef _DIRENT_HAVE_D_RECLEN
60 /* Fixed-size struct; must read one at a time (see below). */
63 maxread
= dirp
->allocation
;
66 bytes
= __GETDENTS (dirp
->fd
, dirp
->data
, maxread
);
69 /* On some systems getdents fails with ENOENT when the
70 open directory has been rmdir'd already. POSIX.1
71 requires that we treat this condition like normal EOF. */
72 if (bytes
< 0 && errno
== ENOENT
)
75 /* Don't modifiy errno when reaching EOF. */
77 __set_errno (saved_errno
);
81 dirp
->size
= (size_t) bytes
;
83 /* Reset the offset into the buffer. */
87 dp
= (DIRENT_TYPE
*) &dirp
->data
[dirp
->offset
];
89 #ifdef _DIRENT_HAVE_D_RECLEN
90 reclen
= dp
->d_reclen
;
92 /* The only version of `struct dirent*' that lacks `d_reclen'
94 assert (sizeof dp
->d_name
> 1);
96 /* The name is not terminated if it is the largest possible size.
97 Clobber the following byte to ensure proper null termination. We
98 read jst one entry at a time above so we know that byte will not
100 dp
->d_name
[sizeof dp
->d_name
] = '\0';
103 dirp
->offset
+= reclen
;
105 #ifdef _DIRENT_HAVE_D_OFF
106 dirp
->filepos
= dp
->d_off
;
108 dirp
->filepos
+= reclen
;
111 /* Skip deleted files. */
112 } while (dp
->d_ino
== 0);
115 __libc_lock_unlock (dirp
->lock
);
121 #ifdef __READDIR_ALIAS
122 weak_alias (__readdir
, readdir
)