Fix coding style
[glibc.git] / sysdeps / posix / readdir_r.c
blobb5a8e2edef44a74ea51d1c588f327146887d9dea
1 /* Copyright (C) 1991-2013 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <limits.h>
20 #include <stddef.h>
21 #include <string.h>
22 #include <dirent.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <assert.h>
27 #include <dirstream.h>
29 #ifndef __READDIR_R
30 # define __READDIR_R __readdir_r
31 # define __GETDENTS __getdents
32 # define DIRENT_TYPE struct dirent
33 # define __READDIR_R_ALIAS
34 #endif
36 /* Read a directory entry from DIRP. */
37 int
38 __READDIR_R (DIR *dirp, DIRENT_TYPE *entry, DIRENT_TYPE **result)
40 DIRENT_TYPE *dp;
41 size_t reclen;
42 const int saved_errno = errno;
44 __libc_lock_lock (dirp->lock);
48 if (dirp->offset >= dirp->size)
50 /* We've emptied out our buffer. Refill it. */
52 size_t maxread;
53 ssize_t bytes;
55 #ifndef _DIRENT_HAVE_D_RECLEN
56 /* Fixed-size struct; must read one at a time (see below). */
57 maxread = sizeof *dp;
58 #else
59 maxread = dirp->allocation;
60 #endif
62 bytes = __GETDENTS (dirp->fd, dirp->data, maxread);
63 if (bytes <= 0)
65 /* On some systems getdents fails with ENOENT when the
66 open directory has been rmdir'd already. POSIX.1
67 requires that we treat this condition like normal EOF. */
68 if (bytes < 0 && errno == ENOENT)
70 bytes = 0;
71 __set_errno (saved_errno);
74 dp = NULL;
75 /* Reclen != 0 signals that an error occurred. */
76 reclen = bytes != 0;
77 break;
79 dirp->size = (size_t) bytes;
81 /* Reset the offset into the buffer. */
82 dirp->offset = 0;
85 dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];
87 #ifdef _DIRENT_HAVE_D_RECLEN
88 reclen = dp->d_reclen;
89 #else
90 /* The only version of `struct dirent*' that lacks `d_reclen'
91 is fixed-size. */
92 assert (sizeof dp->d_name > 1);
93 reclen = sizeof *dp;
94 /* The name is not terminated if it is the largest possible size.
95 Clobber the following byte to ensure proper null termination. We
96 read just one entry at a time above so we know that byte will not
97 be used later. */
98 dp->d_name[sizeof dp->d_name] = '\0';
99 #endif
101 dirp->offset += reclen;
103 #ifdef _DIRENT_HAVE_D_OFF
104 dirp->filepos = dp->d_off;
105 #else
106 dirp->filepos += reclen;
107 #endif
109 /* Skip deleted files. */
111 while (dp->d_ino == 0);
113 if (dp != NULL)
115 #ifdef GETDENTS_64BIT_ALIGNED
116 /* The d_reclen value might include padding which is not part of
117 the DIRENT_TYPE data structure. */
118 reclen = MIN (reclen,
119 offsetof (DIRENT_TYPE, d_name) + sizeof (dp->d_name));
120 #endif
121 *result = memcpy (entry, dp, reclen);
122 #ifdef GETDENTS_64BIT_ALIGNED
123 entry->d_reclen = reclen;
124 #endif
126 else
127 *result = NULL;
129 __libc_lock_unlock (dirp->lock);
131 return dp != NULL ? 0 : reclen ? errno : 0;
134 #ifdef __READDIR_R_ALIAS
135 weak_alias (__readdir_r, readdir_r)
136 #endif