Remove building with NOCRYPTO option
[minix3.git] / lib / libc / gen / opendir.c
blob5442e6a1c63b179f403474664050657825e26064
1 /* $NetBSD: opendir.c,v 1.39 2014/11/26 16:48:43 christos Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94";
36 #else
37 __RCSID("$NetBSD: opendir.c,v 1.39 2014/11/26 16:48:43 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include "reentrant.h"
43 #include "extern.h"
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #include <sys/stat.h>
49 #include <assert.h>
50 #include <dirent.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
57 #include "dirent_private.h"
59 static DIR *__opendir_common(int, const char *, int);
61 __weak_alias(fdopendir,_fdopendir)
64 * Open a directory.
66 DIR *
67 opendir(const char *name)
70 _DIAGASSERT(name != NULL);
72 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
75 DIR *
76 __opendir2(const char *name, int flags)
78 DIR *dirp;
79 int fd;
81 if ((fd = open(name, O_RDONLY|O_DIRECTORY|O_NONBLOCK|O_CLOEXEC)) == -1)
82 return NULL;
84 dirp = __opendir_common(fd, name, flags);
85 if (dirp == NULL) {
86 int serrno = errno;
87 (void)close(fd);
88 errno = serrno;
90 return dirp;
93 #ifndef __LIBC12_SOURCE__
94 DIR *
95 _fdopendir(int fd)
97 struct stat sb;
99 if (fstat(fd, &sb) == -1)
100 return NULL;
102 if (!S_ISDIR(sb.st_mode)) {
103 errno = ENOTDIR;
104 return NULL;
107 /* This is optional according to POSIX, but a good measure */
108 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
109 return NULL;
111 return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP);
113 #endif
115 static DIR *
116 __opendir_common(int fd, const char *name, int flags)
118 DIR *dirp;
119 int serrno;
120 #if !defined(__minix)
121 struct statvfs sfb;
122 #else
123 struct stat sb;
124 #endif /* !defined(__minix) */
125 int error;
127 #if defined(__minix)
128 dirp = NULL; /* LSC: Make sure the pointer is initialized */
129 if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
130 errno = ENOTDIR;
131 goto error;
133 #endif /* defined(__minix) */
135 if ((dirp = malloc(sizeof(*dirp))) == NULL)
136 goto error;
137 dirp->dd_buf = NULL;
138 dirp->dd_internal = NULL;
139 #ifdef _REENTRANT
140 if (__isthreaded) {
141 if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
142 goto error;
143 mutex_init((mutex_t *)dirp->dd_lock, NULL);
145 #endif
148 * Tweak flags for the underlying filesystem.
151 #if defined(__minix)
152 /* MOUNT_UNION and MOUNT_NFS not supported */
153 flags &= ~DTF_NODUP;
154 #else
155 if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
156 goto error;
157 if ((flags & DTF_NODUP) != 0) {
158 if (!strncmp(sfb.f_fstypename, MOUNT_UNION,
159 sizeof(sfb.f_fstypename)) ||
160 (sfb.f_flag & MNT_UNION) != 0) {
161 flags |= __DTF_READALL;
162 } else {
163 flags &= ~DTF_NODUP;
166 if (!strncmp(sfb.f_fstypename, MOUNT_NFS, sizeof(sfb.f_fstypename))) {
167 flags |= __DTF_READALL | __DTF_RETRY_ON_BADCOOKIE;
169 #endif /* !defined(__minix) */
171 dirp->dd_flags = flags;
172 error = _initdir(dirp, fd, name);
173 if (error) {
174 errno = error;
175 goto error;
178 return (dirp);
179 error:
180 serrno = errno;
181 if (dirp != NULL) {
182 #ifdef _REENTRANT
183 if (__isthreaded) {
184 mutex_destroy((mutex_t *)dirp->dd_lock);
185 free(dirp->dd_lock);
187 #endif
188 free(dirp->dd_buf);
190 free(dirp);
191 errno = serrno;
192 return NULL;