tests: Make random tests more robust
[libfiu.git] / tests / small-cat.c
blob4fa42f1bd105724003daaac10e6331a54c60b46b
1 // A small "cat" utility that copies stdin to stdout.
2 // It does only one 4K read from stdin, and writes it to stdout in as few
3 // write()s as possible.
4 // This gives a controlled number of operations, which makes testing the
5 // random operations more robust.
7 #include <stdio.h> // printf(), perror()
8 #include <unistd.h> // read(), write()
10 const size_t BUFSIZE = 4092;
12 int main(void)
14 char buf[BUFSIZE];
15 ssize_t r, w, pos;
17 r = read(0, buf, BUFSIZE);
18 if (r < 0) {
19 perror("Read error in small-cat");
20 return 1;
23 pos = 0;
24 while (r > 0) {
25 w = write(1, buf + pos, r);
26 if (w <= 0) {
27 perror("Write error in small-cat");
28 return 2;
31 pos += w;
32 r -= w;
35 return 0;