summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/ld/etc.c
blob: d3716f03eced3e61bd694bcca5ba5a65ff9716d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* * $OpenBSD: etc.c,v 1.2 1998/03/26 19:46:18 niklas Exp $*/
/*
 */

#include <err.h>
#include <stdlib.h>
#include <string.h>

/*
 * Like malloc but get fatal error if memory is exhausted.
 */
void *
xmalloc(size)
	size_t size;
{
	register void	*result = (void *)malloc(size);

	if (!result)
		errx(1, "virtual memory exhausted");

	return result;
}

/*
 * Like realloc but get fatal error if memory is exhausted.
 */
void *
xrealloc(ptr, size)
	void *ptr;
	size_t size;
{
	register void	*result;

	if (ptr == NULL)
		result = (void *)malloc(size);
	else
		result = (void *)realloc(ptr, size);

	if (!result)
		errx(1, "virtual memory exhausted");

	return result;
}

/*
 * Return a newly-allocated string whose contents concatenate
 * the strings S1, S2, S3.
 */
char *
concat(s1, s2, s3)
	const char *s1, *s2, *s3;
{
	register int	len1 = strlen(s1),
			len2 = strlen(s2),
			len3 = strlen(s3);

	register char *result = (char *)xmalloc(len1 + len2 + len3 + 1);

	strcpy(result, s1);
	strcpy(result + len1, s2);
	strcpy(result + len1 + len2, s3);
	result[len1 + len2 + len3] = 0;

	return result;
}