-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.c
67 lines (56 loc) · 1000 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
#include <stdio.h>
#include <stdlib.h>
#include "platform/platform.h"
#include "common.h"
#include "reallocarray.h"
static void
_debug(const char *pfx, const char *msg, va_list ap)
{
if (pfx)
fprintf(stderr, "%s", pfx);
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
}
void
die(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
_debug("[!] ", msg, ap);
va_end(ap);
exit(1);
}
void
debug(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
_debug("[+] ", msg, ap);
va_end(ap);
}
void
error(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
_debug("[-] ", msg, ap);
va_end(ap);
}
int
fd_array_push(struct fd_array *fda, int fd)
{
int *tmp;
if (fda->n == fda->allocated) {
fda->allocated = fda->allocated ? 2 * fda->allocated : 2;
tmp = xreallocarray(fda->fds, fda->allocated, sizeof *tmp);
if (tmp == NULL) {
free(fda->fds);
fda->fds = NULL;
fda->allocated = 0;
return -1;
}
fda->fds = tmp;
}
fda->fds[fda->n++] = fd;
return 0;
}