]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/readdir64_r.c
1 /* Copyright (C) 1997, 1998 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
28 #include <dirstream.h>
30 extern ssize_t
__getdirentries64 (int, char *, size_t, off_t
*);
33 /* Read a directory entry from DIRP, store result in ENTRY and return
34 pointer to result in *RESULT. */
36 readdir64_r (DIR *dirp
, struct dirent64
*entry
, struct dirent64
**result
)
41 __libc_lock_lock (dirp
->lock
);
45 if (dirp
->offset
>= dirp
->size
)
47 /* We've emptied out our buffer. Refill it. */
53 #ifndef _DIRENT_HAVE_D_RECLEN
54 /* Fixed-size struct; must read one at a time (see below). */
57 maxread
= dirp
->allocation
;
61 bytes
= __getdirentries64 (dirp
->fd
, dirp
->data
, maxread
, &base
);
65 /* Reclen != 0 signals that an error occurred. */
69 dirp
->size
= (size_t) bytes
;
71 /* Reset the offset into the buffer. */
75 dp
= (struct dirent64
*) &dirp
->data
[dirp
->offset
];
77 #ifdef _DIRENT_HAVE_D_RECLEN
78 reclen
= dp
->d_reclen
;
80 /* The only version of `struct dirent64' that lacks `d_reclen'
82 assert (sizeof dp
->d_name
> 1);
84 /* The name is not terminated if it is the largest possible size.
85 Clobber the following byte to ensure proper null termination. We
86 read just one entry at a time above so we know that byte will not
88 dp
->d_name
[sizeof dp
->d_name
] = '\0';
91 dirp
->offset
+= reclen
;
93 #ifdef _DIRENT_HAVE_D_OFF
94 dirp
->filepos
= dp
->d_off
;
96 dirp
->filepos
+= reclen
;
99 /* Skip deleted files. */
101 while (dp
->d_ino
== 0);
104 *result
= memcpy (entry
, dp
, reclen
);
108 __libc_lock_unlock (dirp
->lock
);
110 return dp
!= NULL
? 0 : reclen
? errno
: 0;