1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 98 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. */
25 #include <sys/types.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. */
43 static int o_directory_works
;
46 tryopen_o_directory (void)
49 int x
= __open ("/dev/null", O_RDONLY
|O_NDELAY
|O_DIRECTORY
);
54 o_directory_works
= -1;
56 else if (errno
!= ENOTDIR
)
57 o_directory_works
= -1;
59 o_directory_works
= 1;
63 # define EXTRA_FLAGS O_DIRECTORY
65 # define EXTRA_FLAGS 0
69 /* Open a directory stream on NAME. */
71 __opendir (const char *name
)
81 /* POSIX.1-1990 says an empty name gets ENOENT;
82 but `open' might like it fine. */
88 /* Test whether O_DIRECTORY works. */
89 if (o_directory_works
== 0)
90 tryopen_o_directory ();
92 /* We can skip the expensive `stat' call if O_DIRECTORY works. */
93 if (o_directory_works
< 0)
96 /* We first have to check whether the name is for a directory. We
97 cannot do this after the open() call since the open/close operation
98 performed on, say, a tape device might have undesirable effects. */
99 if (__xstat (_STAT_VER
, name
, &statbuf
) < 0)
101 if (! S_ISDIR (statbuf
.st_mode
))
103 __set_errno (ENOTDIR
);
108 fd
= __open (name
, O_RDONLY
|O_NDELAY
|EXTRA_FLAGS
);
112 /* Now make sure this really is a directory and nothing changed since
114 if (__fstat (fd
, &statbuf
) < 0)
116 if (! S_ISDIR (statbuf
.st_mode
))
118 save_errno
= ENOTDIR
;
122 if (__fcntl (fd
, F_SETFD
, FD_CLOEXEC
) < 0)
125 #ifdef _STATBUF_ST_BLKSIZE
126 if (statbuf
.st_blksize
< sizeof (struct dirent
))
127 allocation
= sizeof (struct dirent
);
129 allocation
= statbuf
.st_blksize
;
131 allocation
= (BUFSIZ
< sizeof (struct dirent
)
132 ? sizeof (struct dirent
) : BUFSIZ
);
135 dirp
= (DIR *) calloc (1, sizeof (DIR) + allocation
); /* Zero-fill. */
141 __set_errno (save_errno
);
144 dirp
->data
= (char *) (dirp
+ 1);
145 dirp
->allocation
= allocation
;
148 __libc_lock_init (dirp
->lock
);
152 weak_alias (__opendir
, opendir
)