scdoc2mdoc

A fork of scdoc to output mdoc(7)
git clone git://git.sgregoratto.me/scdoc2mdoc
Log | Files | Refs | README | LICENSE

compats.c (7000B)


      1 #include "config.h"
      2 #if !HAVE_ERR
      3 /*
      4  * Copyright (c) 1993
      5  *      The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     18  *
     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
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <errno.h>
     33 #include <stdarg.h>
     34 #include <stdio.h>
     35 #include <stdlib.h>
     36 #include <string.h>
     37 
     38 void
     39 vwarnx(const char *fmt, va_list ap)
     40 {
     41 	fprintf(stderr, "%s: ", getprogname());
     42 	if (fmt != NULL)
     43 		vfprintf(stderr, fmt, ap);
     44 }
     45 
     46 void
     47 vwarn(const char *fmt, va_list ap)
     48 {
     49 	int sverrno;
     50 
     51 	sverrno = errno;
     52 	vwarnx(fmt, ap);
     53 	if (fmt != NULL)
     54 		fputs(": ", stderr);
     55 	fprintf(stderr, "%s\n", strerror(sverrno));
     56 }
     57 
     58 void
     59 err(int eval, const char *fmt, ...)
     60 {
     61 	va_list ap;
     62 
     63 	va_start(ap, fmt);
     64 	vwarn(fmt, ap);
     65 	va_end(ap);
     66 	exit(eval);
     67 }
     68 
     69 void
     70 errx(int eval, const char *fmt, ...)
     71 {
     72 	va_list ap;
     73 
     74 	va_start(ap, fmt);
     75 	vwarnx(fmt, ap);
     76 	va_end(ap);
     77 	fputc('\n', stderr);
     78 	exit(eval);
     79 }
     80 
     81 void
     82 warn(const char *fmt, ...)
     83 {
     84 	va_list ap;
     85 
     86 	va_start(ap, fmt);
     87 	vwarn(fmt, ap);
     88 	va_end(ap);
     89 }
     90 
     91 void
     92 warnx(const char *fmt, ...)
     93 {
     94 	va_list ap;
     95 
     96 	va_start(ap, fmt);
     97 	vwarnx(fmt, ap);
     98 	va_end(ap);
     99 	fputc('\n', stderr);
    100 }
    101 #endif /* !HAVE_ERR */
    102 #if !HAVE_GETPROGNAME
    103 /*
    104  * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
    105  * Copyright (c) 2017 Kristaps Dzonsons <kristaps@bsd.lv>
    106  *
    107  * Permission to use, copy, modify, and distribute this software for any
    108  * purpose with or without fee is hereby granted, provided that the above
    109  * copyright notice and this permission notice appear in all copies.
    110  *
    111  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    112  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    113  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    114  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    115  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
    116  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
    117  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    118  */
    119 
    120 #include <sys/types.h>
    121 
    122 #include <errno.h>
    123 
    124 #if HAVE_PROGRAM_INVOCATION_SHORT_NAME
    125 const char *
    126 getprogname(void)
    127 {
    128 	return (program_invocation_short_name);
    129 }
    130 #elif HAVE___PROGNAME
    131 const char *
    132 getprogname(void)
    133 {
    134 	extern char	*__progname;
    135 
    136 	return (__progname);
    137 }
    138 #else
    139 static const char *progname;
    140 
    141 void
    142 setprogname(const char *name)
    143 {
    144 	progname = name;
    145 }
    146 
    147 const char *
    148 getprogname(void)
    149 {
    150 	return progname;
    151 }
    152 #endif
    153 #endif /* !HAVE_GETPROGNAME */
    154 #if !HAVE_REALLOCARRAY
    155 /*
    156  * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
    157  *
    158  * Permission to use, copy, modify, and distribute this software for any
    159  * purpose with or without fee is hereby granted, provided that the above
    160  * copyright notice and this permission notice appear in all copies.
    161  *
    162  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    163  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    164  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    165  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    166  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    167  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    168  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    169  */
    170 
    171 #include <sys/types.h>
    172 #include <errno.h>
    173 #include <stdint.h>
    174 #include <stdlib.h>
    175 
    176 /*
    177  * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
    178  * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
    179  */
    180 #define MUL_NO_OVERFLOW	((size_t)1 << (sizeof(size_t) * 4))
    181 
    182 void *
    183 reallocarray(void *optr, size_t nmemb, size_t size)
    184 {
    185 	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
    186 	    nmemb > 0 && SIZE_MAX / nmemb < size) {
    187 		errno = ENOMEM;
    188 		return NULL;
    189 	}
    190 	return realloc(optr, size * nmemb);
    191 }
    192 #endif /* !HAVE_REALLOCARRAY */
    193 #if !HAVE_STRTONUM
    194 /*
    195  * Copyright (c) 2004 Ted Unangst and Todd Miller
    196  * All rights reserved.
    197  *
    198  * Permission to use, copy, modify, and distribute this software for any
    199  * purpose with or without fee is hereby granted, provided that the above
    200  * copyright notice and this permission notice appear in all copies.
    201  *
    202  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    203  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    204  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    205  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    206  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    207  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    208  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    209  */
    210 
    211 #include <errno.h>
    212 #include <limits.h>
    213 #include <stdlib.h>
    214 
    215 #define	INVALID		1
    216 #define	TOOSMALL	2
    217 #define	TOOLARGE	3
    218 
    219 long long
    220 strtonum(const char *numstr, long long minval, long long maxval,
    221     const char **errstrp)
    222 {
    223 	long long ll = 0;
    224 	int error = 0;
    225 	char *ep;
    226 	struct errval {
    227 		const char *errstr;
    228 		int err;
    229 	} ev[4] = {
    230 		{ NULL,		0 },
    231 		{ "invalid",	EINVAL },
    232 		{ "too small",	ERANGE },
    233 		{ "too large",	ERANGE },
    234 	};
    235 
    236 	ev[0].err = errno;
    237 	errno = 0;
    238 	if (minval > maxval) {
    239 		error = INVALID;
    240 	} else {
    241 		ll = strtoll(numstr, &ep, 10);
    242 		if (numstr == ep || *ep != '\0')
    243 			error = INVALID;
    244 		else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
    245 			error = TOOSMALL;
    246 		else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
    247 			error = TOOLARGE;
    248 	}
    249 	if (errstrp != NULL)
    250 		*errstrp = ev[error].errstr;
    251 	errno = ev[error].err;
    252 	if (error)
    253 		ll = 0;
    254 
    255 	return (ll);
    256 }
    257 #endif /* !HAVE_STRTONUM */