Blob
1 #include <u.h>2 #include <libc.h>3 #include <thread.h>4 #include <fcall.h>5 #include <fs.h>6 #include "plumb.h"8 Plumbmsg*9 threadplumbrecv(int fd)10 {11 char *buf;12 Plumbmsg *m;13 int n, more;15 buf = malloc(8192);16 if(buf == nil)17 return nil;18 n = threadread(fd, buf, 8192);19 m = nil;20 if(n > 0){21 m = plumbunpackpartial(buf, n, &more);22 if(m==nil && more>0){23 /* we now know how many more bytes to read for complete message */24 buf = realloc(buf, n+more);25 if(buf == nil)26 return nil;27 if(threadreadn(fd, buf+n, more) == more)28 m = plumbunpackpartial(buf, n+more, nil);29 }30 }31 free(buf);32 return m;33 }