forked from thockin/mcedaemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
54 lines (50 loc) · 972 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define MAX_BUFLEN 1024
char *
read_line(int fd)
{
static char *buf;
int buflen = 64;
int i = 0;
int r;
int searching = 1;
while (searching) {
buf = realloc(buf, buflen);
if (!buf) {
fprintf(stderr, "ERR: realloc(%d): %s\n",
buflen, strerror(errno));
return NULL;
}
memset(buf+i, 0, buflen-i);
while (i < buflen) {
r = read(fd, buf+i, 1);
if (r < 0 && errno != EINTR) {
/* we should do something with the data */
fprintf(stderr, "ERR: read(): %s\n",
strerror(errno));
return NULL;
} else if (r == 0) {
/* signal this in an almost standard way */
errno = EPIPE;
return NULL;
} else if (r == 1) {
/* scan for a newline */
if (buf[i] == '\n') {
searching = 0;
buf[i] = '\0';
break;
}
i++;
}
}
if (buflen >= MAX_BUFLEN) {
break;
}
buflen *= 2;
}
return buf;
}