-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
58 lines (52 loc) · 856 Bytes
/
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
#include "util.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
FILE *efopen(const char *name, const char *mode)
{
FILE *f = fopen(name, mode);
if (f == NULL)
die("fopen: %s:", name);
return f;
}
pid_t efork(void)
{
pid_t pid = fork();
if (pid == -1)
die("fork:");
return pid;
}
void *emalloc(size_t sz)
{
void *p = malloc(sz);
if (p == NULL)
die("malloc:");
return p;
}
void veprintf(const char *fmt, va_list ap)
{
vfprintf(stderr, fmt, ap);
if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
}
void eprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
veprintf(fmt, ap);
va_end(ap);
}
void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
veprintf(fmt, ap);
va_end(ap);
exit(1);
}