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
|
/* $OpenBSD: etc.c,v 1.5 2003/07/06 20:04:00 deraadt Exp $ */
/* Public Domain */
#include <sys/types.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include "ld.h"
#define OOM_MSG "Out of memory"
char *
xstrdup(const char *s)
{
char *ptr;
if ((ptr = strdup(s)) == NULL)
err(1, OOM_MSG);
return (ptr);
}
void *
xmalloc(size_t size)
{
void *ptr;
if ((ptr = malloc(size)) == NULL)
err(1, OOM_MSG);
return (ptr);
}
void *
xrealloc(void *ptr, size_t size)
{
void *nptr;
if ((nptr = realloc(ptr, size)) == NULL)
err(1, OOM_MSG);
return (nptr);
}
char *
concat(const char *s1, const char *s2, const char *s3)
{
char *str;
size_t len;
len = strlen(s1) + strlen(s2) + strlen(s3) + 1;
str = xmalloc(len);
strlcpy(str, s1, len);
strlcat(str, s2, len);
strlcat(str, s3, len);
return (str);
}
|