1 /* $NetBSD: xwrapper.c,v 1.3 2021/04/10 19:49:59 nia Exp $ */
2
3 /*-
4 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #if HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 #include <nbcompat.h>
36 #if HAVE_SYS_CDEFS_H
37 #include <sys/cdefs.h>
38 #endif
39 __RCSID("$NetBSD: xwrapper.c,v 1.3 2021/04/10 19:49:59 nia Exp $");
40
41 #if HAVE_ERR_H
42 #include <err.h>
43 #endif
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "lib.h"
50
51 char *
xasprintf(const char * fmt,...)52 xasprintf(const char *fmt, ...)
53 {
54 va_list ap;
55 char *buf;
56
57 va_start(ap, fmt);
58 if (vasprintf(&buf, fmt, ap) == -1)
59 err(1, "asprintf failed");
60 va_end(ap);
61 return buf;
62 }
63
64 void *
xmalloc(size_t len)65 xmalloc(size_t len)
66 {
67 void *ptr;
68
69 if ((ptr = malloc(len)) == NULL)
70 err(1, "malloc failed");
71 return ptr;
72 }
73
74 void *
xcalloc(size_t len,size_t n)75 xcalloc(size_t len, size_t n)
76 {
77 void *ptr;
78
79 if ((ptr = calloc(len, n)) == NULL)
80 err(1, "calloc failed");
81 return ptr;
82 }
83
84 void *
xrealloc(void * buf,size_t len)85 xrealloc(void *buf, size_t len)
86 {
87 void *ptr;
88
89 if ((ptr = realloc(buf, len)) == NULL)
90 err(1, "realloc failed");
91 return ptr;
92 }
93
94 char *
xstrdup(const char * str)95 xstrdup(const char *str)
96 {
97 char *buf;
98
99 if ((buf = strdup(str)) == NULL)
100 err(1, "strdup failed");
101 return buf;
102 }
103