From 5a25f38de6d3e2506838191c55af94cb56c9f641 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sun, 19 Apr 2020 10:06:21 -0700 Subject: feat: quality of life scripts to initialize make rules --- sys/libc/stdio.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 sys/libc/stdio.c (limited to 'sys/libc/stdio.c') diff --git a/sys/libc/stdio.c b/sys/libc/stdio.c new file mode 100644 index 0000000..f3295b2 --- /dev/null +++ b/sys/libc/stdio.c @@ -0,0 +1,59 @@ +#include +#include + +int +printf(byte* fmt, ...) +{ + va_list args; + va_start(args, fmt); + + int nw, rem, peek, len; + byte* str; + + while (*fmt) { + rem = INT_MAX - nw; + + if (fmt[0] != '%' || fmt[1] == '%') { + if (fmt[0] == '%') fmt++; + + for (peek = 1; fmt[peek] && fmt[peek] != '%'; peek++) { + ; + } + if (rem < peek) return -1; + // TODO: Print here. + fmt += peek; + nw += peek; + continue; + } + + str = fmt++; + + switch (*fmt++) { + case 'c': + byte c = va_arg(args, int); + if (rem < 0) return -1; + // TODO: Print here + nw++; + break; + + case 's': + str = va_arg(args, byte*); + len = strlen(str); + if (rem < len) return -1; + // TODO: Print here + nw += len; + break; + default: + fmt = str; + len = strlen(fmt); + if (rem < len) return -1; + // TODO: Print here + nw += len; + fmt += len; + break; + } + } + + va_end(args); + return nw; +} -- cgit v1.2.1