reimplmenets the previous iteration with a brand new custom build system
This commit is contained in:
80
libc/stdio/printf.c
Normal file
80
libc/stdio/printf.c
Normal file
@ -0,0 +1,80 @@
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool print(const char* data, size_t length) {
|
||||
const unsigned char* bytes = (const unsigned char*) data;
|
||||
for (size_t i = 0; i < length; i++)
|
||||
if (putchar(bytes[i]) == EOF)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
int printf(const char* restrict format, ...) {
|
||||
va_list parameters;
|
||||
va_start(parameters, format);
|
||||
|
||||
int written = 0;
|
||||
|
||||
while (*format != '\0') {
|
||||
size_t maxrem = INT_MAX - written;
|
||||
|
||||
if (format[0] != '%' || format[1] == '%') {
|
||||
if (format[0] == '%')
|
||||
format++;
|
||||
size_t amount = 1;
|
||||
while (format[amount] && format[amount] != '%')
|
||||
amount++;
|
||||
if (maxrem < amount) {
|
||||
// TODO: Set errno to EOVERFLOW.
|
||||
return -1;
|
||||
}
|
||||
if (!print(format, amount))
|
||||
return -1;
|
||||
format += amount;
|
||||
written += amount;
|
||||
continue;
|
||||
}
|
||||
|
||||
const char* format_begun_at = format++;
|
||||
|
||||
if (*format == 'c') {
|
||||
format++;
|
||||
char c = (char) va_arg(parameters, int /* char promotes to int */);
|
||||
if (!maxrem) {
|
||||
// TODO: Set errno to EOVERFLOW.
|
||||
return -1;
|
||||
}
|
||||
if (!print(&c, sizeof(c)))
|
||||
return -1;
|
||||
written++;
|
||||
} else if (*format == 's') {
|
||||
format++;
|
||||
const char* str = va_arg(parameters, const char*);
|
||||
size_t len = strlen(str);
|
||||
if (maxrem < len) {
|
||||
// TODO: Set errno to EOVERFLOW.
|
||||
return -1;
|
||||
}
|
||||
if (!print(str, len))
|
||||
return -1;
|
||||
written += len;
|
||||
} else {
|
||||
format = format_begun_at;
|
||||
size_t len = strlen(format);
|
||||
if (maxrem < len) {
|
||||
// TODO: Set errno to EOVERFLOW.
|
||||
return -1;
|
||||
}
|
||||
if (!print(format, len))
|
||||
return -1;
|
||||
written += len;
|
||||
format += len;
|
||||
}
|
||||
}
|
||||
|
||||
va_end(parameters);
|
||||
return written;
|
||||
}
|
15
libc/stdio/putchar.c
Normal file
15
libc/stdio/putchar.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(__is_libk)
|
||||
#include <kernel/tty.h>
|
||||
#endif
|
||||
|
||||
int putchar(int ic) {
|
||||
#if defined(__is_libk)
|
||||
char c = (char) ic;
|
||||
terminal_write(&c, sizeof(c));
|
||||
#else
|
||||
// TODO: Implement stdio and the write system call.
|
||||
#endif
|
||||
return ic;
|
||||
}
|
5
libc/stdio/puts.c
Normal file
5
libc/stdio/puts.c
Normal file
@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int puts(const char* string) {
|
||||
return printf("%s\n", string);
|
||||
}
|
Reference in New Issue
Block a user