int main(int argc, char *argv[]) { int fd, fd1; struct iovec iov[3]; struct stat myStruct; /* First buffer */ int x; /* Second buffer */ #define STR_SIZE 100 char str[STR_SIZE]; /* Third buffer */ ssize_t numRead, totRequired, numWrite; if (argc != 3 || strcmp(argv[1], "--help") == 0) usageErr("%s readFile writeFile\n", argv[0]); fd = open(argv[1], O_RDONLY); if (fd == -1) errExit("open"); totRequired = 0; iov[0].iov_base = &myStruct; iov[0].iov_len = sizeof(struct stat); totRequired += iov[0].iov_len; iov[1].iov_base = &x; iov[1].iov_len = sizeof(x); totRequired += iov[1].iov_len; iov[2].iov_base = str; iov[2].iov_len = STR_SIZE; totRequired += iov[2].iov_len; numRead = my_readv(fd, iov, 3); if (numRead == -1) errExit("readv"); if (numRead < totRequired) printf("Read fewer bytes than requested\n"); printf("total bytes requested: %ld; bytes read: %ld\n", (long) totRequired, (long) numRead); fd1 = creat(argv[2], S_IRUSR | S_IWUSR); if (fd1 == -1) errExit("open"); numWrite = my_writev(fd1, iov, 3); if (numWrite == -1) errExit("writev"); printf("total bytes requested: %ld; bytes write: %ld\n", (long) totRequired, (long) numWrite); exit(EXIT_SUCCESS); }
/* test my_readv, my_writev */ int main(int argc, char *argv[]) { int fd; struct iovec iov[3]; struct stat exStruct; int x; #define STR_SIZE 100 char str[STR_SIZE]; ssize_t numRead, totRequired; if (argc != 2 || strcmp(argv[1], "--help") == 0) usageErr("%s file\n", argv[0]); fd = open(argv[1], O_RDONLY); if (fd == -1) errExit("open"); totRequired = 0; iov[0].iov_base = &exStruct; iov[0].iov_len = sizeof(struct stat); totRequired += iov[0].iov_len; iov[1].iov_base = &x; iov[1].iov_len = sizeof(x); totRequired += iov[1].iov_len; iov[2].iov_base = &str; iov[2].iov_len = STR_SIZE; totRequired += iov[2].iov_len; numRead = my_readv(fd, iov, 3); if (numRead == -1) errExit("readv"); if (numRead < totRequired) errMsg("Read fewer bytes than required"); printf("total bytes requested: %ld; bytes read: %ld\n", (long) totRequired, (long) numRead); exit(EXIT_SUCCESS); }
int main(int argc, char * argv[]) { int fd; int rv1,rv2,wv1,wv2; struct iovec iov[3]; char buf[10]; // first buf int x; // second buf struct some_struct y; // third buf iov[0].iov_base = buf; iov[0].iov_len = 10; iov[1].iov_base = &x; iov[1].iov_len = sizeof(x); iov[2].iov_base = &y; iov[2].iov_len = sizeof(struct some_struct); if( argc < 2 ) return -1; fd = open(argv[1], O_RDWR); if( fd == -1 ) return -1; rv1 = readv(fd, iov, 3); print_contents(iov); lseek(fd, 0, SEEK_SET); rv2 = my_readv(fd, iov, 3); print_contents(iov); printf("readv: rv1=(%d) vs my_readv: rv2=(%d)\n STATUS: %s\n",rv1,rv2, rv1==rv2?"PASS":"******"); lseek(fd, 0, SEEK_END); wv1 = writev(fd, iov, 3); wv2 = my_writev(fd, iov, 3); printf("writev: wv1=(%d) vs my_writev: wv2=(%d)\n STATUS: %s\n", wv1, wv2, wv1==wv2?"PASS":"******"); close(fd); return 0; }