efun.c revision 1.1 1 /* $NetBSD: efun.c,v 1.1 2006/08/26 18:11:45 christos 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: efun.c,v 1.1 2006/08/26 18:11:45 christos Exp $");
41
42 #include <err.h>
43 #include <errno.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <stdarg.h>
48 #include <util.h>
49
50 static void (*efunc)(int, const char *, ...) = err;
51
52 void (*
53 esetfunc(void (*ef)(int, const char *, ...)))(int, const char *, ...)
54 {
55 void (*of)(int, const char *, ...) = efunc;
56 efunc = ef == NULL ? (void (*)(int, const char *, ...))exit : ef;
57 return of;
58 }
59
60 size_t
61 estrlcpy(char *dst, const char *src, size_t len)
62 {
63 size_t rv;
64 if ((rv = strlcpy(dst, src, len)) >= len) {
65 errno = ENAMETOOLONG;
66 (*efunc)(1,
67 "Cannot copy string; %zu chars needed %zu provided",
68 rv, len);
69 }
70 return rv;
71 }
72
73 size_t
74 estrlcat(char *dst, const char *src, size_t len)
75 {
76 size_t rv;
77 if ((rv = strlcat(dst, src, len)) >= len) {
78 errno = ENAMETOOLONG;
79 (*efunc)(1,
80 "Cannot append to string; %zu chars needed %zu provided",
81 rv, len);
82 }
83 return rv;
84 }
85
86 char *
87 estrdup(const char *s)
88 {
89 char *d = strdup(s);
90 if (d == NULL)
91 (*efunc)(1, "Cannot copy string");
92 return d;
93 }
94
95 void *
96 emalloc(size_t n)
97 {
98 void *p = malloc(n);
99 if (p == NULL)
100 (*efunc)(1, "Cannot allocate %zu bytes", n);
101 return p;
102 }
103
104 void *
105 ecalloc(size_t n, size_t s)
106 {
107 void *p = calloc(n, s);
108 if (p == NULL)
109 (*efunc)(1, "Cannot allocate %zu bytes", n);
110 return memset(p, 0, n);
111 }
112
113 void *
114 erealloc(void *p, size_t n)
115 {
116 void *q = realloc(p, n);
117 if (q == NULL)
118 (*efunc)(1, "Cannot re-allocate %zu bytes", n);
119 return q;
120 }
121
122 FILE *
123 efopen(const char *p, const char *m)
124 {
125 FILE *fp = fopen(p, m);
126 if (fp == NULL)
127 (*efunc)(1, "Cannot open `%s'", p);
128 return fp;
129 }
130
131 int
132 easprintf(char ** __restrict ret, const char * __restrict format, ...)
133 {
134 int rv;
135 va_list ap;
136 va_start(ap, format);
137 if ((rv = vasprintf(ret, format, ap)) == -1)
138 (*efunc)(1, "Cannot format string");
139 va_end(ap);
140 return rv;
141 }
142
143 int
144 evasprintf(char ** __restrict ret, const char * __restrict format, va_list ap)
145 {
146 int rv;
147 if ((rv = vasprintf(ret, format, ap)) == -1)
148 (*efunc)(1, "Cannot format string");
149 return rv;
150 }
151