readdir.c revision 1.24.8.2 1 1.24.8.2 tonnerre /* $NetBSD: readdir.c,v 1.24.8.2 2008/05/04 18:53:27 tonnerre Exp $ */
2 1.24.8.2 tonnerre
3 1.24.8.2 tonnerre /*
4 1.24.8.2 tonnerre * Copyright (c) 1983, 1993
5 1.24.8.2 tonnerre * The Regents of the University of California. All rights reserved.
6 1.24.8.2 tonnerre *
7 1.24.8.2 tonnerre * Redistribution and use in source and binary forms, with or without
8 1.24.8.2 tonnerre * modification, are permitted provided that the following conditions
9 1.24.8.2 tonnerre * are met:
10 1.24.8.2 tonnerre * 1. Redistributions of source code must retain the above copyright
11 1.24.8.2 tonnerre * notice, this list of conditions and the following disclaimer.
12 1.24.8.2 tonnerre * 2. Redistributions in binary form must reproduce the above copyright
13 1.24.8.2 tonnerre * notice, this list of conditions and the following disclaimer in the
14 1.24.8.2 tonnerre * documentation and/or other materials provided with the distribution.
15 1.24.8.2 tonnerre * 3. Neither the name of the University nor the names of its contributors
16 1.24.8.2 tonnerre * may be used to endorse or promote products derived from this software
17 1.24.8.2 tonnerre * without specific prior written permission.
18 1.24.8.2 tonnerre *
19 1.24.8.2 tonnerre * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.24.8.2 tonnerre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.24.8.2 tonnerre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.24.8.2 tonnerre * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.24.8.2 tonnerre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.24.8.2 tonnerre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.24.8.2 tonnerre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.24.8.2 tonnerre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.24.8.2 tonnerre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.24.8.2 tonnerre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.24.8.2 tonnerre * SUCH DAMAGE.
30 1.24.8.2 tonnerre */
31 1.24.8.2 tonnerre
32 1.24.8.2 tonnerre #include <sys/cdefs.h>
33 1.24.8.2 tonnerre #if defined(LIBC_SCCS) && !defined(lint)
34 1.24.8.2 tonnerre #if 0
35 1.24.8.2 tonnerre static char sccsid[] = "@(#)readdir.c 8.3 (Berkeley) 9/29/94";
36 1.24.8.2 tonnerre #else
37 1.24.8.2 tonnerre __RCSID("$NetBSD: readdir.c,v 1.24.8.2 2008/05/04 18:53:27 tonnerre Exp $");
38 1.24.8.2 tonnerre #endif
39 1.24.8.2 tonnerre #endif /* LIBC_SCCS and not lint */
40 1.24.8.2 tonnerre
41 1.24.8.2 tonnerre #include "namespace.h"
42 1.24.8.2 tonnerre #include "reentrant.h"
43 1.24.8.2 tonnerre #include "extern.h"
44 1.24.8.2 tonnerre #include <sys/param.h>
45 1.24.8.2 tonnerre
46 1.24.8.2 tonnerre #include <dirent.h>
47 1.24.8.2 tonnerre #include <errno.h>
48 1.24.8.2 tonnerre #include <string.h>
49 1.24.8.2 tonnerre #include <unistd.h>
50 1.24.8.2 tonnerre
51 1.24.8.2 tonnerre #include "dirent_private.h"
52 1.24.8.2 tonnerre
53 1.24.8.2 tonnerre /*
54 1.24.8.2 tonnerre * get next entry in a directory.
55 1.24.8.2 tonnerre */
56 1.24.8.2 tonnerre struct dirent *
57 1.24.8.2 tonnerre _readdir_unlocked(DIR *dirp, int skipdeleted)
58 1.24.8.2 tonnerre {
59 1.24.8.2 tonnerre struct dirent *dp;
60 1.24.8.2 tonnerre
61 1.24.8.2 tonnerre
62 1.24.8.2 tonnerre for (;;) {
63 1.24.8.2 tonnerre if (dirp->dd_loc >= dirp->dd_size) {
64 1.24.8.2 tonnerre if (dirp->dd_flags & __DTF_READALL)
65 1.24.8.2 tonnerre return (NULL);
66 1.24.8.2 tonnerre dirp->dd_loc = 0;
67 1.24.8.2 tonnerre }
68 1.24.8.2 tonnerre if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) {
69 1.24.8.2 tonnerre dirp->dd_seek = lseek(dirp->dd_fd, (off_t)0, SEEK_CUR);
70 1.24.8.2 tonnerre dirp->dd_size = getdents(dirp->dd_fd,
71 1.24.8.2 tonnerre dirp->dd_buf, (size_t)dirp->dd_len);
72 1.24.8.2 tonnerre if (dirp->dd_size <= 0)
73 1.24.8.2 tonnerre return (NULL);
74 1.24.8.2 tonnerre }
75 1.24.8.2 tonnerre dp = (struct dirent *)
76 1.24.8.2 tonnerre (void *)(dirp->dd_buf + (size_t)dirp->dd_loc);
77 1.24.8.2 tonnerre if ((intptr_t)dp & _DIRENT_ALIGN(dp))/* bogus pointer check */
78 1.24.8.2 tonnerre return (NULL);
79 1.24.8.2 tonnerre /* d_reclen is unsigned; no need to compare it <= 0 */
80 1.24.8.2 tonnerre if (dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
81 1.24.8.2 tonnerre return (NULL);
82 1.24.8.2 tonnerre dirp->dd_loc += dp->d_reclen;
83 1.24.8.2 tonnerre if (dp->d_ino == 0 && skipdeleted)
84 1.24.8.2 tonnerre continue;
85 1.24.8.2 tonnerre if (dp->d_type == DT_WHT && (dirp->dd_flags & DTF_HIDEW))
86 1.24.8.2 tonnerre continue;
87 1.24.8.2 tonnerre return (dp);
88 1.24.8.2 tonnerre }
89 1.24.8.2 tonnerre }
90 1.24.8.2 tonnerre
91 1.24.8.2 tonnerre struct dirent *
92 1.24.8.2 tonnerre readdir(dirp)
93 1.24.8.2 tonnerre DIR *dirp;
94 1.24.8.2 tonnerre {
95 1.24.8.2 tonnerre struct dirent *dp;
96 1.24.8.2 tonnerre
97 1.24.8.2 tonnerre #ifdef _REENTRANT
98 1.24.8.2 tonnerre if (__isthreaded) {
99 1.24.8.2 tonnerre mutex_lock((mutex_t *)dirp->dd_lock);
100 1.24.8.2 tonnerre dp = _readdir_unlocked(dirp, 1);
101 1.24.8.2 tonnerre mutex_unlock((mutex_t *)dirp->dd_lock);
102 1.24.8.2 tonnerre }
103 1.24.8.2 tonnerre else
104 1.24.8.2 tonnerre #endif
105 1.24.8.2 tonnerre dp = _readdir_unlocked(dirp, 1);
106 1.24.8.2 tonnerre return (dp);
107 1.24.8.2 tonnerre }
108 1.24.8.2 tonnerre
109 1.24.8.2 tonnerre int
110 1.24.8.2 tonnerre readdir_r(dirp, entry, result)
111 1.24.8.2 tonnerre DIR *dirp;
112 1.24.8.2 tonnerre struct dirent *entry;
113 1.24.8.2 tonnerre struct dirent **result;
114 1.24.8.2 tonnerre {
115 1.24.8.2 tonnerre struct dirent *dp;
116 1.24.8.2 tonnerre int saved_errno;
117 1.24.8.2 tonnerre
118 1.24.8.2 tonnerre saved_errno = errno;
119 1.24.8.2 tonnerre errno = 0;
120 1.24.8.2 tonnerre #ifdef _REENTRANT
121 1.24.8.2 tonnerre if (__isthreaded) {
122 1.24.8.2 tonnerre mutex_lock((mutex_t *)dirp->dd_lock);
123 1.24.8.2 tonnerre if ((dp = _readdir_unlocked(dirp, 1)) != NULL)
124 1.24.8.2 tonnerre memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));
125 1.24.8.2 tonnerre mutex_unlock((mutex_t *)dirp->dd_lock);
126 1.24.8.2 tonnerre }
127 1.24.8.2 tonnerre else
128 1.24.8.2 tonnerre #endif
129 1.24.8.2 tonnerre if ((dp = _readdir_unlocked(dirp, 1)) != NULL)
130 1.24.8.2 tonnerre memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));
131 1.24.8.2 tonnerre
132 1.24.8.2 tonnerre if (errno != 0) {
133 1.24.8.2 tonnerre if (dp == NULL)
134 1.24.8.2 tonnerre return (errno);
135 1.24.8.2 tonnerre } else
136 1.24.8.2 tonnerre errno = saved_errno;
137 1.24.8.2 tonnerre
138 1.24.8.2 tonnerre if (dp != NULL)
139 1.24.8.2 tonnerre *result = entry;
140 1.24.8.2 tonnerre else
141 1.24.8.2 tonnerre *result = NULL;
142 1.24.8.2 tonnerre
143 1.24.8.2 tonnerre return (0);
144 1.24.8.2 tonnerre }
145