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