Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice01 / ex2-head.c
blob: 197e8d25d0bdb5423716bb6ad1c4827e06a34de1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <unistd.h> // ssize_t, read, write
#include <fcntl.h> // open, close, O_RDONLY, ...
#include <stdio.h> // printf, perror

int main(int argc, char* argv[]) {
	int f = open(argv[1], O_RDONLY);
	if (f < 0) {
		perror("open");
		return 1;
	}

	ssize_t lines = 10;

	char buf;
	while (read(f, &buf, 1) && lines > 0) {
		if (buf == '\n') lines--;
		printf("%c", buf);
	}

	close(f);
}