Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practicum1 / head.c
diff options
context:
space:
mode:
1 files changed, 19 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practicum1/head.c19
diff --git a/Year 3/System programming/Practicum1/head.c b/Year 3/System programming/Practicum1/head.c
new file mode 100644
index 0000000..73bb144
--- /dev/null
+++ b/Year 3/System programming/Practicum1/head.c
@@ -0,0 +1,19 @@
+#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;
+ while (lines > 0 && read(f, &buf, 1)) {
+ if (buf == '\n') lines--;
+ write(0, &buf, 1);
+ }
+
+ close(f);
+}