Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum1 / tail.c
blob: 35b7d7ffb7522b1714d9b2d90326e26ea4c7131a (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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
	int f = open(argv[1], O_RDONLY);
	if (f == -1) return 1;

	int lines = 1;
	char buf;
	lseek(f, 0, SEEK_END);
	do {
		read(f, &buf, 1);
		if (buf == '\n') lines--;
	} while(lines > 0 && lseek(f, -2, SEEK_CUR));

	while (read(f, &buf, 1)) {
		write(0, &buf, 1);
	}

	close(f);
}