aboutsummaryrefslogtreecommitdiffstats
path: root/src/vu-repeat.c
blob: 69ac8ce818e44de5e7188fe2992797c8fd8fd785 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "arg.h"
#include "util.h"

#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>

static void
usage(void)
{
	eprintf("usage: %s count file\n", argv0);
}

int
main(int argc, char *argv[])
{
	struct stat st;
	size_t count, ptr, n, ptw;
	int fd;
	ssize_t r;
	char buf[BUFSIZ];

	ARGBEGIN {
	default:
		usage();
	} ARGEND;

	if (argc != 2)
		usage();

	if (tozu(argv[0], 0, SIZE_MAX, &count))
		eprintf("the count must be an integer in [0, %zu]\n", SIZE_MAX);

	fd = open(argv[1], O_RDONLY);
	if (fd < 0)
		eprintf("open %s:", argv[1]);

	while (count--) {
		posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
		for (ptr = 0;;) {
			r = pread(fd, buf, sizeof(buf), ptr);
			if (r < 0)
				eprintf("pread %s:", argv[1]);
			else if (r == 0)
				break;
			ptr += n = (size_t)r;
			for (ptw = 0; ptw < n;) {
				r = write(STDOUT_FILENO, buf + ptw, n - ptw);
				if (r < 0)
					eprintf("write <stdout>:");
				ptw += (size_t)r;
			}
		}
	}

	close(fd);
	return 0;
}