-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
83 lines (73 loc) · 1.99 KB
/
util.h
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
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static inline size_t get(int fd, char *data, size_t size) {
size_t length = 0;
ssize_t count;
while (size > 0 && (count = read(fd, data, size))) {
if (count < 0 && errno != EINTR && errno != EAGAIN)
err(1, "read");
if (count > 0)
data += count, size -= count, length += count;
}
return length;
}
static inline size_t getmsg(int fd, char *data, size_t size, int mode) {
size_t length = get(fd, data, size);
char *separator;
switch (mode) {
case 1: /* body only */
while (length > 1) {
if ((separator = memmem(data, length, "\n\n", 2))) {
length = data + length - separator - 2;
memmove(data, separator + 2, length);
} else if (data[length - 1] == '\n') {
data[0] = '\n';
length = 1;
} else {
length = 0;
}
length += get(fd, data + length, size - length);
if (separator)
return length;
}
return 0;
case 2: /* header only */
separator = memmem(data, length, "\n\n", 2);
if (separator)
length = 1 + separator - data;
}
return length;
}
static inline void put(int fd, const char *data, size_t length) {
ssize_t count;
while (length > 0) {
count = write(fd, data, length);
if (count < 0 && errno != EINTR && errno != EAGAIN)
err(1, "write");
if (count > 0)
data += count, length -= count;
}
}
static inline ssize_t readpath(char **out, int delim) {
static size_t size;
static char *line;
ssize_t length;
if (line == NULL) {
size = PATH_MAX; /* aim never to reallocate */
line = malloc(size);
}
if (line == NULL)
err(1, "malloc");
length = getdelim(&line, &size, delim, stdin);
if (length < 0 && errno == ENOMEM)
err(1, "realloc");
while (length > 0 && line[length - 1] == delim)
line[--length] = '\0';
*out = line;
return length;
}