5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 vwarnx(const char *fmt, va_list ap)
41 fprintf(stderr, "%s: ", getprogname());
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
48 vwarnc(int code, const char *fmt, va_list ap)
50 fprintf(stderr, "%s: ", getprogname());
52 vfprintf(stderr, fmt, ap);
53 fprintf(stderr, ": ");
55 fprintf(stderr, "%s\n", strerror(code));
59 vwarn(const char *fmt, va_list ap)
64 fprintf(stderr, "%s: ", getprogname());
66 vfprintf(stderr, fmt, ap);
67 fprintf(stderr, ": ");
69 fprintf(stderr, "%s\n", strerror(sverrno));
73 verrc(int eval, int code, const char *fmt, va_list ap)
75 fprintf(stderr, "%s: ", getprogname());
77 vfprintf(stderr, fmt, ap);
78 fprintf(stderr, ": ");
80 fprintf(stderr, "%s\n", strerror(code));
85 verrx(int eval, const char *fmt, va_list ap)
87 fprintf(stderr, "%s: ", getprogname());
89 vfprintf(stderr, fmt, ap);
90 fprintf(stderr, "\n");
95 verr(int eval, const char *fmt, va_list ap)
100 fprintf(stderr, "%s: ", getprogname());
102 vfprintf(stderr, fmt, ap);
103 fprintf(stderr, ": ");
105 fprintf(stderr, "%s\n", strerror(sverrno));
110 err(int eval, const char *fmt, ...)
120 errc(int eval, int code, const char *fmt, ...)
125 verrc(eval, code, fmt, ap);
130 errx(int eval, const char *fmt, ...)
135 verrx(eval, fmt, ap);
140 warn(const char *fmt, ...)
150 warnc(int code, const char *fmt, ...)
155 vwarnc(code, fmt, ap);
160 warnx(const char *fmt, ...)
168 #endif /* !HAVE_ERR */
169 #if !HAVE_GETPROGNAME
171 * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
172 * Copyright (c) 2017 Kristaps Dzonsons <kristaps@bsd.lv>
173 * Copyright (c) 2020 Stephen Gregoratto <dev@sgregoratto.me>
175 * Permission to use, copy, modify, and distribute this software for any
176 * purpose with or without fee is hereby granted, provided that the above
177 * copyright notice and this permission notice appear in all copies.
179 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
180 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
181 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
182 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
183 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
184 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
185 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
188 #include <sys/types.h>
197 return getexecname();
199 #elif HAVE_PROGRAM_INVOCATION_SHORT_NAME
203 return (program_invocation_short_name);
205 #elif HAVE___PROGNAME
209 extern char *__progname;
214 #error No getprogname available.
216 #endif /* !HAVE_GETPROGNAME */
219 * Copyright (c) 2004, 2005 Darren Tucker (dtucker at zip com au).
221 * Permission to use, copy, modify, and distribute this software for any
222 * purpose with or without fee is hereby granted, provided that the above
223 * copyright notice and this permission notice appear in all copies.
225 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
226 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
227 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
228 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
229 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
230 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
231 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
234 #include <sys/types.h>
238 setresgid(gid_t rgid, gid_t egid, gid_t sgid)
240 /* this is the only configuration tested */
242 if (rgid != egid || egid != sgid)
245 if (setregid(rgid, egid) == -1)
250 #endif /* !HAVE_SETRESGID */
253 * Copyright (c) 2004, 2005 Darren Tucker (dtucker at zip com au).
255 * Permission to use, copy, modify, and distribute this software for any
256 * purpose with or without fee is hereby granted, provided that the above
257 * copyright notice and this permission notice appear in all copies.
259 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
260 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
261 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
262 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
263 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
264 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
265 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
268 #include <sys/types.h>
274 setresuid(uid_t ruid, uid_t euid, uid_t suid)
279 /* Allow only the tested configuration. */
281 if (ruid != euid || euid != suid) {
287 if ((ret = setreuid(euid, euid)) == -1)
291 * When real, effective and saved uids are the same and we have
292 * changed uids, sanity check that we cannot restore the old uid.
295 if (ruid == euid && euid == suid && ouid != ruid &&
296 setuid(ouid) != -1 && seteuid(ouid) != -1) {
302 * Finally, check that the real and effective uids are what we
305 if (getuid() != ruid || geteuid() != euid) {
312 #endif /* !HAVE_SETRESUID */
315 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
317 * Permission to use, copy, modify, and distribute this software for any
318 * purpose with or without fee is hereby granted, provided that the above
319 * copyright notice and this permission notice appear in all copies.
321 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
322 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
323 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
324 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
325 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
326 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
327 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
330 #include <sys/types.h>
334 * Appends src to string dst of size siz (unlike strncat, siz is the
335 * full size of dst, not space left). At most siz-1 characters
336 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
337 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
338 * If retval >= siz, truncation occurred.
341 strlcat(char *dst, const char *src, size_t siz)
348 /* Find the end of dst and adjust bytes left but don't go past end */
349 while (n-- != 0 && *d != '\0')
355 return(dlen + strlen(s));
365 return(dlen + (s - src)); /* count does not include NUL */
367 #endif /* !HAVE_STRLCAT */
370 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
372 * Permission to use, copy, modify, and distribute this software for any
373 * purpose with or without fee is hereby granted, provided that the above
374 * copyright notice and this permission notice appear in all copies.
376 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
377 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
378 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
379 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
380 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
381 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
382 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
385 #include <sys/types.h>
389 * Copy src to string dst of size siz. At most siz-1 characters
390 * will be copied. Always NUL terminates (unless siz == 0).
391 * Returns strlen(src); if retval >= siz, truncation occurred.
394 strlcpy(char *dst, const char *src, size_t siz)
400 /* Copy as many bytes as will fit */
403 if ((*d++ = *s++) == '\0')
408 /* Not enough room in dst, add NUL and traverse rest of src */
411 *d = '\0'; /* NUL-terminate dst */
416 return(s - src - 1); /* count does not include NUL */
418 #endif /* !HAVE_STRLCPY */
422 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
424 * Permission to use, copy, modify, and distribute this software for any
425 * purpose with or without fee is hereby granted, provided that the above
426 * copyright notice and this permission notice appear in all copies.
428 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
429 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
430 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
431 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
432 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
433 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
434 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
437 #include <sys/types.h>
444 strndup(const char *str, size_t maxlen)
449 len = strnlen(str, maxlen);
450 copy = malloc(len + 1);
452 (void)memcpy(copy, str, len);
458 #endif /* !HAVE_STRNDUP */
463 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
465 * Permission to use, copy, modify, and distribute this software for any
466 * purpose with or without fee is hereby granted, provided that the above
467 * copyright notice and this permission notice appear in all copies.
469 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
470 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
471 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
472 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
473 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
474 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
475 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
478 #include <sys/types.h>
482 strnlen(const char *str, size_t maxlen)
486 for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
489 return (size_t)(cp - str);
491 #endif /* !HAVE_STRNLEN */
494 * Copyright (c) 2004 Ted Unangst and Todd Miller
495 * All rights reserved.
497 * Permission to use, copy, modify, and distribute this software for any
498 * purpose with or without fee is hereby granted, provided that the above
499 * copyright notice and this permission notice appear in all copies.
501 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
502 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
503 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
504 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
505 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
506 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
507 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
519 strtonum(const char *numstr, long long minval, long long maxval,
520 const char **errstrp)
530 { "invalid", EINVAL },
531 { "too small", ERANGE },
532 { "too large", ERANGE },
537 if (minval > maxval) {
540 ll = strtoll(numstr, &ep, 10);
541 if (numstr == ep || *ep != '\0')
543 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
545 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
549 *errstrp = ev[error].errstr;
550 errno = ev[error].err;
556 #endif /* !HAVE_STRTONUM */