Advertisement
rfmonk

t_readv.c

Dec 28th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <sys/stat.h>
  2. #include <sys/uio.h>
  3. #include <fcntl.h>
  4. #include "tlpi_hdr.h"
  5.  
  6. int
  7. main(int argc, char *argv[])
  8. {
  9.     int fd;
  10.     struct iovec iov[3];
  11.     struct stat myStruct;   // First buffer
  12.     int x;                  // Second buffer
  13. #define STR_SIZE 100
  14.     char str[STR_SIZE];     // Third buffer
  15.     ssize_t numRead, totRequired;
  16.  
  17.     if (argc != 2 || strcmp(argv[1], "--help") == 0)
  18.         usageErr("%s file\n", argv[0]);
  19.  
  20.     fd = open(argv[1], O_RDONLY);
  21.     if (fd == -1)
  22.         errExit("open");
  23.  
  24.     totRequired = 0;
  25.  
  26.     iov[0].iov_base = &myStruct;
  27.     iov[0].iov_len = sizeof(struct stat);
  28.     totRequired += iov[0].iov_len;
  29.  
  30.     iov[1].iov_base = &x;
  31.     iov[1].iov_len = sizeof(x);
  32.     totRequired += iov[1].iov_len;
  33.  
  34.     iov[2].iov_base = str;
  35.     iov[2].iov_len = STR_SIZE;
  36.     totRequired += iov[2].iov_len;
  37.  
  38.     numRead = readv(fd, iov, 3);
  39.     if (numRead == -1)
  40.         errExit("readv");
  41.  
  42.     if (numRead < totRequired)
  43.         printf("Read fewer bytes than requested\n");
  44.  
  45.     printf("total bytes requested: %ld; bytes read: %ld\n",
  46.             (long) totRequired, (long) numRead);
  47.     exit(EXIT_SUCCESS);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement