Fix warnings, ANSIfy.
[dragonfly.git] / lib / libc / gen / opendir.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
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 $
35  *
36  * @(#)opendir.c        8.8 (Berkeley) 5/1/95
37  */
38
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43
44 #include <dirent.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include "un-namespace.h"
51
52 /*
53  * Open a directory.
54  */
55 DIR *
56 opendir(const char *name)
57 {
58         return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
59 }
60
61 DIR *
62 __opendir2(const char *name, int flags)
63 {
64         DIR *dirp;
65         int fd;
66         int incr;
67         int saved_errno;
68         int unionstack;
69         struct stat statb;
70
71         /*
72          * stat() before _open() because opening of special files may be
73          * harmful.  _fstat() after open because the file may have changed.
74          */
75         if (stat(name, &statb) != 0)
76                 return (NULL);
77         if (!S_ISDIR(statb.st_mode)) {
78                 errno = ENOTDIR;
79                 return (NULL);
80         }
81         if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
82                 return (NULL);
83         dirp = NULL;
84         if (_fstat(fd, &statb) != 0)
85                 goto fail;
86         if (!S_ISDIR(statb.st_mode)) {
87                 errno = ENOTDIR;
88                 goto fail;
89         }
90         if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
91             (dirp = malloc(sizeof(DIR))) == NULL)
92                 goto fail;
93
94         /*
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().
98          */
99         incr = getpagesize();
100         if ((incr % DIRBLKSIZ) != 0) 
101                 incr = DIRBLKSIZ;
102
103         /*
104          * Determine whether this directory is the top of a union stack.
105          */
106         if (flags & DTF_NODUP) {
107                 struct statfs sfb;
108
109                 if (_fstatfs(fd, &sfb) < 0)
110                         goto fail;
111                 unionstack = !strcmp(sfb.f_fstypename, "union")
112                     || (sfb.f_flags & MNT_UNION);
113         } else {
114                 unionstack = 0;
115         }
116
117         if (unionstack) {
118                 int len = 0;
119                 int space = 0;
120                 char *buf = 0;
121                 char *ddptr = 0;
122                 char *ddeptr;
123                 int n;
124                 struct dirent **dpv;
125
126                 /*
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
130                  * number to zero.
131                  */
132
133                 do {
134                         /*
135                          * Always make at least DIRBLKSIZ bytes
136                          * available to _getdirentries
137                          */
138                         if (space < DIRBLKSIZ) {
139                                 space += incr;
140                                 len += incr;
141                                 buf = reallocf(buf, len);
142                                 if (buf == NULL)
143                                         goto fail;
144                                 ddptr = buf + (len - space);
145                         }
146
147                         n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
148                         if (n > 0) {
149                                 ddptr += n;
150                                 space -= n;
151                         }
152                 } while (n > 0);
153
154                 ddeptr = ddptr;
155                 flags |= __DTF_READALL;
156
157                 /*
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.
163                  */
164                 if (flags & DTF_REWIND) {
165                         (void)_close(fd);
166                         if ((fd = _open(name, O_RDONLY)) == -1) {
167                                 saved_errno = errno;
168                                 free(buf);
169                                 free(dirp);
170                                 errno = saved_errno;
171                                 return (NULL);
172                         }
173                 }
174
175                 /*
176                  * There is now a buffer full of (possibly) duplicate
177                  * names.
178                  */
179                 dirp->dd_buf = buf;
180
181                 /*
182                  * Go round this loop twice...
183                  *
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.
187                  */
188                 for (dpv = 0;;) {
189                         n = 0;
190                         ddptr = buf;
191                         while (ddptr < ddeptr) {
192                                 struct dirent *dp;
193
194                                 dp = (struct dirent *) ddptr;
195                                 if ((long)dp & 03L)
196                                         break;
197                                 if ((dp->d_reclen <= 0) ||
198                                     (dp->d_reclen > (ddeptr + 1 - ddptr)))
199                                         break;
200                                 ddptr += dp->d_reclen;
201                                 if (dp->d_fileno) {
202                                         if (dpv)
203                                                 dpv[n] = dp;
204                                         n++;
205                                 }
206                         }
207
208                         if (dpv) {
209                                 struct dirent *xp;
210
211                                 /*
212                                  * This sort must be stable.
213                                  */
214                                 mergesort(dpv, n, sizeof(*dpv), alphasort);
215
216                                 dpv[n] = NULL;
217                                 xp = NULL;
218
219                                 /*
220                                  * Scan through the buffer in sort order,
221                                  * zapping the inode number of any
222                                  * duplicate names.
223                                  */
224                                 for (n = 0; dpv[n]; n++) {
225                                         struct dirent *dp = dpv[n];
226
227                                         if ((xp == NULL) ||
228                                             strcmp(dp->d_name, xp->d_name)) {
229                                                 xp = dp;
230                                         } else {
231                                                 dp->d_fileno = 0;
232                                         }
233                                         if (dp->d_type == DT_WHT &&
234                                             (flags & DTF_HIDEW))
235                                                 dp->d_fileno = 0;
236                                 }
237
238                                 free(dpv);
239                                 break;
240                         } else {
241                                 dpv = malloc((n+1) * sizeof(struct dirent *));
242                                 if (dpv == NULL)
243                                         break;
244                         }
245                 }
246
247                 dirp->dd_len = len;
248                 dirp->dd_size = ddptr - dirp->dd_buf;
249         } else {
250                 dirp->dd_len = incr;
251                 dirp->dd_buf = malloc(dirp->dd_len);
252                 if (dirp->dd_buf == NULL)
253                         goto fail;
254                 dirp->dd_seek = 0;
255                 flags &= ~DTF_REWIND;
256         }
257
258         dirp->dd_loc = 0;
259         dirp->dd_fd = fd;
260         dirp->dd_flags = flags;
261         dirp->dd_lock = NULL;
262
263         /*
264          * Set up seek point for rewinddir.
265          */
266         dirp->dd_rewind = telldir(dirp);
267
268         return (dirp);
269
270 fail:
271         saved_errno = errno;
272         free(dirp);
273         (void)_close(fd);
274         errno = saved_errno;
275         return (NULL);
276 }