]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/readdir_r.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / sysdeps / unix / readdir_r.c
CommitLineData
1a811397 1/* Copyright (C) 1991,92,93,94,95,96,97,98,99,2000,02,10
90341544 2 Free Software Foundation, Inc.
47707456 3 This file is part of the GNU C Library.
a68b0d31 4
47707456 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
a68b0d31 9
47707456
UD
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
41bdb6e2 13 Lesser General Public License for more details.
a68b0d31 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
a68b0d31
UD
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
56ddf355
UD
30#ifndef __READDIR_R
31# define __READDIR_R __readdir_r
32# define __GETDENTS __getdents
33# define DIRENT_TYPE struct dirent
34# define __READDIR_R_ALIAS
35#endif
a68b0d31
UD
36
37/* Read a directory entry from DIRP. */
38int
56ddf355 39__READDIR_R (DIR *dirp, DIRENT_TYPE *entry, DIRENT_TYPE **result)
a68b0d31 40{
56ddf355 41 DIRENT_TYPE *dp;
0d8733c4 42 size_t reclen;
90341544 43 const int saved_errno = errno;
a68b0d31
UD
44
45 __libc_lock_lock (dirp->lock);
46
47 do
48 {
a68b0d31
UD
49 if (dirp->offset >= dirp->size)
50 {
51 /* We've emptied out our buffer. Refill it. */
52
53 size_t maxread;
a68b0d31
UD
54 ssize_t bytes;
55
56#ifndef _DIRENT_HAVE_D_RECLEN
57 /* Fixed-size struct; must read one at a time (see below). */
58 maxread = sizeof *dp;
59#else
60 maxread = dirp->allocation;
61#endif
62
56ddf355 63 bytes = __GETDENTS (dirp->fd, dirp->data, maxread);
a68b0d31
UD
64 if (bytes <= 0)
65 {
90341544
RM
66 /* On some systems getdents fails with ENOENT when the
67 open directory has been rmdir'd already. POSIX.1
68 requires that we treat this condition like normal EOF. */
69 if (bytes < 0 && errno == ENOENT)
70 {
71 bytes = 0;
72 __set_errno (saved_errno);
73 }
74
a68b0d31 75 dp = NULL;
c063ba61
UD
76 /* Reclen != 0 signals that an error occurred. */
77 reclen = bytes != 0;
a68b0d31
UD
78 break;
79 }
80 dirp->size = (size_t) bytes;
81
82 /* Reset the offset into the buffer. */
83 dirp->offset = 0;
84 }
85
56ddf355 86 dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];
a68b0d31
UD
87
88#ifdef _DIRENT_HAVE_D_RECLEN
89 reclen = dp->d_reclen;
90#else
56ddf355 91 /* The only version of `struct dirent*' that lacks `d_reclen'
a68b0d31
UD
92 is fixed-size. */
93 assert (sizeof dp->d_name > 1);
94 reclen = sizeof *dp;
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 just one entry at a time above so we know that byte will not
98 be used later. */
99 dp->d_name[sizeof dp->d_name] = '\0';
100#endif
101
102 dirp->offset += reclen;
103
104#ifdef _DIRENT_HAVE_D_OFF
105 dirp->filepos = dp->d_off;
106#else
107 dirp->filepos += reclen;
108#endif
109
110 /* Skip deleted files. */
c063ba61
UD
111 }
112 while (dp->d_ino == 0);
a68b0d31
UD
113
114 if (dp != NULL)
1a811397
UD
115 {
116#ifdef GETDENTS_64BIT_ALIGNED
117 /* The d_reclen value might include padding which is not part of
118 the DIRENT_TYPE data structure. */
db753e2c
UD
119 reclen = MIN (reclen,
120 offsetof (DIRENT_TYPE, d_name) + sizeof (dp->d_name));
1a811397
UD
121#endif
122 *result = memcpy (entry, dp, reclen);
123#ifdef GETDENTS_64BIT_ALIGNED
124 entry->d_reclen = reclen;
125#endif
126 }
c063ba61
UD
127 else
128 *result = NULL;
a68b0d31
UD
129
130 __libc_lock_unlock (dirp->lock);
131
c063ba61 132 return dp != NULL ? 0 : reclen ? errno : 0;
a68b0d31 133}
56ddf355
UD
134
135#ifdef __READDIR_R_ALIAS
a68b0d31 136weak_alias (__readdir_r, readdir_r)
56ddf355 137#endif