1 | /* Copyright (C) 1991-2017 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 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. |
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 | Lesser General Public License for more details. |
13 | |
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/>. */ |
17 | |
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> |
26 | |
27 | #include <dirstream.h> |
28 | |
29 | #ifndef __READDIR |
30 | # define __READDIR __readdir |
31 | # define __GETDENTS __getdents |
32 | # define DIRENT_TYPE struct dirent |
33 | # define __READDIR_ALIAS |
34 | #endif |
35 | |
36 | /* Read a directory entry from DIRP. */ |
37 | DIRENT_TYPE * |
38 | __READDIR (DIR *dirp) |
39 | { |
40 | DIRENT_TYPE *dp; |
41 | int saved_errno = errno; |
42 | |
43 | #if IS_IN (libc) |
44 | __libc_lock_lock (dirp->lock); |
45 | #endif |
46 | |
47 | do |
48 | { |
49 | size_t reclen; |
50 | |
51 | if (dirp->offset >= dirp->size) |
52 | { |
53 | /* We've emptied out our buffer. Refill it. */ |
54 | |
55 | size_t maxread; |
56 | ssize_t bytes; |
57 | |
58 | #ifndef _DIRENT_HAVE_D_RECLEN |
59 | /* Fixed-size struct; must read one at a time (see below). */ |
60 | maxread = sizeof *dp; |
61 | #else |
62 | maxread = dirp->allocation; |
63 | #endif |
64 | |
65 | bytes = __GETDENTS (dirp->fd, dirp->data, maxread); |
66 | if (bytes <= 0) |
67 | { |
68 | /* On some systems getdents fails with ENOENT when the |
69 | open directory has been rmdir'd already. POSIX.1 |
70 | requires that we treat this condition like normal EOF. */ |
71 | if (bytes < 0 && errno == ENOENT) |
72 | bytes = 0; |
73 | |
74 | /* Don't modifiy errno when reaching EOF. */ |
75 | if (bytes == 0) |
76 | __set_errno (saved_errno); |
77 | dp = NULL; |
78 | break; |
79 | } |
80 | dirp->size = (size_t) bytes; |
81 | |
82 | /* Reset the offset into the buffer. */ |
83 | dirp->offset = 0; |
84 | } |
85 | |
86 | dp = (DIRENT_TYPE *) &dirp->data[dirp->offset]; |
87 | |
88 | #ifdef _DIRENT_HAVE_D_RECLEN |
89 | reclen = dp->d_reclen; |
90 | #else |
91 | /* The only version of `struct dirent*' that lacks `d_reclen' |
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 jst 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. */ |
111 | } while (dp->d_ino == 0); |
112 | |
113 | #if IS_IN (libc) |
114 | __libc_lock_unlock (dirp->lock); |
115 | #endif |
116 | |
117 | return dp; |
118 | } |
119 | |
120 | #ifdef __READDIR_ALIAS |
121 | weak_alias (__readdir, readdir) |
122 | #endif |
123 | |