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