Adjusted.
[glibc.git] / sysdeps / unix / opendir.c
blob4b21e642e81333e46922d02b1c3896b1bd44525a
1 /* Copyright (C) 1991-1996,98,2000,2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <dirent.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdio.h>
30 #include <dirstream.h>
32 /* opendir() must not accidentally open something other than a directory.
33 Some OS's have kernel support for that, some don't. In the worst
34 case we have to stat() before the open() AND fstat() after.
36 We have to test at runtime for kernel support since libc may have
37 been compiled with different headers to the kernel it's running on.
38 This test can't be done reliably in the general case. We'll use
39 /dev/null, which if it's not a device lots of stuff will break, as
40 a guinea pig. It may be missing in chroot environments, so we
41 make sure to fail safe. */
42 #ifdef O_DIRECTORY
43 # ifdef O_DIRECTORY_WORKS
44 # define o_directory_works 1
45 # define tryopen_o_directory() while (1) /* This must not be called. */
46 # else
47 static int o_directory_works;
49 static void
50 tryopen_o_directory (void)
52 int serrno = errno;
53 int x = __open ("/dev/null", O_RDONLY|O_NDELAY|O_DIRECTORY);
55 if (x >= 0)
57 __close (x);
58 o_directory_works = -1;
60 else if (errno != ENOTDIR)
61 o_directory_works = -1;
62 else
63 o_directory_works = 1;
65 __set_errno (serrno);
67 # endif
68 # define EXTRA_FLAGS O_DIRECTORY
69 #else
70 # define EXTRA_FLAGS 0
71 #endif
74 /* Open a directory stream on NAME. */
75 DIR *
76 __opendir (const char *name)
78 DIR *dirp;
79 struct stat64 statbuf;
80 int fd;
81 size_t allocation;
82 int save_errno;
84 if (__builtin_expect (name[0], '\1') == '\0')
86 /* POSIX.1-1990 says an empty name gets ENOENT;
87 but `open' might like it fine. */
88 __set_errno (ENOENT);
89 return NULL;
92 #ifdef O_DIRECTORY
93 /* Test whether O_DIRECTORY works. */
94 if (o_directory_works == 0)
95 tryopen_o_directory ();
97 /* We can skip the expensive `stat' call if O_DIRECTORY works. */
98 if (o_directory_works < 0)
99 #endif
101 /* We first have to check whether the name is for a directory. We
102 cannot do this after the open() call since the open/close operation
103 performed on, say, a tape device might have undesirable effects. */
104 if (__builtin_expect (__xstat64 (_STAT_VER, name, &statbuf), 0) < 0)
105 return NULL;
106 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
108 __set_errno (ENOTDIR);
109 return NULL;
113 fd = __open64 (name, O_RDONLY|O_NDELAY|EXTRA_FLAGS);
114 if (__builtin_expect (fd, 0) < 0)
115 return NULL;
117 /* Now make sure this really is a directory and nothing changed since
118 the `stat' call. We do not have to perform the test for the
119 descriptor being associated with a directory if we know the
120 O_DIRECTORY flag is honored by the kernel. */
121 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
122 goto lose;
123 #ifdef O_DIRECTORY
124 if (o_directory_works <= 0)
125 #endif
127 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
129 save_errno = ENOTDIR;
130 goto lose;
134 if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
135 goto lose;
137 #ifdef _STATBUF_ST_BLKSIZE
138 if (__builtin_expect ((size_t) statbuf.st_blksize < sizeof (struct dirent),
140 allocation = sizeof (struct dirent);
141 else
142 allocation = statbuf.st_blksize;
143 #else
144 allocation = (BUFSIZ < sizeof (struct dirent)
145 ? sizeof (struct dirent) : BUFSIZ);
146 #endif
148 dirp = (DIR *) calloc (1, sizeof (DIR) + allocation); /* Zero-fill. */
149 if (dirp == NULL)
150 lose:
152 save_errno = errno;
153 (void) __close (fd);
154 __set_errno (save_errno);
155 return NULL;
157 dirp->data = (char *) (dirp + 1);
158 dirp->allocation = allocation;
159 dirp->fd = fd;
161 __libc_lock_init (dirp->lock);
163 return dirp;
165 weak_alias (__opendir, opendir)