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