summaryrefslogtreecommitdiff
path: root/regress/sys/kern/preadv/preadv.c
blob: 261af95960828fc93c5de5c1b6989c60fd0ff774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*	$OpenBSD: preadv.c,v 1.2 2003/07/31 21:48:09 deraadt Exp $	*/
/*
 *	Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
 */
#include <sys/types.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <fcntl.h>

int
main(int argc, char *argv[])
{	
	char temp[] = "/tmp/dup2XXXXXXXXX";
	const char magic[10] = "0123456789";
	char c, c1, c2;
	int fd;
	struct iovec iv[2];

	if ((fd = mkstemp(temp)) < 0)
		err(1, "mkstemp");
	remove(temp);

	if (write(fd, magic, sizeof(magic)) != sizeof(magic))
		err(1, "write");

	if (lseek(fd, 0, SEEK_SET) != 0)
		err(1, "lseek");

	if (read(fd, &c, 1) != 1)
		err(1, "read1");

	if (c != magic[0])
		errx(1, "read1 %c != %c", c, magic[0]);

	iv[0].iov_base = &c1;
	iv[0].iov_len = 1;
	iv[1].iov_base = &c2;
	iv[1].iov_len = 1;

	if (preadv(fd, iv, 2, 7) != 2)
		err(1, "pread");

	if (c1 != magic[7])
		errx(1, "pread %c != %c", c1, magic[7]);

	if (c2 != magic[8])
		errx(1, "pread %c != %c", c2, magic[7]);

	if (read(fd, &c, 1) != 1)
		err(1, "read2");

	if (c != magic[1])
		errx(1, "read2 %c != %c", c, magic[1]);

	return 0;
}