xref: /freebsd/lib/libnetbsd/efun.c (revision df9489689d32c57c8fcd01e9586cb8abfb39ebbe)
1*df948968SEd Maste /*	$NetBSD: efun.c,v 1.10 2015/07/26 02:20:30 kamil Exp $	*/
2*df948968SEd Maste /*	$FreeBSD$ */
3*df948968SEd Maste 
4*df948968SEd Maste /*-
5*df948968SEd Maste  * Copyright (c) 2006 The NetBSD Foundation, Inc.
6*df948968SEd Maste  * All rights reserved.
7*df948968SEd Maste  *
8*df948968SEd Maste  * This code is derived from software contributed to The NetBSD Foundation
9*df948968SEd Maste  * by Christos Zoulas.
10*df948968SEd Maste  *
11*df948968SEd Maste  * Redistribution and use in source and binary forms, with or without
12*df948968SEd Maste  * modification, are permitted provided that the following conditions
13*df948968SEd Maste  * are met:
14*df948968SEd Maste  * 1. Redistributions of source code must retain the above copyright
15*df948968SEd Maste  *    notice, this list of conditions and the following disclaimer.
16*df948968SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
17*df948968SEd Maste  *    notice, this list of conditions and the following disclaimer in the
18*df948968SEd Maste  *    documentation and/or other materials provided with the distribution.
19*df948968SEd Maste  *
20*df948968SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21*df948968SEd Maste  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*df948968SEd Maste  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*df948968SEd Maste  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24*df948968SEd Maste  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*df948968SEd Maste  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*df948968SEd Maste  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*df948968SEd Maste  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*df948968SEd Maste  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*df948968SEd Maste  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*df948968SEd Maste  * POSSIBILITY OF SUCH DAMAGE.
31*df948968SEd Maste  */
32*df948968SEd Maste 
33*df948968SEd Maste #if HAVE_NBTOOL_CONFIG_H
34*df948968SEd Maste #include "nbtool_config.h"
35*df948968SEd Maste #endif
36*df948968SEd Maste 
37*df948968SEd Maste #include <sys/cdefs.h>
38*df948968SEd Maste #ifdef __RCSID
39*df948968SEd Maste __RCSID("$NetBSD: efun.c,v 1.10 2015/07/26 02:20:30 kamil Exp $");
40*df948968SEd Maste #endif
41*df948968SEd Maste 
42*df948968SEd Maste #include <err.h>
43*df948968SEd Maste #include <errno.h>
44*df948968SEd Maste #include <inttypes.h>
45*df948968SEd Maste #include <string.h>
46*df948968SEd Maste #include <stdlib.h>
47*df948968SEd Maste #include <stdio.h>
48*df948968SEd Maste #include <stdarg.h>
49*df948968SEd Maste #include <util.h>
50*df948968SEd Maste 
51*df948968SEd Maste static void (*efunc)(int, const char *, ...) = err;
52*df948968SEd Maste 
53*df948968SEd Maste void (*
54*df948968SEd Maste esetfunc(void (*ef)(int, const char *, ...)))(int, const char *, ...)
55*df948968SEd Maste {
56*df948968SEd Maste 	void (*of)(int, const char *, ...) = efunc;
57*df948968SEd Maste 	efunc = ef == NULL ? (void (*)(int, const char *, ...))exit : ef;
58*df948968SEd Maste 	return of;
59*df948968SEd Maste }
60*df948968SEd Maste 
61*df948968SEd Maste size_t
62*df948968SEd Maste estrlcpy(char *dst, const char *src, size_t len)
63*df948968SEd Maste {
64*df948968SEd Maste 	size_t rv;
65*df948968SEd Maste 	if ((rv = strlcpy(dst, src, len)) >= len) {
66*df948968SEd Maste 		errno = ENAMETOOLONG;
67*df948968SEd Maste 		(*efunc)(1,
68*df948968SEd Maste 		    "Cannot copy string; %zu chars needed %zu provided",
69*df948968SEd Maste 		    rv, len);
70*df948968SEd Maste 	}
71*df948968SEd Maste 	return rv;
72*df948968SEd Maste }
73*df948968SEd Maste 
74*df948968SEd Maste size_t
75*df948968SEd Maste estrlcat(char *dst, const char *src, size_t len)
76*df948968SEd Maste {
77*df948968SEd Maste 	size_t rv;
78*df948968SEd Maste 	if ((rv = strlcat(dst, src, len)) >= len) {
79*df948968SEd Maste 		errno = ENAMETOOLONG;
80*df948968SEd Maste 		(*efunc)(1,
81*df948968SEd Maste 		    "Cannot append to string; %zu chars needed %zu provided",
82*df948968SEd Maste 		    rv, len);
83*df948968SEd Maste 	}
84*df948968SEd Maste 	return rv;
85*df948968SEd Maste }
86*df948968SEd Maste 
87*df948968SEd Maste char *
88*df948968SEd Maste estrdup(const char *s)
89*df948968SEd Maste {
90*df948968SEd Maste 	char *d = strdup(s);
91*df948968SEd Maste 	if (d == NULL)
92*df948968SEd Maste 		(*efunc)(1, "Cannot copy string");
93*df948968SEd Maste 	return d;
94*df948968SEd Maste }
95*df948968SEd Maste 
96*df948968SEd Maste char *
97*df948968SEd Maste estrndup(const char *s, size_t len)
98*df948968SEd Maste {
99*df948968SEd Maste 	char *d = strndup(s, len);
100*df948968SEd Maste 	if (d == NULL)
101*df948968SEd Maste 		(*efunc)(1, "Cannot copy string");
102*df948968SEd Maste 	return d;
103*df948968SEd Maste }
104*df948968SEd Maste 
105*df948968SEd Maste void *
106*df948968SEd Maste emalloc(size_t n)
107*df948968SEd Maste {
108*df948968SEd Maste 	void *p = malloc(n);
109*df948968SEd Maste 	if (p == NULL && n != 0)
110*df948968SEd Maste 		(*efunc)(1, "Cannot allocate %zu bytes", n);
111*df948968SEd Maste 	return p;
112*df948968SEd Maste }
113*df948968SEd Maste 
114*df948968SEd Maste void *
115*df948968SEd Maste ecalloc(size_t n, size_t s)
116*df948968SEd Maste {
117*df948968SEd Maste 	void *p = calloc(n, s);
118*df948968SEd Maste 	if (p == NULL && n != 0 && s != 0)
119*df948968SEd Maste 		(*efunc)(1, "Cannot allocate %zu blocks of size %zu", n, s);
120*df948968SEd Maste 	return p;
121*df948968SEd Maste }
122*df948968SEd Maste 
123*df948968SEd Maste void *
124*df948968SEd Maste erealloc(void *p, size_t n)
125*df948968SEd Maste {
126*df948968SEd Maste 	void *q = realloc(p, n);
127*df948968SEd Maste 	if (q == NULL && n != 0)
128*df948968SEd Maste 		(*efunc)(1, "Cannot re-allocate %zu bytes", n);
129*df948968SEd Maste 	return q;
130*df948968SEd Maste }
131*df948968SEd Maste 
132*df948968SEd Maste FILE *
133*df948968SEd Maste efopen(const char *p, const char *m)
134*df948968SEd Maste {
135*df948968SEd Maste 	FILE *fp = fopen(p, m);
136*df948968SEd Maste 	if (fp == NULL)
137*df948968SEd Maste 		(*efunc)(1, "Cannot open `%s'", p);
138*df948968SEd Maste 	return fp;
139*df948968SEd Maste }
140*df948968SEd Maste 
141*df948968SEd Maste int
142*df948968SEd Maste easprintf(char ** __restrict ret, const char * __restrict format, ...)
143*df948968SEd Maste {
144*df948968SEd Maste 	int rv;
145*df948968SEd Maste 	va_list ap;
146*df948968SEd Maste 	va_start(ap, format);
147*df948968SEd Maste 	if ((rv = vasprintf(ret, format, ap)) == -1)
148*df948968SEd Maste 		(*efunc)(1, "Cannot format string");
149*df948968SEd Maste 	va_end(ap);
150*df948968SEd Maste 	return rv;
151*df948968SEd Maste }
152*df948968SEd Maste 
153*df948968SEd Maste int
154*df948968SEd Maste evasprintf(char ** __restrict ret, const char * __restrict format, va_list ap)
155*df948968SEd Maste {
156*df948968SEd Maste 	int rv;
157*df948968SEd Maste 	if ((rv = vasprintf(ret, format, ap)) == -1)
158*df948968SEd Maste 		(*efunc)(1, "Cannot format string");
159*df948968SEd Maste 	return rv;
160*df948968SEd Maste }
161