1#include <stdio.h> 2#include <sys/types.h> 3#include <sys/uio.h> 4#include <fcntl.h> 5#include <errno.h> 6#include <unistd.h> 7#include <string.h> 8 9#define K_1 8192 10#define NBUFS 2 11#define CHUNK K_1 /* single chunk */ 12#define MAX_IOVEC 2 13#define DATA_FILE "writev_data_file" 14 15static char buf1[K_1]; 16static char buf2[K_1]; 17static char *buf_list[NBUFS], f_name[]="writev_data_file"; 18static int fd; 19 20struct iovec wr_iovec[MAX_IOVEC] = { 21 {(caddr_t)-1, CHUNK}, 22 {(caddr_t)NULL, 0} 23}; 24 25int main(void) 26{ 27 int nbytes; 28 29 /* Fill the buf_list[0] and buf_list[1] with 0 zeros */ 30 buf_list[0] = buf1; 31 buf_list[1] = buf2; 32 memset(buf_list[0], 0, K_1); 33 memset(buf_list[1], 0, K_1); 34 35 if ((fd = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) { 36 fprintf(stderr, "open(2) failed: fname = %s, errno = %d\n", 37 f_name, errno); 38 return 1; 39 } else if ((nbytes = write(fd, buf_list[1], K_1)) != K_1) { 40 fprintf(stderr, "write(2) failed: nbytes = %d, errno = %d\n", 41 nbytes, errno); 42 return 1; 43 } 44 if (close(fd) < 0) { 45 fprintf(stderr, "close failed: errno = %d\n", errno); 46 return 1; 47 } 48 fprintf(stderr, "Test file created.\n"); 49 if ((fd = open(f_name, O_RDWR, 0666)) < 0) { 50 fprintf(stderr, "open failed: fname = %s, errno = %d\n", 51 f_name, errno); 52 return 1; 53 } 54 55 lseek(fd, 0, 0); 56 if (writev(fd, wr_iovec, 2) < 0) { 57 if (errno == EFAULT) 58 fprintf(stderr, "Received EFAULT as expected\n"); 59 else 60 fprintf(stderr, "Expected EFAULT, got %d\n", errno); 61 lseek(fd, K_1, 0); 62 if ((nbytes = read(fd, buf_list[0], CHUNK)) != 0) 63 fprintf(stderr, "Expected nbytes = 0, got %d\n", nbytes); 64 } 65 else 66 fprintf(stderr, "Error writev returned a positive value\n"); 67 // Now check invalid vector count 68 if (writev(fd, wr_iovec, -1) < 0) { 69 if (errno == EINVAL) 70 fprintf(stderr, "Received EINVAL as expected\n"); 71 else 72 fprintf(stderr, "expected errno = EINVAL, got %d\n", errno); 73 } 74 else 75 fprintf(stderr, "Error writev returned a positive value\n"); 76 if (readv(fd, wr_iovec, -1) < 0) { 77 if (errno == EINVAL) 78 fprintf(stderr, "Received EINVAL as expected\n"); 79 else 80 fprintf(stderr, "expected errno = EINVAL, got %d\n", errno); 81 } 82 else 83 fprintf(stderr, "Error writev returned a positive value\n"); 84 85 unlink(f_name); 86 87 return 0; 88} 89 90