Adjusted.
[glibc.git] / sysdeps / unix / readdir_r.c
blob113e1b41e13076c312013c820f9057190dd38ec7
1 /* Copyright (C) 1991,92,93,94,95,96,97,98,99,2000 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. */
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>
28 #include <dirstream.h>
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
37 /* Read a directory entry from DIRP. */
38 int
39 __READDIR_R (DIR *dirp, DIRENT_TYPE *entry, DIRENT_TYPE **result)
41 DIRENT_TYPE *dp;
42 size_t reclen;
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 dp = NULL;
66 /* Reclen != 0 signals that an error occurred. */
67 reclen = bytes != 0;
68 break;
70 dirp->size = (size_t) bytes;
72 /* Reset the offset into the buffer. */
73 dirp->offset = 0;
76 dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];
78 #ifdef _DIRENT_HAVE_D_RECLEN
79 reclen = dp->d_reclen;
80 #else
81 /* The only version of `struct dirent*' that lacks `d_reclen'
82 is fixed-size. */
83 assert (sizeof dp->d_name > 1);
84 reclen = sizeof *dp;
85 /* The name is not terminated if it is the largest possible size.
86 Clobber the following byte to ensure proper null termination. We
87 read just one entry at a time above so we know that byte will not
88 be used later. */
89 dp->d_name[sizeof dp->d_name] = '\0';
90 #endif
92 dirp->offset += reclen;
94 #ifdef _DIRENT_HAVE_D_OFF
95 dirp->filepos = dp->d_off;
96 #else
97 dirp->filepos += reclen;
98 #endif
100 /* Skip deleted files. */
102 while (dp->d_ino == 0);
104 if (dp != NULL)
105 *result = memcpy (entry, dp, reclen);
106 else
107 *result = NULL;
109 __libc_lock_unlock (dirp->lock);
111 return dp != NULL ? 0 : reclen ? errno : 0;
114 #ifdef __READDIR_R_ALIAS
115 weak_alias (__readdir_r, readdir_r)
116 #endif