Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice01 / ex2-head.c
diff options
context:
space:
mode:
Commita594346d7e2ca97717cae7e705c00d6fe0b9a460 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-12-02 18:10:23 +0200
Committer
 Syndamia <kamen@syndamia.com>
2023-12-02 18:10:23 +0200
Files995c765606f9f4277e8ba5051a315e76dfb90391 /Year 3/System programming/Practice01/ex2-head.c
uni-projects-a594346d7e2ca97717cae7e705c00d6fe0b9a460.tar
uni-projects-a594346d7e2ca97717cae7e705c00d6fe0b9a460.tar.gz
uni-projects-a594346d7e2ca97717cae7e705c00d6fe0b9a460.zip
Parentcf86e964b166a32227aa4bb0cc1de27a37244c69 (diff)
[SP/P01] Solved exercises from first practice (again)
1 files changed, 21 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practice01/ex2-head.c21
diff --git a/Year 3/System programming/Practice01/ex2-head.c b/Year 3/System programming/Practice01/ex2-head.c
new file mode 100644
index 0000000..197e8d2
--- /dev/null
+++ b/Year 3/System programming/Practice01/ex2-head.c
@@ -0,0 +1,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);
+}