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