-
Notifications
You must be signed in to change notification settings - Fork 9
/
u.c
125 lines (108 loc) · 1.75 KB
/
u.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "u.h"
void
panic(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
fflush(stderr);
va_end(ap);
exit(1);
}
void
say(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
printf("\n");
fflush(stdout);
va_end(ap);
}
void
Scp(char *dst, char *src, size_t n)
{
strncpy(dst, src, n);
dst[n-1] = '\0';
}
ssize_t
atomicio(ssize_t (f)(), int fd, void *_s, size_t n)
{
char *s = _s;
ssize_t res, pos = 0;
while(n>pos){
res = (f)(fd, s + pos, n - pos);
switch(res){
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
case 0:
if (pos != 0)
return pos;
return res;
default:
pos += res;
}
}
return pos;
}
void *
mal(size_t siz)
{
void *p;
p = malloc(siz);
if(p==nil)
panic("malloc");
return p;
}
void *
remal(void *p, size_t siz)
{
void *p1;
p1 = realloc(p, siz);
if(p==nil)
panic("realloc");
return p1;
}
char *
xfgetln(FILE *fp, size_t *len)
{
static char *buf = NULL;
static size_t bufsiz = 0;
char *ptr;
if (buf == NULL) {
bufsiz = BUFSIZ;
if ((buf = malloc(bufsiz)) == NULL)
return NULL;
}
if (fgets(buf, bufsiz, fp) == NULL)
return NULL;
*len = 0;
while ((ptr = strchr(&buf[*len], '\n')) == NULL) {
size_t nbufsiz = bufsiz + BUFSIZ;
char *nbuf = realloc(buf, nbufsiz);
if (nbuf == NULL) {
int oerrno = errno;
free(buf);
errno = oerrno;
buf = NULL;
return NULL;
} else
buf = nbuf;
*len = bufsiz;
if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL)
return buf;
bufsiz = nbufsiz;
}
*len = (ptr - buf) + 1;
return buf;
}