2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/lib/libc/gen/opendir.c,v 1.10.2.1 2001/06/04 20:59:48 joerg Exp $
34 * $DragonFly: src/lib/libc/gen/opendir.c,v 1.4 2005/04/26 08:27:44 joerg Exp $
36 * @(#)opendir.c 8.8 (Berkeley) 5/1/95
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/mount.h>
50 #include "un-namespace.h"
56 opendir(const char *name)
58 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
62 __opendir2(const char *name, int flags)
72 * stat() before _open() because opening of special files may be
73 * harmful. _fstat() after open because the file may have changed.
75 if (stat(name, &statb) != 0)
77 if (!S_ISDIR(statb.st_mode)) {
81 if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
84 if (_fstat(fd, &statb) != 0)
86 if (!S_ISDIR(statb.st_mode)) {
90 if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
91 (dirp = malloc(sizeof(DIR))) == NULL)
95 * Use the system page size if that is a multiple of DIRBLKSIZ.
96 * Hopefully this can be a big win someday by allowing page
97 * trades to user space to be done by _getdirentries().
100 if ((incr % DIRBLKSIZ) != 0)
104 * Determine whether this directory is the top of a union stack.
106 if (flags & DTF_NODUP) {
109 if (_fstatfs(fd, &sfb) < 0)
111 unionstack = !strcmp(sfb.f_fstypename, "union")
112 || (sfb.f_flags & MNT_UNION);
127 * The strategy here is to read all the directory
128 * entries into a buffer, sort the buffer, and
129 * remove duplicate entries by setting the inode
135 * Always make at least DIRBLKSIZ bytes
136 * available to _getdirentries
138 if (space < DIRBLKSIZ) {
141 buf = reallocf(buf, len);
144 ddptr = buf + (len - space);
147 n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
155 flags |= __DTF_READALL;
158 * Re-open the directory.
159 * This has the effect of rewinding back to the
160 * top of the union stack and is needed by
161 * programs which plan to fchdir to a descriptor
162 * which has also been read -- see fts.c.
164 if (flags & DTF_REWIND) {
166 if ((fd = _open(name, O_RDONLY)) == -1) {
176 * There is now a buffer full of (possibly) duplicate
182 * Go round this loop twice...
184 * Scan through the buffer, counting entries.
185 * On the second pass, save pointers to each one.
186 * Then sort the pointers and remove duplicate names.
191 while (ddptr < ddeptr) {
194 dp = (struct dirent *) ddptr;
197 if ((dp->d_reclen <= 0) ||
198 (dp->d_reclen > (ddeptr + 1 - ddptr)))
200 ddptr += dp->d_reclen;
212 * This sort must be stable.
214 mergesort(dpv, n, sizeof(*dpv), alphasort);
220 * Scan through the buffer in sort order,
221 * zapping the inode number of any
224 for (n = 0; dpv[n]; n++) {
225 struct dirent *dp = dpv[n];
228 strcmp(dp->d_name, xp->d_name)) {
233 if (dp->d_type == DT_WHT &&
241 dpv = malloc((n+1) * sizeof(struct dirent *));
248 dirp->dd_size = ddptr - dirp->dd_buf;
251 dirp->dd_buf = malloc(dirp->dd_len);
252 if (dirp->dd_buf == NULL)
255 flags &= ~DTF_REWIND;
260 dirp->dd_flags = flags;
261 dirp->dd_lock = NULL;
264 * Set up seek point for rewinddir.
266 dirp->dd_rewind = telldir(dirp);