-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.c
More file actions
57 lines (40 loc) · 707 Bytes
/
tools.c
File metadata and controls
57 lines (40 loc) · 707 Bytes
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
#include <string.h>
#include <stdlib.h>
#include "tools.h"
#include "debug.h"
void *xmalloc(size_t size)
{
void *p;
p = malloc(size);
if (p == NULL)
err_sys("malloc %zd bytes", size);
return p;
}
void *xrealloc(void *ptr, size_t size)
{
ptr = realloc(ptr, size);
if (size && ptr == NULL)
err_sys("realloc %zd bytes", size);
return ptr;
}
char *xstrdup(const char *s)
{
int len;
if (s == NULL)
return NULL;
len = strlen(s) + 1;
return memcpy(xmalloc(len), s, len);
}
char *xstrndup(const char *s, size_t n)
{
size_t len;
char *p;
if (s == NULL)
return NULL;
len = strlen(s);
if (n < len)
len = n;
p = memcpy(xmalloc(len + 1), s, len);
p[len] = '\0';
return p;
}