From dbdaa77fb5924b4b9e6b374a44ef76481a38d3d2 Mon Sep 17 00:00:00 2001 From: aqua Date: Tue, 8 Nov 2022 17:19:45 +0200 Subject: Add FILE struct --- lib/Makefile | 2 +- lib/stdio.h | 34 +++++++++++++++++++++++++- lib/stdio/fprintf.c | 11 +++++++++ lib/stdio/printf.c | 69 ++++------------------------------------------------ lib/stdio/vfprintf.c | 54 ++++++++++++++++++++++++++++++++++++++++ lib/stdlib.h | 4 ++- 6 files changed, 107 insertions(+), 67 deletions(-) create mode 100644 lib/stdio/fprintf.c create mode 100644 lib/stdio/vfprintf.c (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile index 66181c8..f564ee0 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -2,7 +2,7 @@ include ../Makefile.config CCFLAGS += -I. -I.. -libk.SRCS = stdio/printf.c \ +libk.SRCS = stdio/printf.c stdio/fprintf.c stdio/vfprintf.c \ stdlib/memcpy.c stdlib/memset.c \ string/itoa.c diff --git a/lib/stdio.h b/lib/stdio.h index 201cce1..4711a29 100644 --- a/lib/stdio.h +++ b/lib/stdio.h @@ -1,6 +1,38 @@ #pragma once +#include + +/** An object type used for streams */ +typedef struct FILE_t { + int id; + /** Functions that prints a character to the stream */ + void (*putc)(const struct FILE_t *, char); + /** Function that prints a string to the stream */ + int (*puts)(const struct FILE_t *, const char *, int); + /** Flush all buffers */ + void (*flush)(const struct FILE_t *); +} FILE; + +/** A FILE value corresponding to stdin, the keyboard buffer */ +extern FILE *stdin; +/** A FILE value corresponding to stdout, the display */ +extern FILE *stdout; +/** A FILE value corresponding to stderr, the uart */ +extern FILE *stderr; + /** - * Supports %s (string), %d (decimal), %u (unsigned), %x (hexadecimal) + * Write the formatted string to stdout + * + * Supports ``%s`` (string), ``%d`` (decimal), ``%u`` (unsigned), ``%x`` (hexadecimal) */ int printf(const char *restrict format, ...); + +/** + * Write the formatted string to stream; see printf + */ +int fprintf(FILE *restrict stream, const char *restrict format, ...); + +/** + * Write the formatted string to stream; see printf + */ +int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap); diff --git a/lib/stdio/fprintf.c b/lib/stdio/fprintf.c new file mode 100644 index 0000000..9a96dc6 --- /dev/null +++ b/lib/stdio/fprintf.c @@ -0,0 +1,11 @@ +#include + +int +fprintf(FILE *restrict stream, const char *restrict format, ...) +{ + va_list ap; + va_start(ap, format); + int c = vfprintf(stream, format, ap); + va_end(ap); + return c; +} diff --git a/lib/stdio/printf.c b/lib/stdio/printf.c index ae4d5b1..4efc1ac 100644 --- a/lib/stdio/printf.c +++ b/lib/stdio/printf.c @@ -1,70 +1,11 @@ -#include #include -#include - -#include -#include - -static char buffer[3 * sizeof(int) + 2]; int printf(const char *restrict format, ...) { - int written = 0; - va_list params; - va_start(params, format); - - int s = 0; - int l = 0; - for (int i = 0; format[i] != '\0'; ++i) { - if (format[i] == '%') { - written += uart_puts(COM1, &format[s], l); - vga_puts(&format[s], l); - s = i + 2; - ++i; - - switch (format[i]) { - case 's': { - const char *arg = va_arg(params, const char *); - written += uart_puts(COM1, arg, -1); - vga_puts(arg, -1); - } break; - case 'c': { - const int arg = va_arg(params, int); - uart_write(COM1, arg); - vga_putc(arg); - ++written; - } break; - case 'd': { - const char *arg = itoa(buffer, va_arg(params, int), 10); - written += uart_puts(COM1, arg, -1); - vga_puts(arg, -1); - } break; - case 'u': { - const char *arg = utoa(buffer, va_arg(params, unsigned int), 10); - written += uart_puts(COM1, arg, -1); - vga_puts(arg, -1); - } break; - case 'x': { - const char *arg = utoa(buffer, va_arg(params, unsigned int), 16); - written += uart_puts(COM1, arg, -1); - vga_puts(arg, -1); - } break; - } - - l = 0; - } - - else - ++l; - } - - if (l > 0) { - written += uart_puts(COM1, &format[s], l); - vga_puts(&format[s], l); - } - - va_end(params); - vga_update_cursor(); - return written; + va_list ap; + va_start(ap, format); + int c = vfprintf(stdout, format, ap); + va_end(ap); + return c; } diff --git a/lib/stdio/vfprintf.c b/lib/stdio/vfprintf.c new file mode 100644 index 0000000..d24e43e --- /dev/null +++ b/lib/stdio/vfprintf.c @@ -0,0 +1,54 @@ +#include +#include + +static char buffer[3 * sizeof(int) + 2]; + +int +vfprintf(FILE *restrict stream, const char *restrict format, va_list params) +{ + int written = 0; + + int s = 0; + int l = 0; + for (int i = 0; format[i] != '\0'; ++i) { + if (format[i] == '%') { + written += stream->puts(stream, &format[s], l); + s = i + 2; + ++i; + + switch (format[i]) { + case 's': { + const char *arg = va_arg(params, const char *); + written += stream->puts(stream, arg, -1); + } break; + case 'c': { + const int arg = va_arg(params, int); + stream->putc(stream, arg); + ++written; + } break; + case 'd': { + const char *arg = itoa(buffer, va_arg(params, int), 10); + written += stream->puts(stream, arg, -1); + } break; + case 'u': { + const char *arg = utoa(buffer, va_arg(params, unsigned int), 10); + written += stream->puts(stream, arg, -1); + } break; + case 'x': { + const char *arg = utoa(buffer, va_arg(params, unsigned int), 16); + written += stream->puts(stream, arg, -1); + } break; + } + + l = 0; + } + + else + ++l; + } + + if (l > 0) { written += stream->puts(stream, &format[s], l); } + + stream->flush(stream); + return written; +} diff --git a/lib/stdlib.h b/lib/stdlib.h index 7f235f0..bd8474d 100644 --- a/lib/stdlib.h +++ b/lib/stdlib.h @@ -1,9 +1,11 @@ #pragma once +#include + /** * Allocate size bytes and return a pointer to the allocated memory */ -void *malloc(unsigned int size); +void *malloc(size_t size); /** * Free the memory space pointed to by ptr -- cgit v1.2.1