-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.c
171 lines (141 loc) · 2.38 KB
/
util.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
int
max(int x, int y)
{
return x > y ? x : y;
}
int
min(int x, int y)
{
return x < y ? x : y;
}
int
diff(int x, int y)
{
return x > y ? x - y : y - x;
}
void *
emalloc(size_t size)
{
void *p;
if ((p = malloc(size)) == NULL)
err(EXIT_FAILURE, "malloc");
return p;
}
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
if ((p = calloc(nmemb, size)) == NULL)
err(EXIT_FAILURE, "calloc");
return p;
}
char *
estrdup(const char *s)
{
char *t;
if ((t = strdup(s)) == NULL)
err(EXIT_FAILURE, "strdup");
return t;
}
void *
erealloc(void *ptr, size_t size)
{
void *p;
if ((p = realloc(ptr, size)) == NULL)
err(EXIT_FAILURE, "realloc");
return p;
}
void
egetcwd(char *path, size_t size)
{
if (getcwd(path, size) == NULL) {
err(EXIT_FAILURE, "getcwd");
}
}
int
escandir(const char *dirname, struct dirent ***namelist, int (*select)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **))
{
int ret;
if ((ret = scandir(dirname, namelist, select, compar)) == -1)
err(EXIT_FAILURE, "scandir");
return ret;
}
pid_t
efork(void)
{
pid_t pid;
if ((pid = fork()) < 0)
err(EXIT_FAILURE, "fork");
return pid;
}
void
eexec(char *const argv[])
{
execvp(argv[0], argv);
err(EXIT_FAILURE, "%s", argv[0]);
}
void
etcreate(pthread_t *tid, void *(*thrfn)(void *), void *arg)
{
int errn;
if ((errn = pthread_create(tid, NULL, thrfn, arg)) != 0) {
errno = errn;
err(EXIT_FAILURE, "could not create thread");
}
}
void
etjoin(pthread_t tid, void **rval)
{
int errn;
if ((errn = pthread_join(tid, rval)) != 0) {
errno = errn;
err(EXIT_FAILURE, "could not join with thread");
}
}
void
etlock(pthread_mutex_t *mutex)
{
int errn;
if ((errn = pthread_mutex_lock(mutex)) != 0) {
errno = errn;
err(EXIT_FAILURE, "could not lock mutex");
}
}
void
etunlock(pthread_mutex_t *mutex)
{
int errn;
if ((errn = pthread_mutex_unlock(mutex)) != 0) {
errno = errn;
err(EXIT_FAILURE, "could not unlock mutex");
}
}
int
ewaitpid(pid_t pid)
{
int status;
for (;;) {
if (waitpid(pid, &status, 0) == -1) {
if (errno != EINTR) {
err(EXIT_FAILURE, "waitpid");
}
} else if (!WIFSTOPPED(status)) {
return status;
}
}
}
void
eclose(int fd)
{
while (close(fd) == -1) {
if (errno != EINTR) {
err(EXIT_FAILURE, "close");
}
}
}