/* From Stevens */ int readn(int fd, void *buff, int nbytes) { int nleft, nread; char *ptr; ptr = (char *)buff; nleft = nbytes; while(nleft > 0) { if( (nread = read(fd, ptr, nleft)) < 0) { if( errno == EINTR) nread = 0; else if(errno == EAGAIN) break; else return(-1); } else if(nread == 0) break; nleft -= nread; ptr += nread; } return (nbytes-nleft); } int writen(int fd, const void *buff, int nbytes) { int nleft, nwritten; const char *ptr; ptr = (char*)buff; nleft = nbytes; while(nleft > 0) { if((nwritten = write(fd, ptr, nleft)) <= 0) { if(errno == EINTR) nwritten = 0; else return(-1); } nleft -= nwritten; ptr += nwritten; } return (nbytes); }