(rfc3484_sort): Make sure that even if qsort doesn't support
[glibc.git] / hurd / hurdlookup.c
blob8270132b903bb827b02c9e1390d492610b491f27
1 /* Copyright (C) 1992,1993,1994,1995,1996,1997,1999,2001,2004,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 <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 lastslash = __memrchr (file_name, '/', lastslash - file_name);
190 if (lastslash != NULL)
192 if (lastslash == file_name)
194 /* "/foobar" => crdir + "foobar". */
195 *name = (char *) file_name + 1;
196 return (*use_init_port) (INIT_PORT_CRDIR, &addref);
198 else
200 /* "/dir1/dir2/.../file". */
201 char dirname[lastslash - file_name + 1];
202 memcpy (dirname, file_name, lastslash - file_name);
203 dirname[lastslash - file_name] = '\0';
204 *name = (char *) lastslash + 1;
205 return
206 __hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
207 dirname, 0, 0, dir);
210 else if (file_name[0] == '\0')
211 return ENOENT;
212 else
214 /* "foobar" => cwdir + "foobar". */
215 *name = (char *) file_name;
216 return (*use_init_port) (INIT_PORT_CWDIR, &addref);
219 weak_alias (__hurd_directory_name_split, hurd_directory_name_split)
222 file_t
223 __file_name_lookup (const char *file_name, int flags, mode_t mode)
225 error_t err;
226 file_t result;
228 err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0,
229 file_name, flags, mode & ~_hurd_umask,
230 &result);
232 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
234 weak_alias (__file_name_lookup, file_name_lookup)
237 file_t
238 __file_name_split (const char *file_name, char **name)
240 error_t err;
241 file_t result;
243 err = __hurd_file_name_split (&_hurd_ports_use, &__getdport, 0,
244 file_name, &result, name);
246 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
248 weak_alias (__file_name_split, file_name_split)
250 file_t
251 __directory_name_split (const char *directory_name, char **name)
253 error_t err;
254 file_t result;
256 err = __hurd_directory_name_split (&_hurd_ports_use, &__getdport, 0,
257 directory_name, &result, name);
259 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
261 weak_alias (__directory_name_split, directory_name_split)
264 file_t
265 __file_name_lookup_under (file_t startdir,
266 const char *file_name, int flags, mode_t mode)
268 error_t err;
269 file_t result;
271 error_t use_init_port (int which, error_t (*operate) (mach_port_t))
273 return (which == INIT_PORT_CWDIR ? (*operate) (startdir) :
274 _hurd_ports_use (which, operate));
277 err = __hurd_file_name_lookup (&use_init_port, &__getdport, 0,
278 file_name, flags, mode & ~_hurd_umask,
279 &result);
281 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
283 weak_alias (__file_name_lookup_under, file_name_lookup_under)