]> sourceware.org Git - glibc.git/blob - sysdeps/unix/sysv/linux/readdir64_r.c
57ae4c786ef4711aae5856f6e036f6cf8814ce66
[glibc.git] / sysdeps / unix / sysv / linux / readdir64_r.c
1 /* Copyright (C) 1997 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;
66 break;
67 }
68 dirp->size = (size_t) bytes;
69
70 /* Reset the offset into the buffer. */
71 dirp->offset = 0;
72 }
73
74 dp = (struct dirent64 *) &dirp->data[dirp->offset];
75
76 #ifdef _DIRENT_HAVE_D_RECLEN
77 reclen = dp->d_reclen;
78 #else
79 /* The only version of `struct dirent64' that lacks `d_reclen'
80 is fixed-size. */
81 assert (sizeof dp->d_name > 1);
82 reclen = sizeof *dp;
83 /* The name is not terminated if it is the largest possible size.
84 Clobber the following byte to ensure proper null termination. We
85 read just one entry at a time above so we know that byte will not
86 be used later. */
87 dp->d_name[sizeof dp->d_name] = '\0';
88 #endif
89
90 dirp->offset += reclen;
91
92 #ifdef _DIRENT_HAVE_D_OFF
93 dirp->filepos = dp->d_off;
94 #else
95 dirp->filepos += reclen;
96 #endif
97
98 /* Skip deleted files. */
99 } while (dp->d_ino == 0);
100
101 if (dp != NULL)
102 *result = memcpy (entry, dp, reclen);
103
104 __libc_lock_unlock (dirp->lock);
105
106 return dp != NULL ? 0 : errno;
107 }
This page took 1.697792 seconds and 4 git commands to generate.