Blame


1 80189b3a 2022-08-13 op #include "config.h"
2 80189b3a 2022-08-13 op #if !HAVE_ERR
3 80189b3a 2022-08-13 op /*
4 80189b3a 2022-08-13 op * Copyright (c) 1993
5 80189b3a 2022-08-13 op * The Regents of the University of California. All rights reserved.
6 80189b3a 2022-08-13 op *
7 80189b3a 2022-08-13 op * Redistribution and use in source and binary forms, with or without
8 80189b3a 2022-08-13 op * modification, are permitted provided that the following conditions
9 80189b3a 2022-08-13 op * are met:
10 80189b3a 2022-08-13 op * 1. Redistributions of source code must retain the above copyright
11 80189b3a 2022-08-13 op * notice, this list of conditions and the following disclaimer.
12 80189b3a 2022-08-13 op * 2. Redistributions in binary form must reproduce the above copyright
13 80189b3a 2022-08-13 op * notice, this list of conditions and the following disclaimer in the
14 80189b3a 2022-08-13 op * documentation and/or other materials provided with the distribution.
15 80189b3a 2022-08-13 op * 3. Neither the name of the University nor the names of its contributors
16 80189b3a 2022-08-13 op * may be used to endorse or promote products derived from this software
17 80189b3a 2022-08-13 op * without specific prior written permission.
18 80189b3a 2022-08-13 op *
19 80189b3a 2022-08-13 op * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 80189b3a 2022-08-13 op * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 80189b3a 2022-08-13 op * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 80189b3a 2022-08-13 op * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 80189b3a 2022-08-13 op * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 80189b3a 2022-08-13 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 80189b3a 2022-08-13 op * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 80189b3a 2022-08-13 op * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 80189b3a 2022-08-13 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 80189b3a 2022-08-13 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 80189b3a 2022-08-13 op * SUCH DAMAGE.
30 80189b3a 2022-08-13 op */
31 80189b3a 2022-08-13 op
32 80189b3a 2022-08-13 op #include <errno.h>
33 80189b3a 2022-08-13 op #include <stdarg.h>
34 80189b3a 2022-08-13 op #include <stdio.h>
35 80189b3a 2022-08-13 op #include <stdlib.h>
36 80189b3a 2022-08-13 op #include <string.h>
37 80189b3a 2022-08-13 op
38 80189b3a 2022-08-13 op void
39 80189b3a 2022-08-13 op vwarnx(const char *fmt, va_list ap)
40 80189b3a 2022-08-13 op {
41 80189b3a 2022-08-13 op fprintf(stderr, "%s: ", getprogname());
42 80189b3a 2022-08-13 op if (fmt != NULL)
43 80189b3a 2022-08-13 op vfprintf(stderr, fmt, ap);
44 80189b3a 2022-08-13 op fprintf(stderr, "\n");
45 80189b3a 2022-08-13 op }
46 80189b3a 2022-08-13 op
47 80189b3a 2022-08-13 op void
48 80189b3a 2022-08-13 op vwarnc(int code, const char *fmt, va_list ap)
49 80189b3a 2022-08-13 op {
50 80189b3a 2022-08-13 op fprintf(stderr, "%s: ", getprogname());
51 80189b3a 2022-08-13 op if (fmt != NULL) {
52 80189b3a 2022-08-13 op vfprintf(stderr, fmt, ap);
53 80189b3a 2022-08-13 op fprintf(stderr, ": ");
54 80189b3a 2022-08-13 op }
55 80189b3a 2022-08-13 op fprintf(stderr, "%s\n", strerror(code));
56 80189b3a 2022-08-13 op }
57 80189b3a 2022-08-13 op
58 80189b3a 2022-08-13 op void
59 80189b3a 2022-08-13 op vwarn(const char *fmt, va_list ap)
60 80189b3a 2022-08-13 op {
61 80189b3a 2022-08-13 op int sverrno;
62 80189b3a 2022-08-13 op
63 80189b3a 2022-08-13 op sverrno = errno;
64 80189b3a 2022-08-13 op fprintf(stderr, "%s: ", getprogname());
65 80189b3a 2022-08-13 op if (fmt != NULL) {
66 80189b3a 2022-08-13 op vfprintf(stderr, fmt, ap);
67 80189b3a 2022-08-13 op fprintf(stderr, ": ");
68 80189b3a 2022-08-13 op }
69 80189b3a 2022-08-13 op fprintf(stderr, "%s\n", strerror(sverrno));
70 80189b3a 2022-08-13 op }
71 80189b3a 2022-08-13 op
72 80189b3a 2022-08-13 op void
73 80189b3a 2022-08-13 op verrc(int eval, int code, const char *fmt, va_list ap)
74 80189b3a 2022-08-13 op {
75 80189b3a 2022-08-13 op fprintf(stderr, "%s: ", getprogname());
76 80189b3a 2022-08-13 op if (fmt != NULL) {
77 80189b3a 2022-08-13 op vfprintf(stderr, fmt, ap);
78 80189b3a 2022-08-13 op fprintf(stderr, ": ");
79 80189b3a 2022-08-13 op }
80 80189b3a 2022-08-13 op fprintf(stderr, "%s\n", strerror(code));
81 80189b3a 2022-08-13 op exit(eval);
82 80189b3a 2022-08-13 op }
83 80189b3a 2022-08-13 op
84 80189b3a 2022-08-13 op void
85 80189b3a 2022-08-13 op verrx(int eval, const char *fmt, va_list ap)
86 80189b3a 2022-08-13 op {
87 80189b3a 2022-08-13 op fprintf(stderr, "%s: ", getprogname());
88 80189b3a 2022-08-13 op if (fmt != NULL)
89 80189b3a 2022-08-13 op vfprintf(stderr, fmt, ap);
90 80189b3a 2022-08-13 op fprintf(stderr, "\n");
91 80189b3a 2022-08-13 op exit(eval);
92 80189b3a 2022-08-13 op }
93 80189b3a 2022-08-13 op
94 80189b3a 2022-08-13 op void
95 80189b3a 2022-08-13 op verr(int eval, const char *fmt, va_list ap)
96 80189b3a 2022-08-13 op {
97 80189b3a 2022-08-13 op int sverrno;
98 80189b3a 2022-08-13 op
99 80189b3a 2022-08-13 op sverrno = errno;
100 80189b3a 2022-08-13 op fprintf(stderr, "%s: ", getprogname());
101 80189b3a 2022-08-13 op if (fmt != NULL) {
102 80189b3a 2022-08-13 op vfprintf(stderr, fmt, ap);
103 80189b3a 2022-08-13 op fprintf(stderr, ": ");
104 80189b3a 2022-08-13 op }
105 80189b3a 2022-08-13 op fprintf(stderr, "%s\n", strerror(sverrno));
106 80189b3a 2022-08-13 op exit(eval);
107 80189b3a 2022-08-13 op }
108 80189b3a 2022-08-13 op
109 80189b3a 2022-08-13 op void
110 80189b3a 2022-08-13 op err(int eval, const char *fmt, ...)
111 80189b3a 2022-08-13 op {
112 80189b3a 2022-08-13 op va_list ap;
113 80189b3a 2022-08-13 op
114 80189b3a 2022-08-13 op va_start(ap, fmt);
115 80189b3a 2022-08-13 op verr(eval, fmt, ap);
116 80189b3a 2022-08-13 op va_end(ap);
117 80189b3a 2022-08-13 op }
118 80189b3a 2022-08-13 op
119 80189b3a 2022-08-13 op void
120 80189b3a 2022-08-13 op errc(int eval, int code, const char *fmt, ...)
121 80189b3a 2022-08-13 op {
122 80189b3a 2022-08-13 op va_list ap;
123 80189b3a 2022-08-13 op
124 80189b3a 2022-08-13 op va_start(ap, fmt);
125 80189b3a 2022-08-13 op verrc(eval, code, fmt, ap);
126 80189b3a 2022-08-13 op va_end(ap);
127 80189b3a 2022-08-13 op }
128 80189b3a 2022-08-13 op
129 80189b3a 2022-08-13 op void
130 80189b3a 2022-08-13 op errx(int eval, const char *fmt, ...)
131 80189b3a 2022-08-13 op {
132 80189b3a 2022-08-13 op va_list ap;
133 80189b3a 2022-08-13 op
134 80189b3a 2022-08-13 op va_start(ap, fmt);
135 80189b3a 2022-08-13 op verrx(eval, fmt, ap);
136 80189b3a 2022-08-13 op va_end(ap);
137 80189b3a 2022-08-13 op }
138 80189b3a 2022-08-13 op
139 80189b3a 2022-08-13 op void
140 80189b3a 2022-08-13 op warn(const char *fmt, ...)
141 80189b3a 2022-08-13 op {
142 80189b3a 2022-08-13 op va_list ap;
143 80189b3a 2022-08-13 op
144 80189b3a 2022-08-13 op va_start(ap, fmt);
145 80189b3a 2022-08-13 op vwarn(fmt, ap);
146 80189b3a 2022-08-13 op va_end(ap);
147 80189b3a 2022-08-13 op }
148 80189b3a 2022-08-13 op
149 80189b3a 2022-08-13 op void
150 80189b3a 2022-08-13 op warnc(int code, const char *fmt, ...)
151 80189b3a 2022-08-13 op {
152 80189b3a 2022-08-13 op va_list ap;
153 80189b3a 2022-08-13 op
154 80189b3a 2022-08-13 op va_start(ap, fmt);
155 80189b3a 2022-08-13 op vwarnc(code, fmt, ap);
156 80189b3a 2022-08-13 op va_end(ap);
157 80189b3a 2022-08-13 op }
158 80189b3a 2022-08-13 op
159 80189b3a 2022-08-13 op void
160 80189b3a 2022-08-13 op warnx(const char *fmt, ...)
161 80189b3a 2022-08-13 op {
162 80189b3a 2022-08-13 op va_list ap;
163 80189b3a 2022-08-13 op
164 80189b3a 2022-08-13 op va_start(ap, fmt);
165 80189b3a 2022-08-13 op vwarnx(fmt, ap);
166 80189b3a 2022-08-13 op va_end(ap);
167 80189b3a 2022-08-13 op }
168 80189b3a 2022-08-13 op #endif /* !HAVE_ERR */
169 80189b3a 2022-08-13 op #if !HAVE_GETPROGNAME
170 80189b3a 2022-08-13 op /*
171 80189b3a 2022-08-13 op * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
172 80189b3a 2022-08-13 op * Copyright (c) 2017 Kristaps Dzonsons <kristaps@bsd.lv>
173 80189b3a 2022-08-13 op * Copyright (c) 2020 Stephen Gregoratto <dev@sgregoratto.me>
174 80189b3a 2022-08-13 op *
175 80189b3a 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
176 80189b3a 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
177 80189b3a 2022-08-13 op * copyright notice and this permission notice appear in all copies.
178 80189b3a 2022-08-13 op *
179 80189b3a 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
180 80189b3a 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
181 80189b3a 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
182 80189b3a 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
183 80189b3a 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
184 80189b3a 2022-08-13 op * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
185 80189b3a 2022-08-13 op * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
186 80189b3a 2022-08-13 op */
187 80189b3a 2022-08-13 op
188 80189b3a 2022-08-13 op #include <sys/types.h>
189 80189b3a 2022-08-13 op
190 80189b3a 2022-08-13 op #include <errno.h>
191 80189b3a 2022-08-13 op
192 80189b3a 2022-08-13 op #if HAVE_GETEXECNAME
193 80189b3a 2022-08-13 op #include <stdlib.h>
194 80189b3a 2022-08-13 op const char *
195 80189b3a 2022-08-13 op getprogname(void)
196 80189b3a 2022-08-13 op {
197 80189b3a 2022-08-13 op return getexecname();
198 80189b3a 2022-08-13 op }
199 80189b3a 2022-08-13 op #elif HAVE_PROGRAM_INVOCATION_SHORT_NAME
200 80189b3a 2022-08-13 op const char *
201 80189b3a 2022-08-13 op getprogname(void)
202 80189b3a 2022-08-13 op {
203 80189b3a 2022-08-13 op return (program_invocation_short_name);
204 80189b3a 2022-08-13 op }
205 80189b3a 2022-08-13 op #elif HAVE___PROGNAME
206 80189b3a 2022-08-13 op const char *
207 80189b3a 2022-08-13 op getprogname(void)
208 80189b3a 2022-08-13 op {
209 80189b3a 2022-08-13 op extern char *__progname;
210 80189b3a 2022-08-13 op
211 80189b3a 2022-08-13 op return (__progname);
212 80189b3a 2022-08-13 op }
213 80189b3a 2022-08-13 op #else
214 80189b3a 2022-08-13 op #error No getprogname available.
215 80189b3a 2022-08-13 op #endif
216 80189b3a 2022-08-13 op #endif /* !HAVE_GETPROGNAME */
217 80189b3a 2022-08-13 op #if !HAVE_SETRESGID
218 80189b3a 2022-08-13 op /*
219 80189b3a 2022-08-13 op * Copyright (c) 2004, 2005 Darren Tucker (dtucker at zip com au).
220 80189b3a 2022-08-13 op *
221 80189b3a 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
222 80189b3a 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
223 80189b3a 2022-08-13 op * copyright notice and this permission notice appear in all copies.
224 80189b3a 2022-08-13 op *
225 80189b3a 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
226 80189b3a 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
227 80189b3a 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
228 80189b3a 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
229 80189b3a 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
230 80189b3a 2022-08-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
231 80189b3a 2022-08-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
232 80189b3a 2022-08-13 op */
233 80189b3a 2022-08-13 op
234 80189b3a 2022-08-13 op #include <sys/types.h>
235 80189b3a 2022-08-13 op #include <unistd.h>
236 80189b3a 2022-08-13 op
237 80189b3a 2022-08-13 op int
238 80189b3a 2022-08-13 op setresgid(gid_t rgid, gid_t egid, gid_t sgid)
239 80189b3a 2022-08-13 op {
240 80189b3a 2022-08-13 op /* this is the only configuration tested */
241 80189b3a 2022-08-13 op
242 80189b3a 2022-08-13 op if (rgid != egid || egid != sgid)
243 80189b3a 2022-08-13 op return -1;
244 80189b3a 2022-08-13 op
245 80189b3a 2022-08-13 op if (setregid(rgid, egid) == -1)
246 80189b3a 2022-08-13 op return -1;
247 80189b3a 2022-08-13 op
248 80189b3a 2022-08-13 op return 0;
249 80189b3a 2022-08-13 op }
250 80189b3a 2022-08-13 op #endif /* !HAVE_SETRESGID */
251 80189b3a 2022-08-13 op #if !HAVE_SETRESUID
252 80189b3a 2022-08-13 op /*
253 80189b3a 2022-08-13 op * Copyright (c) 2004, 2005 Darren Tucker (dtucker at zip com au).
254 80189b3a 2022-08-13 op *
255 80189b3a 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
256 80189b3a 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
257 80189b3a 2022-08-13 op * copyright notice and this permission notice appear in all copies.
258 80189b3a 2022-08-13 op *
259 80189b3a 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
260 80189b3a 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
261 80189b3a 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
262 80189b3a 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
263 80189b3a 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
264 80189b3a 2022-08-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
265 80189b3a 2022-08-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
266 80189b3a 2022-08-13 op */
267 80189b3a 2022-08-13 op
268 80189b3a 2022-08-13 op #include <sys/types.h>
269 80189b3a 2022-08-13 op
270 80189b3a 2022-08-13 op #include <errno.h>
271 80189b3a 2022-08-13 op #include <unistd.h>
272 80189b3a 2022-08-13 op
273 80189b3a 2022-08-13 op int
274 80189b3a 2022-08-13 op setresuid(uid_t ruid, uid_t euid, uid_t suid)
275 80189b3a 2022-08-13 op {
276 80189b3a 2022-08-13 op uid_t ouid;
277 80189b3a 2022-08-13 op int ret = -1;
278 80189b3a 2022-08-13 op
279 80189b3a 2022-08-13 op /* Allow only the tested configuration. */
280 80189b3a 2022-08-13 op
281 80189b3a 2022-08-13 op if (ruid != euid || euid != suid) {
282 80189b3a 2022-08-13 op errno = ENOSYS;
283 80189b3a 2022-08-13 op return -1;
284 80189b3a 2022-08-13 op }
285 80189b3a 2022-08-13 op ouid = getuid();
286 80189b3a 2022-08-13 op
287 80189b3a 2022-08-13 op if ((ret = setreuid(euid, euid)) == -1)
288 80189b3a 2022-08-13 op return -1;
289 80189b3a 2022-08-13 op
290 80189b3a 2022-08-13 op /*
291 80189b3a 2022-08-13 op * When real, effective and saved uids are the same and we have
292 80189b3a 2022-08-13 op * changed uids, sanity check that we cannot restore the old uid.
293 80189b3a 2022-08-13 op */
294 80189b3a 2022-08-13 op
295 80189b3a 2022-08-13 op if (ruid == euid && euid == suid && ouid != ruid &&
296 80189b3a 2022-08-13 op setuid(ouid) != -1 && seteuid(ouid) != -1) {
297 80189b3a 2022-08-13 op errno = EINVAL;
298 80189b3a 2022-08-13 op return -1;
299 80189b3a 2022-08-13 op }
300 80189b3a 2022-08-13 op
301 80189b3a 2022-08-13 op /*
302 80189b3a 2022-08-13 op * Finally, check that the real and effective uids are what we
303 80189b3a 2022-08-13 op * expect.
304 80189b3a 2022-08-13 op */
305 80189b3a 2022-08-13 op if (getuid() != ruid || geteuid() != euid) {
306 80189b3a 2022-08-13 op errno = EACCES;
307 80189b3a 2022-08-13 op return -1;
308 80189b3a 2022-08-13 op }
309 80189b3a 2022-08-13 op
310 80189b3a 2022-08-13 op return ret;
311 80189b3a 2022-08-13 op }
312 80189b3a 2022-08-13 op #endif /* !HAVE_SETRESUID */
313 80189b3a 2022-08-13 op #if !HAVE_STRLCAT
314 80189b3a 2022-08-13 op /*
315 80189b3a 2022-08-13 op * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
316 80189b3a 2022-08-13 op *
317 80189b3a 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
318 80189b3a 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
319 80189b3a 2022-08-13 op * copyright notice and this permission notice appear in all copies.
320 80189b3a 2022-08-13 op *
321 80189b3a 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
322 80189b3a 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
323 80189b3a 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
324 80189b3a 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
325 80189b3a 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
326 80189b3a 2022-08-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
327 80189b3a 2022-08-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
328 80189b3a 2022-08-13 op */
329 80189b3a 2022-08-13 op
330 80189b3a 2022-08-13 op #include <sys/types.h>
331 80189b3a 2022-08-13 op #include <string.h>
332 80189b3a 2022-08-13 op
333 80189b3a 2022-08-13 op /*
334 80189b3a 2022-08-13 op * Appends src to string dst of size siz (unlike strncat, siz is the
335 80189b3a 2022-08-13 op * full size of dst, not space left). At most siz-1 characters
336 80189b3a 2022-08-13 op * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
337 80189b3a 2022-08-13 op * Returns strlen(src) + MIN(siz, strlen(initial dst)).
338 80189b3a 2022-08-13 op * If retval >= siz, truncation occurred.
339 80189b3a 2022-08-13 op */
340 80189b3a 2022-08-13 op size_t
341 80189b3a 2022-08-13 op strlcat(char *dst, const char *src, size_t siz)
342 80189b3a 2022-08-13 op {
343 80189b3a 2022-08-13 op char *d = dst;
344 80189b3a 2022-08-13 op const char *s = src;
345 80189b3a 2022-08-13 op size_t n = siz;
346 80189b3a 2022-08-13 op size_t dlen;
347 80189b3a 2022-08-13 op
348 80189b3a 2022-08-13 op /* Find the end of dst and adjust bytes left but don't go past end */
349 80189b3a 2022-08-13 op while (n-- != 0 && *d != '\0')
350 80189b3a 2022-08-13 op d++;
351 80189b3a 2022-08-13 op dlen = d - dst;
352 80189b3a 2022-08-13 op n = siz - dlen;
353 80189b3a 2022-08-13 op
354 80189b3a 2022-08-13 op if (n == 0)
355 80189b3a 2022-08-13 op return(dlen + strlen(s));
356 80189b3a 2022-08-13 op while (*s != '\0') {
357 80189b3a 2022-08-13 op if (n != 1) {
358 80189b3a 2022-08-13 op *d++ = *s;
359 80189b3a 2022-08-13 op n--;
360 80189b3a 2022-08-13 op }
361 80189b3a 2022-08-13 op s++;
362 80189b3a 2022-08-13 op }
363 80189b3a 2022-08-13 op *d = '\0';
364 80189b3a 2022-08-13 op
365 80189b3a 2022-08-13 op return(dlen + (s - src)); /* count does not include NUL */
366 80189b3a 2022-08-13 op }
367 80189b3a 2022-08-13 op #endif /* !HAVE_STRLCAT */
368 80189b3a 2022-08-13 op #if !HAVE_STRLCPY
369 80189b3a 2022-08-13 op /*
370 80189b3a 2022-08-13 op * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
371 80189b3a 2022-08-13 op *
372 80189b3a 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
373 80189b3a 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
374 80189b3a 2022-08-13 op * copyright notice and this permission notice appear in all copies.
375 80189b3a 2022-08-13 op *
376 80189b3a 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
377 80189b3a 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
378 80189b3a 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
379 80189b3a 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
380 80189b3a 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
381 80189b3a 2022-08-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
382 80189b3a 2022-08-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
383 80189b3a 2022-08-13 op */
384 80189b3a 2022-08-13 op
385 80189b3a 2022-08-13 op #include <sys/types.h>
386 80189b3a 2022-08-13 op #include <string.h>
387 80189b3a 2022-08-13 op
388 80189b3a 2022-08-13 op /*
389 80189b3a 2022-08-13 op * Copy src to string dst of size siz. At most siz-1 characters
390 80189b3a 2022-08-13 op * will be copied. Always NUL terminates (unless siz == 0).
391 80189b3a 2022-08-13 op * Returns strlen(src); if retval >= siz, truncation occurred.
392 80189b3a 2022-08-13 op */
393 80189b3a 2022-08-13 op size_t
394 80189b3a 2022-08-13 op strlcpy(char *dst, const char *src, size_t siz)
395 80189b3a 2022-08-13 op {
396 80189b3a 2022-08-13 op char *d = dst;
397 80189b3a 2022-08-13 op const char *s = src;
398 80189b3a 2022-08-13 op size_t n = siz;
399 80189b3a 2022-08-13 op
400 80189b3a 2022-08-13 op /* Copy as many bytes as will fit */
401 80189b3a 2022-08-13 op if (n != 0) {
402 80189b3a 2022-08-13 op while (--n != 0) {
403 80189b3a 2022-08-13 op if ((*d++ = *s++) == '\0')
404 80189b3a 2022-08-13 op break;
405 80189b3a 2022-08-13 op }
406 80189b3a 2022-08-13 op }
407 80189b3a 2022-08-13 op
408 80189b3a 2022-08-13 op /* Not enough room in dst, add NUL and traverse rest of src */
409 80189b3a 2022-08-13 op if (n == 0) {
410 80189b3a 2022-08-13 op if (siz != 0)
411 80189b3a 2022-08-13 op *d = '\0'; /* NUL-terminate dst */
412 80189b3a 2022-08-13 op while (*s++)
413 80189b3a 2022-08-13 op ;
414 80189b3a 2022-08-13 op }
415 80189b3a 2022-08-13 op
416 80189b3a 2022-08-13 op return(s - src - 1); /* count does not include NUL */
417 80189b3a 2022-08-13 op }
418 80189b3a 2022-08-13 op #endif /* !HAVE_STRLCPY */
419 80189b3a 2022-08-13 op #if !HAVE_STRNDUP
420 80189b3a 2022-08-13 op /* $OpenBSD$ */
421 80189b3a 2022-08-13 op /*
422 80189b3a 2022-08-13 op * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
423 80189b3a 2022-08-13 op *
424 80189b3a 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
425 80189b3a 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
426 80189b3a 2022-08-13 op * copyright notice and this permission notice appear in all copies.
427 80189b3a 2022-08-13 op *
428 80189b3a 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
429 80189b3a 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
430 80189b3a 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
431 80189b3a 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
432 80189b3a 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
433 80189b3a 2022-08-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
434 80189b3a 2022-08-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
435 80189b3a 2022-08-13 op */
436 80189b3a 2022-08-13 op
437 80189b3a 2022-08-13 op #include <sys/types.h>
438 80189b3a 2022-08-13 op
439 80189b3a 2022-08-13 op #include <stddef.h>
440 80189b3a 2022-08-13 op #include <stdlib.h>
441 80189b3a 2022-08-13 op #include <string.h>
442 80189b3a 2022-08-13 op
443 80189b3a 2022-08-13 op char *
444 80189b3a 2022-08-13 op strndup(const char *str, size_t maxlen)
445 80189b3a 2022-08-13 op {
446 80189b3a 2022-08-13 op char *copy;
447 80189b3a 2022-08-13 op size_t len;
448 80189b3a 2022-08-13 op
449 80189b3a 2022-08-13 op len = strnlen(str, maxlen);
450 80189b3a 2022-08-13 op copy = malloc(len + 1);
451 80189b3a 2022-08-13 op if (copy != NULL) {
452 80189b3a 2022-08-13 op (void)memcpy(copy, str, len);
453 80189b3a 2022-08-13 op copy[len] = '\0';
454 80189b3a 2022-08-13 op }
455 80189b3a 2022-08-13 op
456 80189b3a 2022-08-13 op return copy;
457 80189b3a 2022-08-13 op }
458 80189b3a 2022-08-13 op #endif /* !HAVE_STRNDUP */
459 80189b3a 2022-08-13 op #if !HAVE_STRNLEN
460 80189b3a 2022-08-13 op /* $OpenBSD$ */
461 80189b3a 2022-08-13 op
462 80189b3a 2022-08-13 op /*
463 80189b3a 2022-08-13 op * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
464 80189b3a 2022-08-13 op *
465 80189b3a 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
466 80189b3a 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
467 80189b3a 2022-08-13 op * copyright notice and this permission notice appear in all copies.
468 80189b3a 2022-08-13 op *
469 80189b3a 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
470 80189b3a 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
471 80189b3a 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
472 80189b3a 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
473 80189b3a 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
474 80189b3a 2022-08-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
475 80189b3a 2022-08-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
476 80189b3a 2022-08-13 op */
477 80189b3a 2022-08-13 op
478 80189b3a 2022-08-13 op #include <sys/types.h>
479 80189b3a 2022-08-13 op #include <string.h>
480 80189b3a 2022-08-13 op
481 80189b3a 2022-08-13 op size_t
482 80189b3a 2022-08-13 op strnlen(const char *str, size_t maxlen)
483 80189b3a 2022-08-13 op {
484 80189b3a 2022-08-13 op const char *cp;
485 80189b3a 2022-08-13 op
486 80189b3a 2022-08-13 op for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
487 80189b3a 2022-08-13 op ;
488 80189b3a 2022-08-13 op
489 80189b3a 2022-08-13 op return (size_t)(cp - str);
490 80189b3a 2022-08-13 op }
491 80189b3a 2022-08-13 op #endif /* !HAVE_STRNLEN */
492 80189b3a 2022-08-13 op #if !HAVE_STRTONUM
493 80189b3a 2022-08-13 op /*
494 80189b3a 2022-08-13 op * Copyright (c) 2004 Ted Unangst and Todd Miller
495 80189b3a 2022-08-13 op * All rights reserved.
496 80189b3a 2022-08-13 op *
497 80189b3a 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
498 80189b3a 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
499 80189b3a 2022-08-13 op * copyright notice and this permission notice appear in all copies.
500 80189b3a 2022-08-13 op *
501 80189b3a 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
502 80189b3a 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
503 80189b3a 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
504 80189b3a 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
505 80189b3a 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
506 80189b3a 2022-08-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
507 80189b3a 2022-08-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
508 80189b3a 2022-08-13 op */
509 80189b3a 2022-08-13 op
510 80189b3a 2022-08-13 op #include <errno.h>
511 80189b3a 2022-08-13 op #include <limits.h>
512 80189b3a 2022-08-13 op #include <stdlib.h>
513 80189b3a 2022-08-13 op
514 80189b3a 2022-08-13 op #define INVALID 1
515 80189b3a 2022-08-13 op #define TOOSMALL 2
516 80189b3a 2022-08-13 op #define TOOLARGE 3
517 80189b3a 2022-08-13 op
518 80189b3a 2022-08-13 op long long
519 80189b3a 2022-08-13 op strtonum(const char *numstr, long long minval, long long maxval,
520 80189b3a 2022-08-13 op const char **errstrp)
521 80189b3a 2022-08-13 op {
522 80189b3a 2022-08-13 op long long ll = 0;
523 80189b3a 2022-08-13 op int error = 0;
524 80189b3a 2022-08-13 op char *ep;
525 80189b3a 2022-08-13 op struct errval {
526 80189b3a 2022-08-13 op const char *errstr;
527 80189b3a 2022-08-13 op int err;
528 80189b3a 2022-08-13 op } ev[4] = {
529 80189b3a 2022-08-13 op { NULL, 0 },
530 80189b3a 2022-08-13 op { "invalid", EINVAL },
531 80189b3a 2022-08-13 op { "too small", ERANGE },
532 80189b3a 2022-08-13 op { "too large", ERANGE },
533 80189b3a 2022-08-13 op };
534 80189b3a 2022-08-13 op
535 80189b3a 2022-08-13 op ev[0].err = errno;
536 80189b3a 2022-08-13 op errno = 0;
537 80189b3a 2022-08-13 op if (minval > maxval) {
538 80189b3a 2022-08-13 op error = INVALID;
539 80189b3a 2022-08-13 op } else {
540 80189b3a 2022-08-13 op ll = strtoll(numstr, &ep, 10);
541 80189b3a 2022-08-13 op if (numstr == ep || *ep != '\0')
542 80189b3a 2022-08-13 op error = INVALID;
543 80189b3a 2022-08-13 op else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
544 80189b3a 2022-08-13 op error = TOOSMALL;
545 80189b3a 2022-08-13 op else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
546 80189b3a 2022-08-13 op error = TOOLARGE;
547 80189b3a 2022-08-13 op }
548 80189b3a 2022-08-13 op if (errstrp != NULL)
549 80189b3a 2022-08-13 op *errstrp = ev[error].errstr;
550 80189b3a 2022-08-13 op errno = ev[error].err;
551 80189b3a 2022-08-13 op if (error)
552 80189b3a 2022-08-13 op ll = 0;
553 80189b3a 2022-08-13 op
554 80189b3a 2022-08-13 op return (ll);
555 80189b3a 2022-08-13 op }
556 80189b3a 2022-08-13 op #endif /* !HAVE_STRTONUM */