1 /* Filename lookup using a search path
3 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
5 Written by Miles Bader <miles@gnu.ai.mit.edu>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <hurd/lookup.h>
25 /* If FILE_NAME contains a '/', or PATH is NULL, call FUN with FILE_NAME, and
26 return the result (if PREFIXED_NAME is non-NULL, setting *PREFIXED_NAME to
27 NULL). Otherwise, call FUN repeatedly with FILE_NAME prefixed with each
28 successive `:' separated element of PATH, returning whenever FUN returns
29 0 (if PREFIXED_NAME is non-NULL, setting *PREFIXED_NAME to the resulting
30 prefixed path). If FUN never returns 0, return the first non-ENOENT
31 return value, or ENOENT if there is none. */
33 file_name_path_scan (const char *file_name, const char *path,
34 error_t (*fun)(const char *name),
37 if (path == NULL || index (file_name, '/'))
41 return (*fun)(file_name);
46 size_t file_name_len = strlen (file_name);
51 const char *next = index (path, ':') ?: path + strlen (path);
52 size_t pfx_len = next - path;
53 char pfxed_name[pfx_len + 2 + file_name_len + 1];
56 pfxed_name[pfx_len++] = '.';
58 bcopy (path, pfxed_name, pfx_len);
59 if (pfxed_name[pfx_len - 1] != '/')
60 pfxed_name[pfx_len++] = '/';
61 bcopy (file_name, pfxed_name + pfx_len, file_name_len + 1);
63 err = (*fun)(pfxed_name);
67 *prefixed_name = strdup (pfxed_name);
70 if (!real_err && err != ENOENT)
74 return real_err ?: ENOENT;
81 /* Lookup FILE_NAME and return the node opened with FLAGS & MODE in result
82 (see hurd_file_name_lookup for details), but a simple filename (without
83 any directory prefixes) will be consectutively prefixed with the pathnames
84 in the `:' separated list PATH until one succeeds in a successful lookup.
85 If none succeed, then the first error that wasn't ENOENT is returned, or
86 ENOENT if no other errors were returned. If PREFIXED_NAME is non-NULL,
87 then if RESULT is looked up directly, *PREFIXED_NAME is set to NULL, and
88 if it is looked up using a prefix from PATH, *PREFIXED_NAME is set to
89 malloced storage containing the prefixed name. */
91 hurd_file_name_path_lookup (error_t (*use_init_port)
92 (int which, error_t (*operate) (mach_port_t)),
93 file_t (*get_dtable_port) (int fd),
95 (file_t dir, char *name, int flags, mode_t mode,
96 retry_type *do_retry, string_t retry_name,
98 const char *file_name, const char *path,
99 int flags, mode_t mode,
100 file_t *result, char **prefixed_name)
102 error_t scan_lookup (const char *name)
105 __hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
106 name, flags, mode, result);
108 return file_name_path_scan (file_name, path, scan_lookup, prefixed_name);
112 file_name_path_lookup (const char *file_name, const char *path,
113 int flags, mode_t mode, char **prefixed_name)
118 err = hurd_file_name_path_lookup (&_hurd_ports_use, &__getdport, 0,
119 file_name, path, flags, mode,
120 &result, prefixed_name);
122 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;