ef75c362344c944defc9d0a824cd815f31dd08e6
[platform/upstream/glibc.git] / sysdeps / unix / readdir_r.c
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, 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
31 /* Read a directory entry from DIRP.  */
32 int
33 __readdir_r (DIR *dirp, struct dirent *entry, struct dirent **result)
34 {
35   struct dirent *dp;
36
37   __libc_lock_lock (dirp->lock);
38
39   do
40     {
41       size_t reclen;
42
43       if (dirp->offset >= dirp->size)
44         {
45           /* We've emptied out our buffer.  Refill it.  */
46
47           size_t maxread;
48           off_t base;
49           ssize_t bytes;
50
51 #ifndef _DIRENT_HAVE_D_RECLEN
52           /* Fixed-size struct; must read one at a time (see below).  */
53           maxread = sizeof *dp;
54 #else
55           maxread = dirp->allocation;
56 #endif
57
58           base = dirp->filepos;
59           bytes = __getdirentries (dirp->fd, dirp->data, maxread, &base);
60           if (bytes <= 0)
61             {
62               dp = NULL;
63               break;
64             }
65           dirp->size = (size_t) bytes;
66
67           /* Reset the offset into the buffer.  */
68           dirp->offset = 0;
69         }
70
71       dp = (struct dirent *) &dirp->data[dirp->offset];
72
73 #ifdef _DIRENT_HAVE_D_RECLEN
74       reclen = dp->d_reclen;
75 #else
76       /* The only version of `struct dirent' that lacks `d_reclen'
77          is fixed-size.  */
78       assert (sizeof dp->d_name > 1);
79       reclen = sizeof *dp;
80       /* The name is not terminated if it is the largest possible size.
81          Clobber the following byte to ensure proper null termination.  We
82          read just one entry at a time above so we know that byte will not
83          be used later.  */
84       dp->d_name[sizeof dp->d_name] = '\0';
85 #endif
86
87       dirp->offset += reclen;
88
89 #ifdef _DIRENT_HAVE_D_OFF
90       dirp->filepos = dp->d_off;
91 #else
92       dirp->filepos += reclen;
93 #endif
94
95       /* Skip deleted files.  */
96     } while (dp->d_ino == 0);
97
98   if (dp != NULL)
99     {
100       *entry = *dp;
101       *result = entry;
102     }
103
104   __libc_lock_unlock (dirp->lock);
105
106   return dp != NULL ? 0 : -1;
107 }
108 weak_alias (__readdir_r, readdir_r)