(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / mach / hurd / opendir.c
bloba1ff947f06a65be0539b98966793b91c541cc38d
1 /* Copyright (C) 1993,94,95,96,97,98,2001,2003 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <hurd.h>
31 #include <hurd/fd.h>
32 #include "dirstream.h"
35 /* Open a directory stream on NAME. */
36 DIR *
37 __opendir (const char *name)
39 DIR *dirp;
40 int fd;
41 struct hurd_fd *d;
43 if (name[0] == '\0')
45 /* POSIX.1-1990 says an empty name gets ENOENT;
46 but `open' might like it fine. */
47 __set_errno (ENOENT);
48 return NULL;
52 /* Append trailing slash to directory name to force ENOTDIR
53 if it's not a directory.
55 We open using the O_NONBLOCK flag so that a nondirectory with
56 blocking behavior (FIFO or device) gets ENOTDIR immediately
57 rather than waiting for the special file's open wakeup predicate. */
59 size_t len = strlen (name);
60 if (name[len - 1] == '/')
61 fd = __open (name, O_RDONLY | O_NONBLOCK);
62 else
64 char n[len + 2];
65 memcpy (n, name, len);
66 n[len] = '/';
67 n[len + 1] = '\0';
68 fd = __open (n, O_RDONLY | O_NONBLOCK);
71 if (fd < 0)
72 return NULL;
74 dirp = (DIR *) malloc (sizeof (DIR));
75 if (dirp == NULL)
77 __close (fd);
78 return NULL;
81 /* Extract the pointer to the descriptor structure. */
82 __mutex_lock (&_hurd_dtable_lock);
83 d = dirp->__fd = _hurd_dtable[fd];
84 __mutex_unlock (&_hurd_dtable_lock);
86 /* Set the descriptor to close on exec. */
87 __spin_lock (&d->port.lock);
88 d->flags |= FD_CLOEXEC;
89 __spin_unlock (&d->port.lock);
91 dirp->__data = dirp->__ptr = NULL;
92 dirp->__entry_data = dirp->__entry_ptr = 0;
93 dirp->__allocation = 0;
94 dirp->__size = 0;
96 __libc_lock_init (dirp->__lock);
98 return dirp;
100 weak_alias (__opendir, opendir)