Make unistd.h pre-c((-safe.
[glibc.git] / sysdeps / mach / hurd / opendir.c
blob23e04ede0e6cd44844c25c314dbcec92c67695be
1 /* Copyright (C) 1993,1994,1995,1996,1997,1998,2001,2003,2005,2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <hurd.h>
32 #include <hurd/fd.h>
33 #include "dirstream.h"
36 /* Open a directory stream on a file descriptor in Hurd internal form.
37 We do no checking here on the descriptor. */
38 DIR *
39 _hurd_fd_opendir (struct hurd_fd *d)
41 DIR *dirp;
43 if (d == NULL)
45 errno = EBADF;
46 return NULL;
49 dirp = (DIR *) malloc (sizeof (DIR));
50 if (dirp == NULL)
51 return NULL;
53 /* Set the descriptor to close on exec. */
54 __spin_lock (&d->port.lock);
55 d->flags |= FD_CLOEXEC;
56 __spin_unlock (&d->port.lock);
58 dirp->__fd = d;
59 dirp->__data = dirp->__ptr = NULL;
60 dirp->__entry_data = dirp->__entry_ptr = 0;
61 dirp->__allocation = 0;
62 dirp->__size = 0;
64 __libc_lock_init (dirp->__lock);
66 return dirp;
70 /* Open a directory stream on NAME. */
71 DIR *
72 __opendir (const char *name)
74 if (name[0] == '\0')
76 /* POSIX.1-1990 says an empty name gets ENOENT;
77 but `open' might like it fine. */
78 __set_errno (ENOENT);
79 return NULL;
82 int fd = __open (name, O_RDONLY | O_NONBLOCK | O_DIRECTORY);
83 if (fd < 0)
84 return NULL;
86 /* Extract the pointer to the descriptor structure. */
87 DIR *dirp = _hurd_fd_opendir (_hurd_fd_get (fd));
88 if (dirp == NULL)
89 __close (fd);
91 return dirp;
93 weak_alias (__opendir, opendir)