diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2022-12-15 22:46:25 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2022-12-15 22:46:25 +0100 |
| commit | b9038b1c6b31a8f8d6b8b648cee0cf3b457dc24c (patch) | |
| tree | d967cf38fba25c1a3eed621026f37891bba8f191 /lib | |
| parent | 02062f0cf84e1cb7fb294de54b0c00db6323c529 (diff) | |
first version of io and wstdf
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/io/Makefile | 3 | ||||
| -rw-r--r-- | lib/io/io.c | 50 | ||||
| -rw-r--r-- | lib/io/io.h | 17 | ||||
| -rwxr-xr-x | lib/io/test | bin | 0 -> 20240 bytes | |||
| -rw-r--r-- | lib/sys/io.h | 4 | ||||
| -rw-r--r-- | lib/sys/syscalls.h | 1 | ||||
| -rw-r--r-- | lib/sys/write.h | 2 |
7 files changed, 73 insertions, 4 deletions
diff --git a/lib/io/Makefile b/lib/io/Makefile new file mode 100644 index 0000000..e60a656 --- /dev/null +++ b/lib/io/Makefile @@ -0,0 +1,3 @@ +unit_test: + gcc io.c ../cstr/cstr.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -g + ./test diff --git a/lib/io/io.c b/lib/io/io.c new file mode 100644 index 0000000..41cd3e0 --- /dev/null +++ b/lib/io/io.c @@ -0,0 +1,50 @@ +#include "io.h" + +#include "../cstr/cstr.h" + +void __wstdn(const char *buf, u64 n) +{ + write(STDOUT_FD, buf, n); +} + + +void wstd(const char *buf) +{ + __wstdn(buf, cstr_length(buf)); +} + + +void rstd(char *buf, unsigned long count) +{ + read(STDIN_FD, buf, count); +} + +void wstdf__(const char *buf, void **args) +{ + const char *start = buf; + for (; *buf; ++buf) { + if (*buf == '%') { + if (buf - start > 0) + __wstdn(start, buf - start); + + ++buf; + + switch (*buf) { + case '%': __wstdn("%", 1); break; + case 's': + __wstdn(((const char **)args)[0], cstr_length(((const char**)args)[0])); + ++args; break; + } + + start = buf + 1; + } + } + + if (buf - start > 0) + __wstdn(start, buf - start); +} + +int main() { + void *args[] = { "hallo", "welt" }; + wstdf__("%s %s\n", args); +} diff --git a/lib/io/io.h b/lib/io/io.h new file mode 100644 index 0000000..b68a75d --- /dev/null +++ b/lib/io/io.h @@ -0,0 +1,17 @@ +#ifndef IO_H +#define IO_H + +#include "../sys/io.h" + +void wstd(const char *buf); +void rstd(char *buf, unsigned long count); + +void wstdf__(const char *buf, void **args); + +#define wstdf(buf, ...) { \ + void *__wstdf__args__[] = { __VA_ARGS__ }; \ + wstdf__(buf, __wstdf__args__); \ +} + + +#endif diff --git a/lib/io/test b/lib/io/test Binary files differnew file mode 100755 index 0000000..560b047 --- /dev/null +++ b/lib/io/test diff --git a/lib/sys/io.h b/lib/sys/io.h index 3048ebe..8086869 100644 --- a/lib/sys/io.h +++ b/lib/sys/io.h @@ -1,5 +1,5 @@ -#ifndef IO_H -#define IO_H +#ifndef IO_SYS_H +#define IO_SYS_H #include "write.h" #include "read.h" diff --git a/lib/sys/syscalls.h b/lib/sys/syscalls.h index 03ce997..50e8a4f 100644 --- a/lib/sys/syscalls.h +++ b/lib/sys/syscalls.h @@ -3,7 +3,6 @@ __asm__ ( "syscall:\n" - "endbr64\n" "mov %rdi, %rax\n" "mov %rsi, %rdi\n" "mov %rdx, %rsi\n" diff --git a/lib/sys/write.h b/lib/sys/write.h index 6ad748e..6447ed8 100644 --- a/lib/sys/write.h +++ b/lib/sys/write.h @@ -3,7 +3,7 @@ #include "syscalls.h" -static int write(unsigned int fd, const char * buf, unsigned long count) +static int write(unsigned int fd, const char *buf, unsigned long count) { return syscall(WRITE, fd, buf, count); } |