previous | start | next

A readn wrapper function

The function below can will only return a short read when the fewer than n bytes remain.

This function is slightly shorter than the one in the csapp.c file from the text (and from the text web site) as it doesn't deal with signals.

    1   ssize_t rio_readn(int fd, void *usrbuf, size_t n)
    2   {
    3       size_t nleft = n;
    4       ssize_t nread;
    5       char *bufp = usrbuf;
    6   
    7       while (nleft > 0) {
    8           if ((nread = read(fd, bufp, nleft)) == 0) {
    9               break;              /* EOF */
   10           }
   11           nleft -= nread;
   12           bufp += nread;
   13       }
   14       return (n - nleft);         /* return >= 0 */
   15   }


previous | start | next