preadv.c revision 1.3
1/* $OpenBSD: preadv.c,v 1.3 2011/11/05 15:43:04 guenther 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 <errno.h> 12#include <fcntl.h> 13 14int 15main(int argc, char *argv[]) 16{ 17 char temp[] = "/tmp/dup2XXXXXXXXX"; 18 const char magic[10] = "0123456789"; 19 char c, c1, c2; 20 int fd, ret; 21 struct iovec iv[2]; 22 23 if ((fd = mkstemp(temp)) < 0) 24 err(1, "mkstemp"); 25 remove(temp); 26 27 if (write(fd, magic, sizeof(magic)) != sizeof(magic)) 28 err(1, "write"); 29 30 if (lseek(fd, 0, SEEK_SET) != 0) 31 err(1, "lseek"); 32 33 if (read(fd, &c, 1) != 1) 34 err(1, "read1"); 35 36 if (c != magic[0]) 37 errx(1, "read1 %c != %c", c, magic[0]); 38 39 iv[0].iov_base = &c1; 40 iv[0].iov_len = 1; 41 iv[1].iov_base = &c2; 42 iv[1].iov_len = 1; 43 44 if (preadv(fd, iv, 2, 7) != 2) 45 err(1, "preadv"); 46 47 if (c1 != magic[7]) 48 errx(1, "preadv %c != %c", c1, magic[7]); 49 50 if (c2 != magic[8]) 51 errx(1, "preadv %c != %c", c2, magic[7]); 52 53 if (read(fd, &c, 1) != 1) 54 err(1, "read2"); 55 56 if (c != magic[1]) 57 errx(1, "read2 %c != %c", c, magic[1]); 58 59 close(fd); 60 61 /* also, verify that preadv fails on ttys */ 62 fd = open("/dev/tty", O_RDWR); 63 if (fd < 0) 64 printf("skipping tty test\n"); 65 else if ((ret = preadv(fd, iv, 2, 7)) != -1) 66 errx(1, "preadv succeeded on tty, returning %d", ret); 67 else if (errno != ESPIPE) 68 err(1, "preadv"); 69 70 return 0; 71} 72