Merge from vendor branch GDB:
[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  * $DragonFly: src/libexec/rtld-aout/Attic/support.c,v 1.2 2003/06/17 04:27:08 dillon Exp $
33  */
34 #include <sys/types.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <err.h>
38
39 #include "support.h"
40
41 char *
42 concat(s1, s2, s3)
43         const char *s1, *s2, *s3;
44 {
45         int len = 1;
46         char *s;
47         if (s1)
48                 len += strlen(s1);
49         if (s2)
50                 len += strlen(s2);
51         if (s3)
52                 len += strlen(s3);
53         s = xmalloc(len);
54         s[0] = '\0';
55         if (s1)
56                 strcat(s, s1);
57         if (s2)
58                 strcat(s, s2);
59         if (s3)
60                 strcat(s, s3);
61         return s;
62 }
63
64 void *
65 xmalloc(n)
66         size_t n;
67 {
68         char *p = malloc(n);
69
70         if (p == NULL)
71                 errx(1, "Could not allocate memory");
72
73         return p;
74 }
75
76 void *
77 xrealloc(p, n)
78         void *p;
79         size_t n;
80 {
81         p = realloc(p, n);
82
83         if (p == NULL)
84                 errx(1, "Could not allocate memory");
85
86         return p;
87 }