1*0a6a1f1dSLionel Sambuc /* $NetBSD: misc.c,v 1.15 2014/06/26 02:14:32 christos Exp $ */
2f789fee2SBen Gras
3f789fee2SBen Gras /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 1992 Diomidis Spinellis.
5f789fee2SBen Gras * Copyright (c) 1992, 1993
6f789fee2SBen Gras * The Regents of the University of California. All rights reserved.
7f789fee2SBen Gras *
8f789fee2SBen Gras * This code is derived from software contributed to Berkeley by
9f789fee2SBen Gras * Diomidis Spinellis of Imperial College, University of London.
10f789fee2SBen Gras *
11f789fee2SBen Gras * Redistribution and use in source and binary forms, with or without
12f789fee2SBen Gras * modification, are permitted provided that the following conditions
13f789fee2SBen Gras * are met:
14f789fee2SBen Gras * 1. Redistributions of source code must retain the above copyright
15f789fee2SBen Gras * notice, this list of conditions and the following disclaimer.
16f789fee2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
17f789fee2SBen Gras * notice, this list of conditions and the following disclaimer in the
18f789fee2SBen Gras * documentation and/or other materials provided with the distribution.
19f789fee2SBen Gras * 3. Neither the name of the University nor the names of its contributors
20f789fee2SBen Gras * may be used to endorse or promote products derived from this software
21f789fee2SBen Gras * without specific prior written permission.
22f789fee2SBen Gras *
23f789fee2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24f789fee2SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f789fee2SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f789fee2SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27f789fee2SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f789fee2SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f789fee2SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f789fee2SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f789fee2SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f789fee2SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f789fee2SBen Gras * SUCH DAMAGE.
34f789fee2SBen Gras */
35f789fee2SBen Gras
36f789fee2SBen Gras #if HAVE_NBTOOL_CONFIG_H
37f789fee2SBen Gras #include "nbtool_config.h"
38f789fee2SBen Gras #endif
39f789fee2SBen Gras
40f789fee2SBen Gras #include <sys/cdefs.h>
41*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: misc.c,v 1.15 2014/06/26 02:14:32 christos Exp $");
42*0a6a1f1dSLionel Sambuc #ifdef __FBSDID
43*0a6a1f1dSLionel Sambuc __FBSDID("$FreeBSD: head/usr.bin/sed/misc.c 200462 2009-12-13 03:14:06Z delphij $");
44f789fee2SBen Gras #endif
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc #if 0
47*0a6a1f1dSLionel Sambuc static const char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
48*0a6a1f1dSLionel Sambuc #endif
49f789fee2SBen Gras
50f789fee2SBen Gras #include <sys/types.h>
51f789fee2SBen Gras
52*0a6a1f1dSLionel Sambuc #include <err.h>
53*0a6a1f1dSLionel Sambuc #include <limits.h>
54f789fee2SBen Gras #include <regex.h>
55f789fee2SBen Gras #include <stdio.h>
56f789fee2SBen Gras #include <stdlib.h>
57f789fee2SBen Gras #include <string.h>
58f789fee2SBen Gras
59f789fee2SBen Gras #include "defs.h"
60f789fee2SBen Gras #include "extern.h"
61f789fee2SBen Gras
62f789fee2SBen Gras /*
63f789fee2SBen Gras * malloc with result test
64f789fee2SBen Gras */
65f789fee2SBen Gras void *
xmalloc(size_t size)66f789fee2SBen Gras xmalloc(size_t size)
67f789fee2SBen Gras {
68f789fee2SBen Gras void *p;
69f789fee2SBen Gras
70f789fee2SBen Gras if ((p = malloc(size)) == NULL)
71*0a6a1f1dSLionel Sambuc err(1, "malloc(%zu)", size);
72*0a6a1f1dSLionel Sambuc return p;
73f789fee2SBen Gras }
74f789fee2SBen Gras
75f789fee2SBen Gras /*
76f789fee2SBen Gras * realloc with result test
77f789fee2SBen Gras */
78f789fee2SBen Gras void *
xrealloc(void * p,size_t size)79f789fee2SBen Gras xrealloc(void *p, size_t size)
80f789fee2SBen Gras {
81f789fee2SBen Gras if (p == NULL) /* Compatibility hack. */
82f789fee2SBen Gras return (xmalloc(size));
83f789fee2SBen Gras
84f789fee2SBen Gras if ((p = realloc(p, size)) == NULL)
85*0a6a1f1dSLionel Sambuc err(1, "realloc(%zu)", size);
86*0a6a1f1dSLionel Sambuc return p;
87f789fee2SBen Gras }
88f789fee2SBen Gras
89f789fee2SBen Gras /*
90*0a6a1f1dSLionel Sambuc * realloc with result test
91*0a6a1f1dSLionel Sambuc */
92*0a6a1f1dSLionel Sambuc void *
xcalloc(size_t c,size_t n)93*0a6a1f1dSLionel Sambuc xcalloc(size_t c, size_t n)
94*0a6a1f1dSLionel Sambuc {
95*0a6a1f1dSLionel Sambuc void *p;
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc if ((p = calloc(c, n)) == NULL)
98*0a6a1f1dSLionel Sambuc err(1, "calloc(%zu, %zu)", c, n);
99*0a6a1f1dSLionel Sambuc return p;
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc /*
102*0a6a1f1dSLionel Sambuc * Return a string for a regular expression error passed. This is overkill,
103f789fee2SBen Gras * because of the silly semantics of regerror (we can never know the size of
104f789fee2SBen Gras * the buffer).
105f789fee2SBen Gras */
106f789fee2SBen Gras char *
strregerror(int errcode,regex_t * preg)107f789fee2SBen Gras strregerror(int errcode, regex_t *preg)
108f789fee2SBen Gras {
109f789fee2SBen Gras char buf[1];
110f789fee2SBen Gras static char *oe;
111f789fee2SBen Gras size_t s;
112f789fee2SBen Gras
113f789fee2SBen Gras if (oe != NULL)
114f789fee2SBen Gras free(oe);
115f789fee2SBen Gras s = regerror(errcode, preg, buf, 0);
116f789fee2SBen Gras oe = xmalloc(s);
117f789fee2SBen Gras (void)regerror(errcode, preg, oe, s);
118f789fee2SBen Gras return (oe);
119f789fee2SBen Gras }
120