readdir.c revision 1.26 1 1.26 abs /* $NetBSD: readdir.c,v 1.26 2012/06/25 22:32:43 abs 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.26 abs __RCSID("$NetBSD: readdir.c,v 1.26 2012/06/25 22:32:43 abs 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.23 christos #include "dirent_private.h"
52 1.23 christos
53 1.20 christos /*
54 1.20 christos * get next entry in a directory.
55 1.20 christos */
56 1.21 christos struct dirent *
57 1.24 tonnerre _readdir_unlocked(DIR *dirp, int skipdeleted)
58 1.20 christos {
59 1.20 christos struct dirent *dp;
60 1.20 christos
61 1.20 christos for (;;) {
62 1.20 christos if (dirp->dd_loc >= dirp->dd_size) {
63 1.20 christos if (dirp->dd_flags & __DTF_READALL)
64 1.20 christos return (NULL);
65 1.20 christos dirp->dd_loc = 0;
66 1.20 christos }
67 1.20 christos if (dirp->dd_loc == 0 && !(dirp->dd_flags & __DTF_READALL)) {
68 1.20 christos dirp->dd_seek = lseek(dirp->dd_fd, (off_t)0, SEEK_CUR);
69 1.20 christos dirp->dd_size = getdents(dirp->dd_fd,
70 1.20 christos dirp->dd_buf, (size_t)dirp->dd_len);
71 1.20 christos if (dirp->dd_size <= 0)
72 1.20 christos return (NULL);
73 1.20 christos }
74 1.20 christos dp = (struct dirent *)
75 1.20 christos (void *)(dirp->dd_buf + (size_t)dirp->dd_loc);
76 1.20 christos if ((intptr_t)dp & _DIRENT_ALIGN(dp))/* bogus pointer check */
77 1.20 christos return (NULL);
78 1.20 christos /* d_reclen is unsigned; no need to compare it <= 0 */
79 1.20 christos if (dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
80 1.20 christos return (NULL);
81 1.20 christos dirp->dd_loc += dp->d_reclen;
82 1.24 tonnerre if (dp->d_ino == 0 && skipdeleted)
83 1.20 christos continue;
84 1.20 christos if (dp->d_type == DT_WHT && (dirp->dd_flags & DTF_HIDEW))
85 1.20 christos continue;
86 1.20 christos return (dp);
87 1.20 christos }
88 1.20 christos }
89 1.20 christos
90 1.20 christos struct dirent *
91 1.26 abs readdir(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.24 tonnerre dp = _readdir_unlocked(dirp, 1);
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.24 tonnerre dp = _readdir_unlocked(dirp, 1);
104 1.20 christos return (dp);
105 1.20 christos }
106 1.20 christos
107 1.20 christos int
108 1.26 abs readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
109 1.20 christos {
110 1.20 christos struct dirent *dp;
111 1.20 christos int saved_errno;
112 1.20 christos
113 1.20 christos saved_errno = errno;
114 1.20 christos errno = 0;
115 1.20 christos #ifdef _REENTRANT
116 1.20 christos if (__isthreaded) {
117 1.20 christos mutex_lock((mutex_t *)dirp->dd_lock);
118 1.24 tonnerre if ((dp = _readdir_unlocked(dirp, 1)) != NULL)
119 1.20 christos memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));
120 1.20 christos mutex_unlock((mutex_t *)dirp->dd_lock);
121 1.20 christos }
122 1.20 christos else
123 1.20 christos #endif
124 1.24 tonnerre if ((dp = _readdir_unlocked(dirp, 1)) != NULL)
125 1.20 christos memcpy(entry, dp, (size_t)_DIRENT_SIZE(dp));
126 1.20 christos
127 1.20 christos if (errno != 0) {
128 1.20 christos if (dp == NULL)
129 1.20 christos return (errno);
130 1.20 christos } else
131 1.20 christos errno = saved_errno;
132 1.20 christos
133 1.20 christos if (dp != NULL)
134 1.20 christos *result = entry;
135 1.20 christos else
136 1.20 christos *result = NULL;
137 1.20 christos
138 1.20 christos return (0);
139 1.20 christos }
140