support.c revision 201217
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: head/libexec/rtld-aout/support.c 201217 2009-12-29 21:07:17Z ed $ 32 */ 33#include <sys/types.h> 34#include <string.h> 35#include <stdlib.h> 36#include <err.h> 37 38#include "support.h" 39 40char * 41concat(const char *s1, const char *s2, const char *s3) 42{ 43 int len = 1; 44 char *s; 45 if (s1) 46 len += strlen(s1); 47 if (s2) 48 len += strlen(s2); 49 if (s3) 50 len += strlen(s3); 51 s = xmalloc(len); 52 s[0] = '\0'; 53 if (s1) 54 strcat(s, s1); 55 if (s2) 56 strcat(s, s2); 57 if (s3) 58 strcat(s, s3); 59 return s; 60} 61 62void * 63xmalloc(size_t n) 64{ 65 char *p = malloc(n); 66 67 if (p == NULL) 68 errx(1, "Could not allocate memory"); 69 70 return p; 71} 72 73void * 74xrealloc(void *p, size_t n) 75{ 76 p = realloc(p, n); 77 78 if (p == NULL) 79 errx(1, "Could not allocate memory"); 80 81 return p; 82} 83