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