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