string: Fix tester with fortify enabled
[glibc.git] / hurd / lookup-at.c
blob88c8377941020b6262a4ca1236b6c24814b9da07
1 /* Lookup helper function for Hurd implementation of *at functions.
2 Copyright (C) 2006-2023 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, see
17 <https://www.gnu.org/licenses/>. */
19 #include <hurd.h>
20 #include <hurd/lookup.h>
21 #include <hurd/fd.h>
22 #include <string.h>
23 #include <fcntl.h>
25 file_t
26 __file_name_lookup_at (int fd, int at_flags,
27 const char *file_name, int flags, mode_t mode)
29 error_t err;
30 file_t result;
31 int empty = at_flags & AT_EMPTY_PATH;
32 int orig_flags;
34 at_flags &= ~AT_EMPTY_PATH;
36 err = __hurd_at_flags (&at_flags, &flags);
37 if (err)
38 return (__hurd_fail (err), MACH_PORT_NULL);
40 if (empty != 0 && file_name[0] == '\0')
42 enum retry_type doretry;
43 char retryname[1024]; /* XXX string_t LOSES! */
45 err = HURD_DPORT_USE (fd, __dir_lookup (port, "", flags, mode,
46 &doretry, retryname,
47 &result));
49 if (! err)
50 err = __hurd_file_name_lookup_retry (&_hurd_ports_use, &__getdport,
51 NULL, doretry, retryname,
52 flags, mode, &result);
54 return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
57 orig_flags = flags;
58 if (flags & O_TMPFILE)
59 flags = O_DIRECTORY;
61 if (fd == AT_FDCWD || file_name[0] == '/')
63 err = __hurd_file_name_lookup (&_hurd_ports_use, &__getdport, 0,
64 file_name, flags, mode & ~_hurd_umask,
65 &result);
66 if (err)
68 __hurd_fail (err);
69 return MACH_PORT_NULL;
72 else
74 file_t startdir;
75 /* We need to look the file up relative to the given directory (and
76 not our cwd). For this to work, we supply our own wrapper for
77 _hurd_ports_use, which replaces cwd with our startdir. */
78 error_t use_init_port (int which, error_t (*operate) (mach_port_t))
80 return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
81 : _hurd_ports_use (which, operate));
84 err = HURD_DPORT_USE (fd, (startdir = port,
85 __hurd_file_name_lookup (&use_init_port,
86 &__getdport, NULL,
87 file_name,
88 flags,
89 mode & ~_hurd_umask,
90 &result)));
91 if (err)
93 __hurd_dfail (fd, err);
94 return MACH_PORT_NULL;
98 if (orig_flags & O_TMPFILE)
100 /* What we have looked up is not the file itself, but actually
101 the directory to create the file in. Do that now. */
102 file_t dir = result;
104 err = __dir_mkfile (dir, orig_flags & ~(O_TMPFILE | O_DIRECTORY),
105 mode, &result);
106 __mach_port_deallocate (__mach_task_self (), dir);
107 if (err)
109 __hurd_fail (err);
110 return MACH_PORT_NULL;
114 return result;
117 file_t
118 __file_name_split_at (int fd, const char *file_name, char **name)
120 error_t err;
121 file_t result;
123 if (fd == AT_FDCWD || file_name[0] == '/')
124 return __file_name_split (file_name, name);
126 err = __hurd_file_name_split (&_hurd_ports_use, &__getdport, 0,
127 file_name, &result, name);
129 file_t startdir;
130 error_t use_init_port (int which, error_t (*operate) (mach_port_t))
132 return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
133 : _hurd_ports_use (which, operate));
136 err = HURD_DPORT_USE (fd, (startdir = port,
137 __hurd_file_name_split (&use_init_port,
138 &__getdport, 0,
139 file_name,
140 &result, name)));
142 return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;
145 file_t
146 __directory_name_split_at (int fd, const char *directory_name, char **name)
148 error_t err;
149 file_t result;
151 if (fd == AT_FDCWD || directory_name[0] == '/')
152 return __directory_name_split (directory_name, name);
154 file_t startdir;
155 error_t use_init_port (int which, error_t (*operate) (mach_port_t))
157 return (which == INIT_PORT_CWDIR ? (*operate) (startdir)
158 : _hurd_ports_use (which, operate));
161 err = HURD_DPORT_USE (fd, (startdir = port,
162 __hurd_directory_name_split (&use_init_port,
163 &__getdport, 0,
164 directory_name,
165 &result, name)));
167 return err ? (__hurd_dfail (fd, err), MACH_PORT_NULL) : result;