home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / ns2tab / part01 / readv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-04  |  1.1 KB  |  66 lines

  1. #include <sys/types.h>
  2. #include <sys/uio.h>
  3. #include <errno.h>
  4.  
  5. int readv(fd, iov, iovcnt)
  6.      int fd;
  7.      struct iovec *iov;
  8.      int iovcnt;
  9. {
  10.   int i,len, total;
  11.   char *base;
  12.  
  13.   errno = 0;
  14.   for (i=0,total=0; i<iovcnt; i++,iov++) {
  15.     len = iov->iov_len;
  16.     base = iov->iov_base;
  17.     while (len > 0) {
  18.       register int nbytes;
  19.       nbytes = read(fd, base, len);
  20.       if (nbytes < 0 && total == 0) {
  21.     return(-1);
  22.       }
  23.       if (nbytes <= 0) {
  24.     return(total);
  25.       }
  26.       errno = 0;
  27.       len   -= nbytes;
  28.       total += nbytes;
  29.       base  += nbytes;
  30.     }
  31.   }
  32.   return(total);
  33. }
  34.  
  35. int
  36. writev(fd,iov,iovcnt)
  37.      int fd;
  38.      struct iovec *iov;
  39.      int iovcnt;
  40. {
  41.   int i,len,total;
  42.   char *base;
  43.  
  44.   errno = 0;
  45.   for (i=0,total=0; i<iovcnt; i++, iov++) {
  46.     len = iov->iov_len;
  47.     base = iov->iov_base;
  48.     while (len > 0) {
  49.       register int nbytes;
  50.       nbytes = write(fd, base, len);
  51.       if (nbytes < 0 && total == 0) {
  52.     return(-1);
  53.       }
  54.       if (nbytes <= 0) {
  55.     return(total);
  56.       }
  57.  
  58.       errno = 0;
  59.       len   -= nbytes;
  60.       total += nbytes;
  61.       base  += nbytes;
  62.     }
  63.   }
  64.   return(total);
  65. }
  66.