1 /* $NetBSD: efun.c,v 1.5 2007/07/01 21:41:16 pooka 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 #if HAVE_NBTOOL_CONFIG_H 40 #include "nbtool_config.h" 41 #endif 42 43 #include <sys/cdefs.h> 44 #ifdef __RCSID 45 __RCSID("$NetBSD: efun.c,v 1.5 2007/07/01 21:41:16 pooka Exp $"); 46 #endif 47 48 #include <err.h> 49 #include <errno.h> 50 #include <string.h> 51 #include <stdlib.h> 52 #include <stdio.h> 53 #include <stdarg.h> 54 #include <util.h> 55 56 static void (*efunc)(int, const char *, ...) = err; 57 58 void (* 59 esetfunc(void (*ef)(int, const char *, ...)))(int, const char *, ...) 60 { 61 void (*of)(int, const char *, ...) = efunc; 62 efunc = ef == NULL ? (void (*)(int, const char *, ...))exit : ef; 63 return of; 64 } 65 66 size_t 67 estrlcpy(char *dst, const char *src, size_t len) 68 { 69 size_t rv; 70 if ((rv = strlcpy(dst, src, len)) >= len) { 71 errno = ENAMETOOLONG; 72 (*efunc)(1, 73 "Cannot copy string; %zu chars needed %zu provided", 74 rv, len); 75 } 76 return rv; 77 } 78 79 size_t 80 estrlcat(char *dst, const char *src, size_t len) 81 { 82 size_t rv; 83 if ((rv = strlcat(dst, src, len)) >= len) { 84 errno = ENAMETOOLONG; 85 (*efunc)(1, 86 "Cannot append to string; %zu chars needed %zu provided", 87 rv, len); 88 } 89 return rv; 90 } 91 92 char * 93 estrdup(const char *s) 94 { 95 char *d = strdup(s); 96 if (d == NULL) 97 (*efunc)(1, "Cannot copy string"); 98 return d; 99 } 100 101 char * 102 estrndup(const char *s, size_t len) 103 { 104 char *d = strndup(s, len); 105 if (d == NULL) 106 (*efunc)(1, "Cannot copy string"); 107 return d; 108 } 109 110 void * 111 emalloc(size_t n) 112 { 113 void *p = malloc(n); 114 if (p == NULL) 115 (*efunc)(1, "Cannot allocate %zu bytes", n); 116 return p; 117 } 118 119 void * 120 ecalloc(size_t n, size_t s) 121 { 122 void *p = calloc(n, s); 123 if (p == NULL) 124 (*efunc)(1, "Cannot allocate %zu bytes", n); 125 return p; 126 } 127 128 void * 129 erealloc(void *p, size_t n) 130 { 131 void *q = realloc(p, n); 132 if (q == NULL) 133 (*efunc)(1, "Cannot re-allocate %zu bytes", n); 134 return q; 135 } 136 137 FILE * 138 efopen(const char *p, const char *m) 139 { 140 FILE *fp = fopen(p, m); 141 if (fp == NULL) 142 (*efunc)(1, "Cannot open `%s'", p); 143 return fp; 144 } 145 146 int 147 easprintf(char ** __restrict ret, const char * __restrict format, ...) 148 { 149 int rv; 150 va_list ap; 151 va_start(ap, format); 152 if ((rv = vasprintf(ret, format, ap)) == -1) 153 (*efunc)(1, "Cannot format string"); 154 va_end(ap); 155 return rv; 156 } 157 158 int 159 evasprintf(char ** __restrict ret, const char * __restrict format, va_list ap) 160 { 161 int rv; 162 if ((rv = vasprintf(ret, format, ap)) == -1) 163 (*efunc)(1, "Cannot format string"); 164 return rv; 165 } 166