regress: Support NULL fdset arguments to select
[adns] / regress / hplayback.c
1 #include <assert.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include "harness.h"
13 static FILE *Tinputfile, *Treportfile;
14 static vbuf vb2;
15 extern void Tshutdown(void) {
16 adns__vbuf_free(&vb2);
17 }
18 static void Tensurereportfile(void) {
19 const char *fdstr;
20 int fd;
21 if (Treportfile) return;
22 Treportfile= stderr;
23 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
24 fd= atoi(fdstr);
25 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
26 }
27 static void Psyntax(const char *where) {
28 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
29 exit(-1);
30 }
31 static void Pcheckinput(void) {
32 if (ferror(Tinputfile)) Tfailed("read test log input file");
33 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
34 }
35 void Tensurerecordfile(void) {
36 const char *fdstr;
37 int fd;
38 int chars;
39 unsigned long sec, usec;
40 if (Tinputfile) return;
41 Tinputfile= stdin;
42 fdstr= getenv("ADNS_TEST_IN_FD");
43 if (fdstr) {
44 fd= atoi(fdstr);
45 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
46 }
47 setvbuf(Tinputfile,0,_IONBF,0);
48 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
49 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
50 chars= -1;
51 sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars);
52 if (chars==-1) Psyntax("start time invalid");
53 currenttime.tv_sec= sec;
54 currenttime.tv_usec= usec;
55 if (vb2.buf[chars] != '\n') Psyntax("not newline after start time");
56 }
57 static void Parg(const char *argname) {
58 int l;
59 if (vb2.buf[vb2.used++] != ' ') Psyntax("not a space before argument");
60 l= strlen(argname);
61 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
62 vb2.used+= l;
63 if (vb2.buf[vb2.used++] != '=') Psyntax("not = after argument name");
64 }
65 static int Pstring_maybe(const char *string) {
66 int l;
67 l= strlen(string);
68 if (memcmp(vb2.buf+vb2.used,string,l)) return 0;
69 vb2.used+= l;
70 return 1;
71 }
72 static void Pstring(const char *string, const char *emsg) {
73 if (Pstring_maybe(string)) return;
74 Psyntax(emsg);
75 }
76 static int Perrno(const char *stuff) {
77 const struct Terrno *te;
78 int r;
79 char *ep;
80 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
81 if (te->n) return te->v;
82 r= strtoul(stuff+2,&ep,10);
83 if (*ep) Psyntax("errno value not recognised, not numeric");
84 return r;
85 }
86 static void P_updatetime(void) {
87 int chars;
88 unsigned long sec, usec;
89 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
90 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
91 chars= -1;
92 sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars);
93 if (chars==-1) Psyntax("update time invalid");
94 currenttime.tv_sec+= sec;
95 currenttime.tv_usec+= usec;
96 if (currenttime.tv_usec > 1000000) {
97 currenttime.tv_sec++;
98 currenttime.tv_usec -= 1000000;
99 }
100 if (vb2.buf[chars] != '\n') Psyntax("not newline after update time");
101 }
102 static void Pfdset(fd_set *set, int max) {
103 int r, c;
104 char *ep;
105 if (!set) {
106 Pstring("null","null fdset pointer");
107 return;
108 }
109 if (vb2.buf[vb2.used++] != '[') Psyntax("fd set start not [");
110 FD_ZERO(set);
111 if (vb2.buf[vb2.used] == ']') { vb2.used++; return; }
112 for (;;) {
113 r= strtoul(vb2.buf+vb2.used,&ep,10);
114 if (r>=max) Psyntax("fd set member > max");
115 if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set");
116 FD_SET(r,set);
117 vb2.used= ep - (char*)vb2.buf;
118 c= vb2.buf[vb2.used++];
119 if (c == ']') break;
120 if (c != ',') Psyntax("fd set separator not ,");
121 }
122 }
123 #ifdef HAVE_POLL
124 static int Ppollfdevents(void) {
125 int events;
126 if (Pstring_maybe("0")) return 0;
127 events= 0;
128 if (Pstring_maybe("POLLIN")) {
129 events |= POLLIN;
130 if (!Pstring_maybe("|")) return events;
131 }
132 if (Pstring_maybe("POLLOUT")) {
133 events |= POLLOUT;
134 if (!Pstring_maybe("|")) return events;
135 }
136 Pstring("POLLPRI","pollfdevents PRI?");
137 return events;
138 }
139 static void Ppollfds(struct pollfd *fds, int nfds) {
140 int i;
141 char *ep;
142 const char *comma= "";
143 if (vb2.buf[vb2.used++] != '[') Psyntax("pollfds start not [");
144 for (i=0; i<nfds; i++) {
145 Pstring("{fd=","{fd= in pollfds");
146 fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
147 vb2.used= ep - (char*)vb2.buf;
148 Pstring(", events=",", events= in pollfds");
149 fds->events= Ppollfdevents();
150 Pstring(", revents=",", revents= in pollfds");
151 fds->revents= Ppollfdevents();
152 Pstring("}","} in pollfds");
153 Pstring(comma,"separator in pollfds");
154 comma= ", ";
155 }
156 if (vb2.buf[vb2.used++] != ']') Psyntax("pollfds end not ]");
157 }
158 #endif
159 static void Paddr(struct sockaddr *addr, int *lenr) {
160 adns_rr_addr a;
161 char *p, *q, *ep;
162 int err;
163 unsigned long ul;
164 p= vb2.buf+vb2.used;
165 if (*p!='[') {
166 q= strchr(p,':');
167 if (!q) Psyntax("missing :");
168 *q++= 0;
169 } else {
170 p++;
171 q= strchr(p,']');
172 if (!q) Psyntax("missing ]");
173 *q++= 0;
174 if (*q!=':') Psyntax("expected : after ]");
175 q++;
176 }
177 ul= strtoul(q,&ep,10);
178 if (*ep && *ep != ' ') Psyntax("invalid port (bad syntax)");
179 if (ul >= 65536) Psyntax("port too large");
180 a.len= sizeof(a.addr);
181 err= adns_text2addr(p, (int)ul, 0, &a.addr.sa,&a.len);
182 if (err) Psyntax("invalid address");
183 assert(*lenr >= a.len);
184 memcpy(addr, &a.addr, a.len);
185 *lenr= a.len;
186 vb2.used= ep - (char*)vb2.buf;
187 }
188 static int Pbytes(byte *buf, int maxlen) {
189 static const char hexdigits[]= "0123456789abcdef";
190 int c, v, done;
191 const char *pf;
192 done= 0;
193 for (;;) {
194 c= getc(Tinputfile); Pcheckinput();
195 if (c=='\n' || c==' ' || c=='\t') continue;
196 if (c=='.') break;
197 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
198 v= (pf-hexdigits)<<4;
199 c= getc(Tinputfile); Pcheckinput();
200 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
201 v |= (pf-hexdigits);
202 if (maxlen<=0) Psyntax("buffer overflow in bytes");
203 *buf++= v;
204 maxlen--; done++;
205 }
206 for (;;) {
207 c= getc(Tinputfile); Pcheckinput();
208 if (c=='\n') return done;
209 }
210 }
211 void Q_vb(void) {
212 const char *nl;
213 Tensurerecordfile();
214 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
215 fread(vb2.buf,1,vb.used+2,Tinputfile);
216 if (feof(Tinputfile)) {
217 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
218 vb.used,vb.buf);
219 exit(-1);
220 }
221 Pcheckinput();
222 if (vb2.buf[0] != ' ') Psyntax("not space before call");
223 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
224 vb2.buf[vb.used+1] != '\n') {
225 fprintf(stderr,
226 "adns test harness: program did unexpected:\n %.*s\n"
227 "was expecting:\n %.*s\n",
228 vb.used,vb.buf, vb.used,vb2.buf+1);
229 exit(1);
230 }
231 Tensurereportfile();
232 nl= memchr(vb.buf,'\n',vb.used);
233 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
234 }
235 int Hselect( int max , fd_set *rfds , fd_set *wfds , fd_set *efds , struct timeval *to ) {
236 int r, amtread;
237 char *ep;
238 Qselect( max , rfds , wfds , efds , to );
239 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
240 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
241 Tensurereportfile();
242 fprintf(Treportfile,"%s",vb2.buf);
243 amtread= strlen(vb2.buf);
244 if (amtread<=0 || vb2.buf[--amtread]!='\n')
245 Psyntax("badly formed line");
246 vb2.buf[amtread]= 0;
247 if (memcmp(vb2.buf," select=",8)) Psyntax("syscall reply mismatch");
248 if (vb2.buf[8] == 'E') {
249 int e;
250 e= Perrno(vb2.buf+8);
251 P_updatetime();
252 errno= e;
253 return -1;
254 }
255 r= strtoul(vb2.buf+8,&ep,10);
256 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
257 vb2.used= ep - (char*)vb2.buf;
258 Parg("rfds"); Pfdset(rfds,max);
259 Parg("wfds"); Pfdset(wfds,max);
260 Parg("efds"); Pfdset(efds,max);
261 assert(vb2.used <= amtread);
262 if (vb2.used != amtread) Psyntax("junk at end of line");
263 P_updatetime();
264 return r;
265 }
266 #ifdef HAVE_POLL
267 int Hpoll( struct pollfd *fds , int nfds , int timeout ) {
268 int r, amtread;
269 char *ep;
270 Qpoll( fds , nfds , timeout );
271 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
272 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
273 Tensurereportfile();
274 fprintf(Treportfile,"%s",vb2.buf);
275 amtread= strlen(vb2.buf);
276 if (amtread<=0 || vb2.buf[--amtread]!='\n')
277 Psyntax("badly formed line");
278 vb2.buf[amtread]= 0;
279 if (memcmp(vb2.buf," poll=",6)) Psyntax("syscall reply mismatch");
280 if (vb2.buf[6] == 'E') {
281 int e;
282 e= Perrno(vb2.buf+6);
283 P_updatetime();
284 errno= e;
285 return -1;
286 }
287 r= strtoul(vb2.buf+6,&ep,10);
288 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
289 vb2.used= ep - (char*)vb2.buf;
290 Parg("fds"); Ppollfds(fds,nfds);
291 assert(vb2.used <= amtread);
292 if (vb2.used != amtread) Psyntax("junk at end of line");
293 P_updatetime();
294 return r;
295 }
296 #endif
297 int Hsocket( int domain , int type , int protocol ) {
298 int r, amtread;
299 char *ep;
300 Tmust("socket","domain",domain==AF_INET || domain==AF_INET6);
301 Tmust("socket","type",type==SOCK_STREAM || type==SOCK_DGRAM);
302 Qsocket( domain , type );
303 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
304 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
305 Tensurereportfile();
306 fprintf(Treportfile,"%s",vb2.buf);
307 amtread= strlen(vb2.buf);
308 if (amtread<=0 || vb2.buf[--amtread]!='\n')
309 Psyntax("badly formed line");
310 vb2.buf[amtread]= 0;
311 if (memcmp(vb2.buf," socket=",8)) Psyntax("syscall reply mismatch");
312 if (vb2.buf[8] == 'E') {
313 int e;
314 e= Perrno(vb2.buf+8);
315 P_updatetime();
316 errno= e;
317 return -1;
318 }
319 r= strtoul(vb2.buf+8,&ep,10);
320 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
321 vb2.used= ep - (char*)vb2.buf;
322 assert(vb2.used <= amtread);
323 if (vb2.used != amtread) Psyntax("junk at end of line");
324 P_updatetime();
325 return r;
326 }
327 int Hfcntl( int fd , int cmd , ... ) {
328 int r, amtread;
329 va_list al; long arg;
330 Tmust("fcntl","cmd",cmd==F_SETFL || cmd==F_GETFL);
331 if (cmd == F_SETFL) {
332 va_start(al,cmd); arg= va_arg(al,long); va_end(al);
333 } else {
334 arg= 0;
335 }
336 Qfcntl( fd , cmd , arg );
337 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
338 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
339 Tensurereportfile();
340 fprintf(Treportfile,"%s",vb2.buf);
341 amtread= strlen(vb2.buf);
342 if (amtread<=0 || vb2.buf[--amtread]!='\n')
343 Psyntax("badly formed line");
344 vb2.buf[amtread]= 0;
345 if (memcmp(vb2.buf," fcntl=",7)) Psyntax("syscall reply mismatch");
346 if (vb2.buf[7] == 'E') {
347 int e;
348 e= Perrno(vb2.buf+7);
349 P_updatetime();
350 errno= e;
351 return -1;
352 }
353 r= 0;
354 if (cmd == F_GETFL) {
355 if (!memcmp(vb2.buf+7,"O_NONBLOCK|...",14)) {
356 r= O_NONBLOCK;
357 vb2.used= 7+14;
358 } else if (!memcmp(vb2.buf+7,"~O_NONBLOCK&...",15)) {
359 vb2.used= 7+15;
360 } else {
361 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
362 }
363 } else if (cmd == F_SETFL) {
364 if (memcmp(vb2.buf+7,"OK",2)) Psyntax("success/fail not E* or OK");
365 vb2.used= 7+2;
366 r= 0;
367 } else {
368 Psyntax("fcntl not F_GETFL or F_SETFL");
369 }
370 assert(vb2.used <= amtread);
371 if (vb2.used != amtread) Psyntax("junk at end of line");
372 P_updatetime();
373 return r;
374 }
375 int Hconnect( int fd , const struct sockaddr *addr , int addrlen ) {
376 int r, amtread;
377 Qconnect( fd , addr , addrlen );
378 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
379 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
380 Tensurereportfile();
381 fprintf(Treportfile,"%s",vb2.buf);
382 amtread= strlen(vb2.buf);
383 if (amtread<=0 || vb2.buf[--amtread]!='\n')
384 Psyntax("badly formed line");
385 vb2.buf[amtread]= 0;
386 if (memcmp(vb2.buf," connect=",9)) Psyntax("syscall reply mismatch");
387 if (vb2.buf[9] == 'E') {
388 int e;
389 e= Perrno(vb2.buf+9);
390 P_updatetime();
391 errno= e;
392 return -1;
393 }
394 if (memcmp(vb2.buf+9,"OK",2)) Psyntax("success/fail not E* or OK");
395 vb2.used= 9+2;
396 r= 0;
397 assert(vb2.used <= amtread);
398 if (vb2.used != amtread) Psyntax("junk at end of line");
399 P_updatetime();
400 return r;
401 }
402 int Hbind( int fd , const struct sockaddr *addr , int addrlen ) {
403 int r, amtread;
404 Qbind( fd , addr , addrlen );
405 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
406 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
407 Tensurereportfile();
408 fprintf(Treportfile,"%s",vb2.buf);
409 amtread= strlen(vb2.buf);
410 if (amtread<=0 || vb2.buf[--amtread]!='\n')
411 Psyntax("badly formed line");
412 vb2.buf[amtread]= 0;
413 if (memcmp(vb2.buf," bind=",6)) Psyntax("syscall reply mismatch");
414 if (vb2.buf[6] == 'E') {
415 int e;
416 e= Perrno(vb2.buf+6);
417 P_updatetime();
418 errno= e;
419 return -1;
420 }
421 if (memcmp(vb2.buf+6,"OK",2)) Psyntax("success/fail not E* or OK");
422 vb2.used= 6+2;
423 r= 0;
424 assert(vb2.used <= amtread);
425 if (vb2.used != amtread) Psyntax("junk at end of line");
426 P_updatetime();
427 return r;
428 }
429 int Hlisten( int fd , int backlog ) {
430 int r, amtread;
431 Qlisten( fd , backlog );
432 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
433 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
434 Tensurereportfile();
435 fprintf(Treportfile,"%s",vb2.buf);
436 amtread= strlen(vb2.buf);
437 if (amtread<=0 || vb2.buf[--amtread]!='\n')
438 Psyntax("badly formed line");
439 vb2.buf[amtread]= 0;
440 if (memcmp(vb2.buf," listen=",8)) Psyntax("syscall reply mismatch");
441 if (vb2.buf[8] == 'E') {
442 int e;
443 e= Perrno(vb2.buf+8);
444 P_updatetime();
445 errno= e;
446 return -1;
447 }
448 if (memcmp(vb2.buf+8,"OK",2)) Psyntax("success/fail not E* or OK");
449 vb2.used= 8+2;
450 r= 0;
451 assert(vb2.used <= amtread);
452 if (vb2.used != amtread) Psyntax("junk at end of line");
453 P_updatetime();
454 return r;
455 }
456 int Hclose( int fd ) {
457 int r, amtread;
458 Qclose( fd );
459 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
460 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
461 Tensurereportfile();
462 fprintf(Treportfile,"%s",vb2.buf);
463 amtread= strlen(vb2.buf);
464 if (amtread<=0 || vb2.buf[--amtread]!='\n')
465 Psyntax("badly formed line");
466 vb2.buf[amtread]= 0;
467 if (memcmp(vb2.buf," close=",7)) Psyntax("syscall reply mismatch");
468 if (vb2.buf[7] == 'E') {
469 int e;
470 e= Perrno(vb2.buf+7);
471 P_updatetime();
472 errno= e;
473 return -1;
474 }
475 if (memcmp(vb2.buf+7,"OK",2)) Psyntax("success/fail not E* or OK");
476 vb2.used= 7+2;
477 r= 0;
478 assert(vb2.used <= amtread);
479 if (vb2.used != amtread) Psyntax("junk at end of line");
480 P_updatetime();
481 return r;
482 }
483 int Hsendto( int fd , const void *msg , int msglen , unsigned int flags , const struct sockaddr *addr , int addrlen ) {
484 int r, amtread;
485 char *ep;
486 Tmust("sendto","flags",flags==0);
487 Qsendto( fd , msg , msglen , addr , addrlen );
488 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
489 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
490 Tensurereportfile();
491 fprintf(Treportfile,"%s",vb2.buf);
492 amtread= strlen(vb2.buf);
493 if (amtread<=0 || vb2.buf[--amtread]!='\n')
494 Psyntax("badly formed line");
495 vb2.buf[amtread]= 0;
496 if (memcmp(vb2.buf," sendto=",8)) Psyntax("syscall reply mismatch");
497 if (vb2.buf[8] == 'E') {
498 int e;
499 e= Perrno(vb2.buf+8);
500 P_updatetime();
501 errno= e;
502 return -1;
503 }
504 r= strtoul(vb2.buf+8,&ep,10);
505 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
506 vb2.used= ep - (char*)vb2.buf;
507 assert(vb2.used <= amtread);
508 if (vb2.used != amtread) Psyntax("junk at end of line");
509 P_updatetime();
510 return r;
511 }
512 int Hrecvfrom( int fd , void *buf , int buflen , unsigned int flags , struct sockaddr *addr , int *addrlen ) {
513 int r, amtread;
514 Tmust("recvfrom","flags",flags==0);
515 Tmust("recvfrom","*addrlen",*addrlen>=sizeof(struct sockaddr_in));
516 Qrecvfrom( fd , buflen , *addrlen );
517 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
518 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
519 Tensurereportfile();
520 fprintf(Treportfile,"%s",vb2.buf);
521 amtread= strlen(vb2.buf);
522 if (amtread<=0 || vb2.buf[--amtread]!='\n')
523 Psyntax("badly formed line");
524 vb2.buf[amtread]= 0;
525 if (memcmp(vb2.buf," recvfrom=",10)) Psyntax("syscall reply mismatch");
526 if (vb2.buf[10] == 'E') {
527 int e;
528 e= Perrno(vb2.buf+10);
529 P_updatetime();
530 errno= e;
531 return -1;
532 }
533 if (memcmp(vb2.buf+10,"OK",2)) Psyntax("success/fail not E* or OK");
534 vb2.used= 10+2;
535 r= 0;
536 Parg("addr"); Paddr(addr,addrlen);
537 assert(vb2.used <= amtread);
538 if (vb2.used != amtread) Psyntax("junk at end of line");
539 r= Pbytes(buf,buflen);
540 P_updatetime();
541 return r;
542 }
543 int Hread( int fd , void *buf , size_t buflen ) {
544 int r, amtread;
545 Qread( fd , buflen );
546 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
547 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
548 Tensurereportfile();
549 fprintf(Treportfile,"%s",vb2.buf);
550 amtread= strlen(vb2.buf);
551 if (amtread<=0 || vb2.buf[--amtread]!='\n')
552 Psyntax("badly formed line");
553 vb2.buf[amtread]= 0;
554 if (memcmp(vb2.buf," read=",6)) Psyntax("syscall reply mismatch");
555 if (vb2.buf[6] == 'E') {
556 int e;
557 e= Perrno(vb2.buf+6);
558 P_updatetime();
559 errno= e;
560 return -1;
561 }
562 if (memcmp(vb2.buf+6,"OK",2)) Psyntax("success/fail not E* or OK");
563 vb2.used= 6+2;
564 r= 0;
565 assert(vb2.used <= amtread);
566 if (vb2.used != amtread) Psyntax("junk at end of line");
567 r= Pbytes(buf,buflen);
568 P_updatetime();
569 return r;
570 }
571 int Hwrite( int fd , const void *buf , size_t len ) {
572 int r, amtread;
573 char *ep;
574 Qwrite( fd , buf , len );
575 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
576 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
577 Tensurereportfile();
578 fprintf(Treportfile,"%s",vb2.buf);
579 amtread= strlen(vb2.buf);
580 if (amtread<=0 || vb2.buf[--amtread]!='\n')
581 Psyntax("badly formed line");
582 vb2.buf[amtread]= 0;
583 if (memcmp(vb2.buf," write=",7)) Psyntax("syscall reply mismatch");
584 if (vb2.buf[7] == 'E') {
585 int e;
586 e= Perrno(vb2.buf+7);
587 P_updatetime();
588 errno= e;
589 return -1;
590 }
591 r= strtoul(vb2.buf+7,&ep,10);
592 if (*ep && *ep!=' ') Psyntax("return value not E* or positive number");
593 vb2.used= ep - (char*)vb2.buf;
594 assert(vb2.used <= amtread);
595 if (vb2.used != amtread) Psyntax("junk at end of line");
596 P_updatetime();
597 return r;
598 }