Import pkg_install-20090528:
[netbsd-mini2440.git] / external / bsd / pkg_install / dist / lib / xwrapper.c
blob75f5fa3108f11c03de420663298fb2f3c71d2285
1 /* $NetBSD: xwrapper.c,v 1.2 2009/02/02 12:35:01 joerg Exp $ */
3 /*-
4 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
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.
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.2 2009/02/02 12:35:01 joerg Exp $");
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>
49 #include "lib.h"
51 char *
52 xasprintf(const char *fmt, ...)
54 va_list ap;
55 char *buf;
57 va_start(ap, fmt);
58 if (vasprintf(&buf, fmt, ap) == -1)
59 err(1, "asprintf failed");
60 va_end(ap);
61 return buf;
64 void *
65 xmalloc(size_t len)
67 void *ptr;
69 if ((ptr = malloc(len)) == NULL)
70 err(1, "malloc failed");
71 return ptr;
74 void *
75 xcalloc(size_t len, size_t n)
77 void *ptr;
79 if ((ptr = calloc(len, n)) == NULL)
80 err(1, "calloc failed");
81 return ptr;
84 void *
85 xrealloc(void *buf, size_t len)
87 void *ptr;
89 if ((ptr = realloc(buf, len)) == NULL)
90 err(1, "realloc failed");
91 return ptr;
94 char *
95 xstrdup(const char *str)
97 char *buf;
99 if ((buf = strdup(str)) == NULL)
100 err(1, "strdup failed");
101 return buf;