* sysdeps/powerpc/powerpc64/setjmp-common.S: Make sure pointer guard
[glibc.git] / hurd / hurdlookup.c
blob77de27150ac23e038400e8001d59c37e9af53eae
1 /* Copyright (C) 1992,93,94,95,96,97,99,2001,2004
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 <hurd.h>
21 #include <hurd/lookup.h>
22 #include <string.h>
23 #include <fcntl.h>
26 /* Translate the error from dir_lookup into the error the user sees. */
27 static inline error_t
28 lookup_error (error_t error)
30 switch (error)
32 case EOPNOTSUPP:
33 case MIG_BAD_ID:
34 /* These indicate that the server does not understand dir_lookup
35 at all. If it were a directory, it would, by definition. */
36 return ENOTDIR;
37 default:
38 return error;
42 error_t
43 __hurd_file_name_lookup (error_t (*use_init_port)
44 (int which, error_t (*operate) (file_t)),
45 file_t (*get_dtable_port) (int fd),
46 error_t (*lookup)
47 (file_t dir, char *name, int flags, mode_t mode,
48 retry_type *do_retry, string_t retry_name,
49 mach_port_t *result),
50 const char *file_name, int flags, mode_t mode,
51 file_t *result)
53 error_t err;
54 enum retry_type doretry;
55 char retryname[1024]; /* XXX string_t LOSES! */
56 int startport;
58 error_t lookup_op (mach_port_t startdir)
60 return lookup_error ((*lookup) (startdir, file_name, flags, mode,
61 &doretry, retryname, result));
64 if (! lookup)
65 lookup = __dir_lookup;
67 if (file_name[0] == '\0')
68 return ENOENT;
70 startport = (file_name[0] == '/') ? INIT_PORT_CRDIR : INIT_PORT_CWDIR;
71 while (file_name[0] == '/')
72 file_name++;
74 if (flags & O_NOFOLLOW) /* See lookup-retry.c about O_NOFOLLOW. */
75 flags |= O_NOTRANS;
77 if (flags & O_DIRECTORY)
79 /* The caller wants to require that the file we look up is a directory.
80 We can do this without an extra RPC by appending a trailing slash
81 to the file name we look up. */
82 size_t len = strlen (file_name);
83 if (len == 0)
84 file_name = "/";
85 else if (file_name[len - 1] != '/')
87 char *n = alloca (len + 2);
88 memcpy (n, file_name, len);
89 n[len] = '/';
90 n[len + 1] = '\0';
91 file_name = n;
95 err = (*use_init_port) (startport, &lookup_op);
96 if (! err)
97 err = __hurd_file_name_lookup_retry (use_init_port, get_dtable_port,
98 lookup, doretry, retryname,
99 flags, mode, result);
101 return err;
103 weak_alias (__hurd_file_name_lookup, hurd_file_name_lookup)
105 error_t
106 __hurd_file_name_split (error_t (*use_init_port)
107 (int which, error_t (*operate) (file_t)),
108 file_t (*get_dtable_port) (int fd),
109 error_t (*lookup)
110 (file_t dir, char *name, int flags, mode_t mode,
111 retry_type *do_retry, string_t retry_name,
112 mach_port_t *result),
113 const char *file_name,
114 file_t *dir, char **name)
116 error_t addref (file_t crdir)
118 *dir = crdir;
119 return __mach_port_mod_refs (__mach_task_self (),
120 crdir, MACH_PORT_RIGHT_SEND, +1);
123 const char *lastslash = strrchr (file_name, '/');
125 if (lastslash != NULL)
127 if (lastslash == file_name)
129 /* "/foobar" => crdir + "foobar". */
130 *name = (char *) file_name + 1;
131 return (*use_init_port) (INIT_PORT_CRDIR, &addref);
133 else
135 /* "/dir1/dir2/.../file". */
136 char dirname[lastslash - file_name + 1];
137 memcpy (dirname, file_name, lastslash - file_name);
138 dirname[lastslash - file_name] = '\0';
139 *name = (char *) lastslash + 1;
140 return
141 __hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
142 dirname, 0, 0, dir);
145 else if (file_name[0] == '\0')
146 return ENOENT;
147 else
149 /* "foobar" => cwdir + "foobar". */
150 *name = (char *) file_name;
151 return (*use_init_port) (INIT_PORT_CWDIR, &addref);
154 weak_alias (__hurd_file_name_split, hurd_file_name_split)
156 /* This is the same as hurd_file_name_split, except that it ignores
157 trailing slashes (so *NAME is never ""). */
158 error_t
159 __hurd_directory_name_split (error_t (*use_init_port)
160 (int which, error_t (*operate) (file_t)),
161 file_t (*get_dtable_port) (int fd),
162 error_t (*lookup)
163 (file_t dir, char *name, int flags, mode_t mode,
164 retry_type *do_retry, string_t retry_name,
165 mach_port_t *result),
166 const char *file_name,
167 file_t *dir, char **name)
169 error_t addref (file_t crdir)
171 *dir = crdir;
172 return __mach_port_mod_refs (__mach_task_self (),
173 crdir, MACH_PORT_RIGHT_SEND, +1);
176 const char *lastslash = strrchr (file_name, '/');
178 if (lastslash != NULL && lastslash[1] == '\0')
180 /* Trailing slash doesn't count. Look back further. */
182 /* Back up over all trailing slashes. */
183 while (lastslash > file_name && *lastslash == '/')
184 --lastslash;
186 /* Find the last one earlier in the string, before the trailing ones. */
187 #if __GLIBC__ > 2 || __GLIBC_MINOR__ >= 2
188 lastslash = __memrchr (file_name, '/', lastslash - file_name);
189 #else
190 /* Keep backing up, looking for a slash. */
192 if (lastslash == file_name)
194 /* Hit the start with no slash. */
195 lastslash = NULL;
196 break;
198 while (*lastslash-- != '/');
199 #endif
202 if (lastslash != NULL)
204 if (lastslash == file_name)
206 /* "/foobar" => crdir + "foobar". */
207 *name = (char *) file_name + 1;
208 return (*use_init_port) (INIT_PORT_CRDIR, &addref);
210 else
212 /* "/dir1/dir2/.../file". */
213 char dirname[lastslash - file_name + 1];
214 memcpy (dirname, file_name, lastslash - file_name);
215 dirname[lastslash - file_name] = '\0';
216 *name = (char *) lastslash + 1;
217 return
218 __hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
219 dirname, 0, 0, dir);
222 else if (file_name[0] == '\0')
223 return ENOENT;
224 else
226 /* "foobar" => cwdir + "foobar". */
227 *name = (char *) file_name;
228 return (*use_init_port) (INIT_PORT_CWDIR, &addref);
231 weak_alias (__hurd_directory_name_split, hurd_directory_name_split)
234 file_t
235 __file_name_lookup (const char *file_name, int flags, mode_t mode)
237 error_t err;
238 file_t result;
240 err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0,
241 file_name, flags, mode & ~_hurd_umask,
242 &result);
244 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
246 weak_alias (__file_name_lookup, file_name_lookup)
249 file_t
250 __file_name_split (const char *file_name, char **name)
252 error_t err;
253 file_t result;
255 err = __hurd_file_name_split (&_hurd_ports_use, &__getdport, 0,
256 file_name, &result, name);
258 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
260 weak_alias (__file_name_split, file_name_split)
262 file_t
263 __directory_name_split (const char *directory_name, char **name)
265 error_t err;
266 file_t result;
268 err = __hurd_directory_name_split (&_hurd_ports_use, &__getdport, 0,
269 directory_name, &result, name);
271 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
273 weak_alias (__directory_name_split, directory_name_split)
276 file_t
277 __file_name_lookup_under (file_t startdir,
278 const char *file_name, int flags, mode_t mode)
280 error_t err;
281 file_t result;
283 error_t use_init_port (int which, error_t (*operate) (mach_port_t))
285 return (which == INIT_PORT_CWDIR ? (*operate) (startdir) :
286 _hurd_ports_use (which, operate));
289 err = __hurd_file_name_lookup (&use_init_port, &__getdport, 0,
290 file_name, flags, mode & ~_hurd_umask,
291 &result);
293 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
295 weak_alias (__file_name_lookup_under, file_name_lookup_under)