]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/readdir64_r.c
Update.
[thirdparty/glibc.git] / 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.
3
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.
8
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.
13
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. */
18
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <assert.h>
27
28 #include <dirstream.h>
29
30 extern ssize_t __getdirentries64 (int, char *, size_t, off_t *);
31
32
33 /* Read a directory entry from DIRP, store result in ENTRY and return
34 pointer to result in *RESULT. */
35 int
36 readdir64_r (DIR *dirp, struct dirent64 *entry, struct dirent64 **result)
37 {
38 struct dirent64 *dp;
39 size_t reclen;
40
41 __libc_lock_lock (dirp->lock);
42
43 do
44 {
45 if (dirp->offset >= dirp->size)
46 {
47 /* We've emptied out our buffer. Refill it. */
48
49 size_t maxread;
50 off_t base;
51 ssize_t bytes;
52
53 #ifndef _DIRENT_HAVE_D_RECLEN
54 /* Fixed-size struct; must read one at a time (see below). */
55 maxread = sizeof *dp;
56 #else
57 maxread = dirp->allocation;
58 #endif
59
60 base = dirp->filepos;
61 bytes = __getdirentries64 (dirp->fd, dirp->data, maxread, &base);
62 if (bytes <= 0)
63 {
64 dp = NULL;
65 /* Reclen != 0 signals that an error occurred. */
66 reclen = bytes != 0;
67 break;
68 }
69 dirp->size = (size_t) bytes;
70
71 /* Reset the offset into the buffer. */
72 dirp->offset = 0;
73 }
74
75 dp = (struct dirent64 *) &dirp->data[dirp->offset];
76
77 #ifdef _DIRENT_HAVE_D_RECLEN
78 reclen = dp->d_reclen;
79 #else
80 /* The only version of `struct dirent64' that lacks `d_reclen'
81 is fixed-size. */
82 assert (sizeof dp->d_name > 1);
83 reclen = sizeof *dp;
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
87 be used later. */
88 dp->d_name[sizeof dp->d_name] = '\0';
89 #endif
90
91 dirp->offset += reclen;
92
93 #ifdef _DIRENT_HAVE_D_OFF
94 dirp->filepos = dp->d_off;
95 #else
96 dirp->filepos += reclen;
97 #endif
98
99 /* Skip deleted files. */
100 }
101 while (dp->d_ino == 0);
102
103 if (dp != NULL)
104 *result = memcpy (entry, dp, reclen);
105 else
106 *result = NULL;
107
108 __libc_lock_unlock (dirp->lock);
109
110 return dp != NULL ? 0 : reclen ? errno : 0;
111 }