]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/path-lookup.c
Fix BZ #21654 - grp-merge.c alignment
[thirdparty/glibc.git] / hurd / path-lookup.c
CommitLineData
8f0c527e 1/* Filename lookup using a search path
bfff8b1b 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
478b92f0 3 This file is part of the GNU C Library.
8f0c527e
RM
4 Written by Miles Bader <miles@gnu.ai.mit.edu>
5
478b92f0 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
8f0c527e 10
478b92f0
UD
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
8f0c527e 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
8f0c527e 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
8f0c527e
RM
19
20#include <string.h>
21#include <hurd.h>
22#include <hurd/lookup.h>
23
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. */
31error_t
32file_name_path_scan (const char *file_name, const char *path,
33 error_t (*fun)(const char *name),
34 char **prefixed_name)
35{
c7a37ad3 36 if (path == NULL || strchr (file_name, '/'))
8f0c527e
RM
37 {
38 if (prefixed_name)
39 *prefixed_name = 0;
40 return (*fun)(file_name);
41 }
42 else
43 {
44 error_t real_err = 0;
45 size_t file_name_len = strlen (file_name);
46
47 for (;;)
48 {
49 error_t err;
c7a37ad3 50 const char *next = strchr (path, ':') ?: path + strlen (path);
8f0c527e
RM
51 size_t pfx_len = next - path;
52 char pfxed_name[pfx_len + 2 + file_name_len + 1];
53
54 if (pfx_len == 0)
55 pfxed_name[pfx_len++] = '.';
56 else
9596d0dd 57 memcpy (pfxed_name, path, pfx_len);
8f0c527e
RM
58 if (pfxed_name[pfx_len - 1] != '/')
59 pfxed_name[pfx_len++] = '/';
9596d0dd 60 memcpy (pfxed_name + pfx_len, file_name, file_name_len + 1);
8f0c527e
RM
61
62 err = (*fun)(pfxed_name);
63 if (err == 0)
64 {
65 if (prefixed_name)
66 *prefixed_name = strdup (pfxed_name);
67 return 0;
68 }
69 if (!real_err && err != ENOENT)
70 real_err = err;
71
72 if (*next == '\0')
73 return real_err ?: ENOENT;
74 else
75 path = next + 1;
76 }
77 }
78}
79
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
6d52618b 82 any directory prefixes) will be consecutively prefixed with the pathnames
8f0c527e
RM
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. */
89error_t
90hurd_file_name_path_lookup (error_t (*use_init_port)
bf0f4720 91 (int which, error_t (*operate) (mach_port_t)),
8f0c527e 92 file_t (*get_dtable_port) (int fd),
bf0f4720
MB
93 error_t (*lookup)
94 (file_t dir, char *name, int flags, mode_t mode,
95 retry_type *do_retry, string_t retry_name,
96 mach_port_t *result),
8f0c527e
RM
97 const char *file_name, const char *path,
98 int flags, mode_t mode,
99 file_t *result, char **prefixed_name)
100{
bf0f4720 101 error_t scan_lookup (const char *name)
8f0c527e
RM
102 {
103 return
bf0f4720 104 __hurd_file_name_lookup (use_init_port, get_dtable_port, lookup,
8f0c527e
RM
105 name, flags, mode, result);
106 }
bf0f4720 107 return file_name_path_scan (file_name, path, scan_lookup, prefixed_name);
8f0c527e
RM
108}
109
110file_t
111file_name_path_lookup (const char *file_name, const char *path,
112 int flags, mode_t mode, char **prefixed_name)
113{
114 error_t err;
115 file_t result;
116
bf0f4720 117 err = hurd_file_name_path_lookup (&_hurd_ports_use, &__getdport, 0,
8f0c527e
RM
118 file_name, path, flags, mode,
119 &result, prefixed_name);
120
121 return err ? (__hurd_fail (err), MACH_PORT_NULL) : result;
122}