commit 3273cb68f82adb0347e3ad1810fc891ad3dbe217
parent 70b03ed486e3fddcc9416a4a3e38576455ebfe98
Author: Mattias Andrée <maandree@kth.se>
Date: Sun, 8 Jan 2017 05:16:25 +0100
Add vu-repeat
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat:
| A | src/vu-repeat.c | | | 59 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 59 insertions(+), 0 deletions(-)
diff --git a/src/vu-repeat.c b/src/vu-repeat.c
@@ -0,0 +1,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;
+}