commit 45288b3777f08d28498f04f554105b77821317b8
parent cef6981283f4881c9d4d6df78db7ecd3b5028938
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Mon, 16 Dec 2024 15:36:13 +0000
add cat example
Diffstat:
3 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/Tupfile b/Tupfile
@@ -1,7 +1,9 @@
CFLAGS = -Os -fno-asynchronous-unwind-tables -no-pie -g
LDFLAGS = -static -nostartfiles -nostdlib -no-pie -Xlinker --gc-sections
-: foreach *.c *.s |> cc $(CFLAGS) -c -o %o %f |> obj/%B.o {objs}
-: {objs} |> cc $(LDFLAGS) -o %o -T x86_64.ld %f |> echo
+: foreach *.s |> cc $(CFLAGS) -c -o %o %f |> obj/%B.o {asm_objs}
+: foreach *.c |> cc $(CFLAGS) -c -o %o %f |> obj/%B.o
+: obj/echo.o {asm_objs} | x86_64.ld |> cc $(LDFLAGS) -o %o -T x86_64.ld %f |> %1B
+: obj/cat.o {asm_objs} | x86_64.ld |> cc $(LDFLAGS) -o %o -T x86_64.ld %f |> %1B
.gitignore
diff --git a/cat.c b/cat.c
@@ -0,0 +1,29 @@
+#include "syscall.h"
+
+static void cat(int fd)
+{
+ static char buf[0x1000];
+
+ for (;;)
+ {
+ int n = read(fd, buf, sizeof(buf));
+ if (n < 0)
+ exit(2);
+ if (n == 0)
+ return;
+ if (write(1, buf, n) != n)
+ exit(3);
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ for (int i = 1; i < argc; ++i)
+ {
+ int fd = open(argv[i], 0, /*O_RDONLY*/0);
+ if (fd < 0)
+ return 1;
+ cat(fd);
+ close(fd);
+ }
+}
diff --git a/main.c b/echo.c