-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
xalloc.c
70 lines (52 loc) · 870 Bytes
/
xalloc.c
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
67
68
69
/*
* Copyright 2014-2017 Katherine Flavel
*
* See LICENCE for the full copyright terms.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "xalloc.h"
#include "txt.h"
void *
xmalloc(size_t size)
{
void *new;
new = malloc(size);
if (new == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
return new;
}
char *
xstrdup(const char *s)
{
char *new;
new = xmalloc(strlen(s) + 1);
return strcpy(new, s);
}
struct txt
xtxtdup(const struct txt *t)
{
struct txt new;
assert(t != NULL);
assert(t->p != NULL);
new.n = t->n;
new.p = xmalloc(new.n);
memcpy((void *) new.p, t->p, new.n);
return new;
}
void
xerror(const char *msg, ...)
{
va_list ap;
fprintf(stderr, "kgt: ");
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}