1 /* Copyright (C) 1991,92,93,94,95,96,97,99,2000,02
2 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <sys/types.h>
29 #include <dirstream.h>
32 # define __READDIR __readdir
33 # define __GETDENTS __getdents
34 # define DIRENT_TYPE struct dirent
35 # define __READDIR_ALIAS
38 /* Read a directory entry from DIRP. */
43 int saved_errno
= errno
;
45 __libc_lock_lock (dirp
->lock
);
51 if (dirp
->offset
>= dirp
->size
)
53 /* We've emptied out our buffer. Refill it. */
58 #ifndef _DIRENT_HAVE_D_RECLEN
59 /* Fixed-size struct; must read one at a time (see below). */
62 maxread
= dirp
->allocation
;
65 bytes
= __GETDENTS (dirp
->fd
, dirp
->data
, maxread
);
68 /* On some systems getdents fails with ENOENT when the
69 open directory has been rmdir'd already. POSIX.1
70 requires that we treat this condition like normal EOF. */
71 if (bytes
< 0 && errno
== ENOENT
)
74 /* Don't modifiy errno when reaching EOF. */
76 __set_errno (saved_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 jst 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
;
110 /* Skip deleted files. */
111 } while (dp
->d_ino
== 0);
113 __libc_lock_unlock (dirp
->lock
);
118 #ifdef __READDIR_ALIAS
119 weak_alias (__readdir
, readdir
)