1 /* Filename lookup using a search path
2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 #include <hurd/lookup.h>
24 /* If FILE_NAME contains a '/', or PATH is NULL, call FUN with FILE_NAME, and
25 return the result (if PREFIXED_NAME is non-NULL, setting *PREFIXED_NAME to
26 NULL). Otherwise, call FUN repeatedly with FILE_NAME prefixed with each
27 successive `:' separated element of PATH, returning whenever FUN returns
28 0 (if PREFIXED_NAME is non-NULL, setting *PREFIXED_NAME to the resulting
29 prefixed path). If FUN never returns 0, return the first non-ENOENT
30 return value, or ENOENT if there is none. */
32 file_name_path_scan (const char *file_name
, const char *path
,
33 error_t (*fun
)(const char *name
),
36 if (path
== NULL
|| index (file_name
, '/'))
40 return (*fun
)(file_name
);
45 size_t file_name_len
= strlen (file_name
);
50 const char *next
= index (path
, ':') ?: path
+ strlen (path
);
51 size_t pfx_len
= next
- path
;
52 char pfxed_name
[pfx_len
+ 2 + file_name_len
+ 1];
55 pfxed_name
[pfx_len
++] = '.';
57 memcpy (pfxed_name
, path
, pfx_len
);
58 if (pfxed_name
[pfx_len
- 1] != '/')
59 pfxed_name
[pfx_len
++] = '/';
60 memcpy (pfxed_name
+ pfx_len
, file_name
, file_name_len
+ 1);
62 err
= (*fun
)(pfxed_name
);
66 *prefixed_name
= strdup (pfxed_name
);
69 if (!real_err
&& err
!= ENOENT
)
73 return real_err
?: ENOENT
;
80 /* Lookup FILE_NAME and return the node opened with FLAGS & MODE in result
81 (see hurd_file_name_lookup for details), but a simple filename (without
82 any directory prefixes) will be consecutively prefixed with the pathnames
83 in the `:' separated list PATH until one succeeds in a successful lookup.
84 If none succeed, then the first error that wasn't ENOENT is returned, or
85 ENOENT if no other errors were returned. If PREFIXED_NAME is non-NULL,
86 then if RESULT is looked up directly, *PREFIXED_NAME is set to NULL, and
87 if it is looked up using a prefix from PATH, *PREFIXED_NAME is set to
88 malloced storage containing the prefixed name. */
90 hurd_file_name_path_lookup (error_t (*use_init_port
)
91 (int which
, error_t (*operate
) (mach_port_t
)),
92 file_t (*get_dtable_port
) (int fd
),
94 (file_t dir
, char *name
, int flags
, mode_t mode
,
95 retry_type
*do_retry
, string_t retry_name
,
97 const char *file_name
, const char *path
,
98 int flags
, mode_t mode
,
99 file_t
*result
, char **prefixed_name
)
101 error_t
scan_lookup (const char *name
)
104 __hurd_file_name_lookup (use_init_port
, get_dtable_port
, lookup
,
105 name
, flags
, mode
, result
);
107 return file_name_path_scan (file_name
, path
, scan_lookup
, prefixed_name
);
111 file_name_path_lookup (const char *file_name
, const char *path
,
112 int flags
, mode_t mode
, char **prefixed_name
)
117 err
= hurd_file_name_path_lookup (&_hurd_ports_use
, &__getdport
, 0,
118 file_name
, path
, flags
, mode
,
119 &result
, prefixed_name
);
121 return err
? (__hurd_fail (err
), MACH_PORT_NULL
) : result
;