]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1993-2015 Free Software Foundation, Inc. |
478b92f0 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
478b92f0 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 8 | |
478b92f0 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 17 | |
28f540f4 RM |
18 | #include <errno.h> |
19 | #include <limits.h> | |
20 | #include <stddef.h> | |
21 | #include <stdlib.h> | |
8a0746ae | 22 | #include <string.h> |
28f540f4 RM |
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> | |
29 | #include <hurd.h> | |
f0bf9cb9 | 30 | #include <hurd/fd.h> |
80b4e5f3 | 31 | #include <not-cancel.h> |
9e865ade | 32 | #include "dirstream.h" |
28f540f4 RM |
33 | |
34 | ||
63be256a RM |
35 | /* Open a directory stream on a file descriptor in Hurd internal form. |
36 | We do no checking here on the descriptor. */ | |
37 | DIR * | |
38 | _hurd_fd_opendir (struct hurd_fd *d) | |
39 | { | |
40 | DIR *dirp; | |
41 | ||
42 | if (d == NULL) | |
43 | { | |
44 | errno = EBADF; | |
45 | return NULL; | |
46 | } | |
47 | ||
48 | dirp = (DIR *) malloc (sizeof (DIR)); | |
49 | if (dirp == NULL) | |
50 | return NULL; | |
51 | ||
52 | /* Set the descriptor to close on exec. */ | |
802ca5a5 | 53 | HURD_CRITICAL_BEGIN; |
63be256a RM |
54 | __spin_lock (&d->port.lock); |
55 | d->flags |= FD_CLOEXEC; | |
56 | __spin_unlock (&d->port.lock); | |
802ca5a5 | 57 | HURD_CRITICAL_END; |
63be256a RM |
58 | |
59 | dirp->__fd = d; | |
60 | dirp->__data = dirp->__ptr = NULL; | |
61 | dirp->__entry_data = dirp->__entry_ptr = 0; | |
62 | dirp->__allocation = 0; | |
63 | dirp->__size = 0; | |
64 | ||
65 | __libc_lock_init (dirp->__lock); | |
66 | ||
67 | return dirp; | |
68 | } | |
69 | ||
70 | ||
80b4e5f3 TS |
71 | DIR * |
72 | internal_function | |
73 | __opendirat (int dfd, const char *name) | |
74 | { | |
75 | if (name[0] == '\0') | |
76 | { | |
77 | /* POSIX.1-1990 says an empty name gets ENOENT; | |
78 | but `open' might like it fine. */ | |
79 | __set_errno (ENOENT); | |
80 | return NULL; | |
81 | } | |
82 | ||
83 | int flags = O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC; | |
84 | int fd; | |
a3848485 | 85 | #if IS_IN (rtld) |
80b4e5f3 TS |
86 | assert (dfd == AT_FDCWD); |
87 | fd = open_not_cancel_2 (name, flags); | |
88 | #else | |
89 | fd = openat_not_cancel_3 (dfd, name, flags); | |
90 | #endif | |
91 | if (fd < 0) | |
92 | return NULL; | |
93 | ||
94 | /* Extract the pointer to the descriptor structure. */ | |
95 | DIR *dirp = _hurd_fd_opendir (_hurd_fd_get (fd)); | |
96 | if (dirp == NULL) | |
97 | __close (fd); | |
98 | ||
99 | return dirp; | |
100 | } | |
101 | ||
102 | ||
a334319f | 103 | /* Open a directory stream on NAME. */ |
28f540f4 | 104 | DIR * |
a334319f | 105 | __opendir (const char *name) |
28f540f4 | 106 | { |
80b4e5f3 TS |
107 | #if 0 /* TODO. */ |
108 | return __opendirat (AT_FDCWD, name); | |
109 | #else | |
a334319f | 110 | if (name[0] == '\0') |
8db34fe1 | 111 | { |
a334319f UD |
112 | /* POSIX.1-1990 says an empty name gets ENOENT; |
113 | but `open' might like it fine. */ | |
114 | __set_errno (ENOENT); | |
8db34fe1 RM |
115 | return NULL; |
116 | } | |
117 | ||
f96c3e9f | 118 | int fd = __open (name, O_RDONLY | O_NONBLOCK | O_DIRECTORY); |
a334319f UD |
119 | if (fd < 0) |
120 | return NULL; | |
121 | ||
a334319f | 122 | /* Extract the pointer to the descriptor structure. */ |
f96c3e9f | 123 | DIR *dirp = _hurd_fd_opendir (_hurd_fd_get (fd)); |
63be256a RM |
124 | if (dirp == NULL) |
125 | __close (fd); | |
28f540f4 RM |
126 | |
127 | return dirp; | |
80b4e5f3 | 128 | #endif |
28f540f4 | 129 | } |
23396375 | 130 | weak_alias (__opendir, opendir) |