Initial import from FreeBSD RELENG_4:
[dragonfly.git] / libexec / rtld-aout / support.c
1 /*
2  * Generic "support" routines to replace those obtained from libiberty for ld.
3  *
4  * I've collected these from random bits of (published) code I've written
5  * over the years, not that they are a big deal.  peter@freebsd.org
6  *-
7  * Copyright (C) 1996
8  *      Peter Wemm.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *-
31  * $FreeBSD: src/libexec/rtld-aout/support.c,v 1.5 1999/08/28 00:10:06 peter Exp $
32  */
33 #include <sys/types.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <err.h>
37
38 #include "support.h"
39
40 char *
41 concat(s1, s2, s3)
42         const char *s1, *s2, *s3;
43 {
44         int len = 1;
45         char *s;
46         if (s1)
47                 len += strlen(s1);
48         if (s2)
49                 len += strlen(s2);
50         if (s3)
51                 len += strlen(s3);
52         s = xmalloc(len);
53         s[0] = '\0';
54         if (s1)
55                 strcat(s, s1);
56         if (s2)
57                 strcat(s, s2);
58         if (s3)
59                 strcat(s, s3);
60         return s;
61 }
62
63 void *
64 xmalloc(n)
65         size_t n;
66 {
67         char *p = malloc(n);
68
69         if (p == NULL)
70                 errx(1, "Could not allocate memory");
71
72         return p;
73 }
74
75 void *
76 xrealloc(p, n)
77         void *p;
78         size_t n;
79 {
80         p = realloc(p, n);
81
82         if (p == NULL)
83                 errx(1, "Could not allocate memory");
84
85         return p;
86 }