-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathio.c
280 lines (219 loc) · 4.42 KB
/
io.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* io.c
* XBolo
*
* Created by Robert Chrzanowski.
* Copyright 2004 Robert Chrzanowski. All rights reserved.
*
*/
#include "io.h"
#include "errchk.h"
#include <stdlib.h>
#include <string.h>
ssize_t readblock(int d, void *buf, size_t nbytes) {
size_t n = 0;
TRY
while (n < nbytes) {
ssize_t r;
if ((r = read(d, buf + n, nbytes - n)) == -1) {
if (errno != EINTR) LOGFAIL(errno)
continue;
}
if (r == 0) { /* incomplete bytes */
FAIL(EPIPE)
}
n += r;
}
CLEANUP
ERRHANDLER(nbytes, -1)
END
}
ssize_t writeblock(int d, const void *buf, size_t nbytes) {
size_t n = 0;
TRY
while (n < nbytes) {
ssize_t r;
if ((r = write(d, buf + n, nbytes - n)) == -1) {
if (errno != ENOBUFS) LOGFAIL(errno)
usleep(100);
continue;
}
n += r;
}
CLEANUP
ERRHANDLER(nbytes, -1)
END
}
ssize_t writestr(int d, const char *str) {
size_t nbytes;
TRY
nbytes = strlen(str);
if (writeblock(d, str, nbytes) == -1) LOGFAIL(errno)
CLEANUP
ERRHANDLER(0, -1)
END
}
int sendblock(int s, const void *msg, size_t len) {
size_t b = 0;
TRY
while (b < len) {
ssize_t r;
if ((r = send(s, msg + b, len - b, 0)) == -1) {
if (errno != ENOBUFS) {
LOGFAIL(errno)
}
usleep(100);
continue;
}
b += r;
}
CLEANUP
ERRHANDLER(b, -1)
END
}
int sendstr(int s, const char *str) {
ssize_t bytes;
TRY
if ((bytes = sendblock(s, str, strlen(str) + 1)) == -1) LOGFAIL(errno)
CLEANUP
ERRHANDLER(bytes, -1)
END
}
int sendblockto(int s, const void *msg, size_t *len, int flags, const struct sockaddr *to, int tolen) {
size_t b = 0;
TRY
while (b < *len) {
ssize_t r;
if ((r = sendto(s, msg + b, (*len) - b, flags, to, tolen)) == -1) {
if (errno != ENOBUFS) LOGFAIL(errno)
usleep(100);
continue;
}
b += r;
}
CLEANUP
*len = b;
ERRHANDLER(0, -1)
END
}
ssize_t recvblock(int s, void *buf, size_t len) {
size_t n = 0;
TRY
while (n < len) {
ssize_t r;
if ((r = recv(s, buf + n, len - n, MSG_WAITALL)) == -1) {
if (errno != EINTR) {
LOGFAIL(errno)
}
else {
continue;
}
}
/* Connection closed. EPIPE is normally returned for when send()ing
on a closed pipe but it seems apropriate here since the socket
was closed before the expected number of bytes could be recevied. */
if (r == 0) FAIL(EPIPE)
n += r;
}
CLEANUP
ERRHANDLER(n, -1)
END
}
ssize_t recvblockpeek(int s, void *buf, size_t len) {
ssize_t r;
TRY
do {
if ((r = recv(s, buf, len, MSG_PEEK | MSG_WAITALL)) == -1) {
if (errno != EINTR) {
LOGFAIL(errno)
}
else {
continue;
}
}
/* Connection closed. EPIPE is normally returned for when send()ing
on a closed pipe but it seems apropriate here since the socket
was closed before the expected number of bytes could be recevied. */
if (r == 0) FAIL(EPIPE)
} while (r < len);
CLEANUP
ERRHANDLER(r, -1)
END
}
ssize_t recvstr(int s, char **str) {
char *buf = NULL;
size_t bufsize = 8;
size_t len = 0;
TRY
if ((buf = (char *)malloc(bufsize)) == NULL) LOGFAIL(errno)
do {
/* allocate more memory if needed */
if (len + 1 >= bufsize) {
char *newbuf;
bufsize *= 2;
if ((newbuf = realloc(buf, bufsize)) == NULL) LOGFAIL(errno)
buf = newbuf;
}
/* receive one byte at a time */
for (;;) {
if (recv(s, buf + len, 1, MSG_WAITALL) == -1) {
if (errno != EINTR) {
LOGFAIL(errno)
}
else {
continue;
}
}
else {
break;
}
}
len++;
} while (buf[len - 1] != '\0');
CLEANUP
switch (ERROR) {
case 0:
*str = buf;
RETURN(len)
default:
if (buf != NULL) {
free(buf);
}
*str = NULL;
RETERR(-1)
}
END
}
ssize_t recvblockfrom(int s, void *buf, size_t *len, int flags, struct sockaddr *from, socklen_t *fromlen) {
ssize_t n = 0;
TRY
while (n < *len) {
ssize_t r;
if ((r = recvfrom(s, buf + n, *len - n, flags, from, (socklen_t *)fromlen)) == -1) {
if (errno != EINTR) LOGFAIL(errno)
continue;
}
if (r == 0) {
break;
}
n += r;
}
CLEANUP
*len = n;
ERRHANDLER(0, -1)
END
}
int closesock(int *sock) {
assert(sock);
assert(*sock != -1);
TRY
while (close(*sock)) {
if (errno != EINTR) {
LOGFAIL(errno)
}
}
*sock = -1;
CLEANUP
ERRHANDLER(0, -1)
END
}