preadv.c revision 1.2
1/* $OpenBSD: preadv.c,v 1.2 2003/07/31 21:48:09 deraadt Exp $ */ 2/* 3 * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain. 4 */ 5#include <sys/types.h> 6#include <sys/uio.h> 7#include <stdio.h> 8#include <stdlib.h> 9#include <unistd.h> 10#include <err.h> 11#include <fcntl.h> 12 13int 14main(int argc, char *argv[]) 15{ 16 char temp[] = "/tmp/dup2XXXXXXXXX"; 17 const char magic[10] = "0123456789"; 18 char c, c1, c2; 19 int fd; 20 struct iovec iv[2]; 21 22 if ((fd = mkstemp(temp)) < 0) 23 err(1, "mkstemp"); 24 remove(temp); 25 26 if (write(fd, magic, sizeof(magic)) != sizeof(magic)) 27 err(1, "write"); 28 29 if (lseek(fd, 0, SEEK_SET) != 0) 30 err(1, "lseek"); 31 32 if (read(fd, &c, 1) != 1) 33 err(1, "read1"); 34 35 if (c != magic[0]) 36 errx(1, "read1 %c != %c", c, magic[0]); 37 38 iv[0].iov_base = &c1; 39 iv[0].iov_len = 1; 40 iv[1].iov_base = &c2; 41 iv[1].iov_len = 1; 42 43 if (preadv(fd, iv, 2, 7) != 2) 44 err(1, "pread"); 45 46 if (c1 != magic[7]) 47 errx(1, "pread %c != %c", c1, magic[7]); 48 49 if (c2 != magic[8]) 50 errx(1, "pread %c != %c", c2, magic[7]); 51 52 if (read(fd, &c, 1) != 1) 53 err(1, "read2"); 54 55 if (c != magic[1]) 56 errx(1, "read2 %c != %c", c, magic[1]); 57 58 return 0; 59} 60