readclose.c (557B)
1 /* Public domain. */ 2 3 #include <unistd.h> 4 #include "error.h" 5 #include "readclose.h" 6 7 int readclose_append(int fd,stralloc *sa,unsigned int bufsize) 8 { 9 int r; 10 for (;;) { 11 if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; } 12 r = read(fd,sa->s + sa->len,bufsize); 13 if (r == -1) if (errno == error_intr) continue; 14 if (r <= 0) { close(fd); return r; } 15 sa->len += r; 16 } 17 } 18 19 int readclose(int fd,stralloc *sa,unsigned int bufsize) 20 { 21 if (!stralloc_copys(sa,"")) { close(fd); return -1; } 22 return readclose_append(fd,sa,bufsize); 23 }