Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum1 / tail.c
diff options
context:
space:
mode:
Commit3ba593c0c4da762969a48a5c89c4a096e93680a5 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-10-04 10:20:22 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-10-04 10:20:22 +0300
Files10f076dbd76e7dec550064f0d20179a23f757afe /Year 3/System programming/Practicum1/tail.c
uni-projects-3ba593c0c4da762969a48a5c89c4a096e93680a5.tar
uni-projects-3ba593c0c4da762969a48a5c89c4a096e93680a5.tar.gz
uni-projects-3ba593c0c4da762969a48a5c89c4a096e93680a5.zip
Parent91d5a6e02ebb1535026887d49da3ef2f48406519 (diff)
[SP/P1] Solved all exercises
1 files changed, 24 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practicum1/tail.c24
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 <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);
+}