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