-
Notifications
You must be signed in to change notification settings - Fork 2
/
compat.c
69 lines (57 loc) · 1005 Bytes
/
compat.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "compat.h"
char *argv0;
#ifdef COMPAT_ERR
void
errx(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s ", argv0);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
va_end(ap);
exit(eval);
}
void
warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s ", argv0);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
va_end(ap);
}
#endif
#ifdef COMPAT_GETLINE
ssize_t
getline(char **linep, size_t *linecapp, FILE *stream)
{
size_t len = 0;
int ch;
fflush(stdout);
if (!*linep) {
*linecapp = 128;
if (!(*linep = malloc(*linecapp)))
return -1;
}
while (1) {
if (len+2 >= *linecapp) {
while (len+2 >= (*linecapp *= 2))
;
if (!(*linep = realloc(*linep, *linecapp)))
return -1;
}
ch = fgetc(stream);
if (ch != -1)
(*linep)[len++] = (int)ch;
if (ch == -1 || ch == '\n') {
(*linep)[len] = '\0';
return len;
}
}
}
#endif