-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextern.c
60 lines (49 loc) · 1 KB
/
extern.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
#include "extern.h"
int linenum = 1; /* line being read */
char *filename = NULL; /* name of the file being read */
/* Safe malloc. */
void *
smalloc(size_t size)
{
void *vp;
if ((vp = malloc(size)) == NULL)
err_sys("Not enough memory");
return vp;
}
/* Safe calloc */
void *
scalloc(size_t num, size_t size)
{
void *vp;
if ((vp = calloc(num, size)) == NULL)
err_sys("Not enough memory");
return vp;
}
/* Safe realloc */
void *
srealloc(void *ptr, size_t size)
{
void *vp;
if ((vp = realloc(ptr, size)) == NULL)
err_sys("Not enough memory");
return vp;
}
/* Safe strdup */
char *
sstrdup(const char *s)
{
char *d;
d = smalloc(strlen(s)+1);
strcpy(d, s);
return d;
}
/* Safe strndup */
char *
sstrndup(const char *s, size_t n)
{
char *d;
d = smalloc(n+1);
strncpy(d, s, n);
*(d+n) = '\0';
return d;
}