From 3ba593c0c4da762969a48a5c89c4a096e93680a5 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 4 Oct 2023 10:20:22 +0300 Subject: [SP/P1] Solved all exercises --- Year 3/System programming/Practicum1/tail.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Year 3/System programming/Practicum1/tail.c (limited to 'Year 3/System programming/Practicum1/tail.c') diff --git a/Year 3/System programming/Practicum1/tail.c b/Year 3/System programming/Practicum1/tail.c new file mode 100644 index 0000000..35b7d7f --- /dev/null +++ b/Year 3/System programming/Practicum1/tail.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include + +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); +} -- cgit v1.3.1-26-gb76a