File: small-cat.c

package info (click to toggle)
libfiu 1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ansic: 2,633; python: 973; makefile: 599; sh: 309
file content (36 lines) | stat: -rw-r--r-- 695 bytes parent folder | download | duplicates (6)
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
// A small "cat" utility that copies stdin to stdout.
// It does only one 4K read from stdin, and writes it to stdout in as few
// write()s as possible.
// This gives a controlled number of operations, which makes testing the
// random operations more robust.

#include <stdio.h>  // printf(), perror()
#include <unistd.h> // read(), write()

const size_t BUFSIZE = 4092;

int main(void)
{
	char buf[BUFSIZE];
	ssize_t r, w, pos;

	r = read(0, buf, BUFSIZE);
	if (r < 0) {
		perror("Read error in small-cat");
		return 1;
	}

	pos = 0;
	while (r > 0) {
		w = write(1, buf + pos, r);
		if (w <= 0) {
			perror("Write error in small-cat");
			return 2;
		}

		pos += w;
		r -= w;
	}

	return 0;
}