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