-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
69 lines (54 loc) · 989 Bytes
/
common.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
#include "common.h"
#include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include <stdlib.h>
pid_t safe_fork(void)
{
pid_t pid = fork();
assert(pid != -1);
return pid;
}
int safe_creat(const char *pathname, mode_t mode)
{
int fd = creat(pathname, mode);
assert(fd != -1);
return fd;
}
int safe_open(const char *pathname, int flags)
{
int fd = open(pathname, flags);
assert(fd != -1);
return fd;
}
void safe_read(int fd, void *buf, size_t count)
{
assert(read(fd, buf, count) == count);
}
void *safe_calloc(size_t size, size_t cnt)
{
void *buf = calloc(size, cnt);
assert(buf != NULL);
return buf;
}
void *safe_malloc(size_t size)
{
void *buf = malloc(size);
assert(buf != NULL);
return buf;
}
void safe_free(void *buf)
{
assert(buf != NULL);
free(buf);
}
void safe_write(int fd, const void *buf, size_t count)
{
assert(write(fd, buf, count) == count);
}
void safe_close(int fd)
{
assert(close(fd) == 0);
}