From 989f9ec5fe82fbe451a0861076aaf19084643f6a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 7 Apr 2023 02:26:44 +0300 Subject: [PATCH] LibC: remove old unused files --- libc/stdio/printf.cpp | 191 ----------------------------------------- libc/stdio/putchar.cpp | 17 ---- libc/stdio/puts.cpp | 6 -- 3 files changed, 214 deletions(-) delete mode 100644 libc/stdio/printf.cpp delete mode 100644 libc/stdio/putchar.cpp delete mode 100644 libc/stdio/puts.cpp diff --git a/libc/stdio/printf.cpp b/libc/stdio/printf.cpp deleted file mode 100644 index 429eecbf40..0000000000 --- a/libc/stdio/printf.cpp +++ /dev/null @@ -1,191 +0,0 @@ -#include -#include -#include -#include -#include -#include - -static bool print(const char* data, size_t len) -{ - const unsigned char* bytes = reinterpret_cast(data); - for(size_t i = 0; i < len; i++) - if (putchar(bytes[i]) == EOF) - return false; - return true; -} - -template -static bool print_integer(T value, size_t& out_len) -{ - char buffer[32] {}; - int len = 0; - bool sign = false; - - if (value < 0) - { - sign = true; - buffer[len++] = ((value % 10 + 10) % 10) + '0'; - value = -(value / 10); - } - - while (value) - { - buffer[len++] = (value % 10) + '0'; - value /= 10; - } - - if (sign) - buffer[len++] = '-'; - if (len == 0) - buffer[len++] = '0'; - - for (int i = 0; i < len / 2; i++) - { - char temp = buffer[i]; - buffer[i] = buffer[len - i - 1]; - buffer[len - i - 1] = temp; - } - - if (!print(buffer, len)) - return false; - - out_len = len; - return true; -} - -static char bits_to_hex(int bits, bool upper_case) -{ - if (bits < 10) - return bits + '0'; - return bits - 10 + (upper_case ? 'A' : 'a'); -} - -template -static bool print_hex(T value, bool upper_case, size_t& out_len) -{ - char buffer[16] {}; - int len = 0; - - while (value) - { - buffer[len++] = bits_to_hex(value & 0xF, upper_case); - value >>= 4; - } - - if (len == 0) - buffer[len++] = '0'; - - for (int i = 0; i < len / 2; i++) - { - char temp = buffer[i]; - buffer[i] = buffer[len - i - 1]; - buffer[len - i - 1] = temp; - } - - if (!print(buffer, len)) - return false; - - out_len = len; - return true; -} - -int printf(const char* __restrict format, ...) -{ - va_list args; - va_start(args, format); - - int written = 0; - - while (*format) - { - size_t max_rem = INT_MAX - written; - - if (format[0] != '%' || format[1] == '%') - { - if (format[0] == '%') - format++; - size_t len = 1; - while (format[len] && format[len] != '%') - len++; - if (!print(format, len)) - return -1; - format += len; - written += len; - continue; - } - - const char* format_start = format++; - - if (*format == 'c') - { - format++; - char c = (char)va_arg(args, int); - if (!max_rem) // FIXME: EOVERFLOW - return -1; - if (!print(&c, sizeof(c))) - return -1; - written++; - } - else if (*format == 's') - { - format++; - const char* str = va_arg(args, const char*); - size_t len = strlen(str); - if (max_rem < len) // FIXME: EOVERFLOW - return -1; - if (!print(str, len)) - return -1; - written += len; - } - else if (*format == 'd' || *format == 'i') - { - format++; - int value = va_arg(args, int); - size_t len; - if (!print_integer(value, len)) - return -1; - written += len; - } - else if (*format == 'u') - { - format++; - unsigned int value = va_arg(args, unsigned int); - size_t len; - if (!print_integer(value, len)) - return -1; - written += len; - } - else if (*format == 'x' || *format == 'X') - { - format++; - unsigned int value = va_arg(args, unsigned int); - size_t len; - if (!print_hex(value, *(format - 1) == 'X', len)) - return -1; - written += len; - } - else if (*format == 'p') - { - format++; - void* ptr = va_arg(args, void*); - size_t len; - if (!print("0x", 2) || !print_hex((ptrdiff_t)ptr, false, len)) - return -1; - written += len; - } - else - { - format = format_start; - size_t len = strlen(format); - if (max_rem < len) // FIXME: EOVERFLOW - return -1; - if (!print(format, len)) - return -1; - written += len; - format += len; - } - } - - va_end(args); - return written; -} \ No newline at end of file diff --git a/libc/stdio/putchar.cpp b/libc/stdio/putchar.cpp deleted file mode 100644 index c26e85692c..0000000000 --- a/libc/stdio/putchar.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include - -#if defined(__is_libk) - #include -#else - #include -#endif - -int putchar(int c) -{ -#if defined(__is_libk) - Kernel::panic("Please use kprint() instead of stdio"); -#else - abort(); -#endif - return c; -} \ No newline at end of file diff --git a/libc/stdio/puts.cpp b/libc/stdio/puts.cpp deleted file mode 100644 index ba661b8804..0000000000 --- a/libc/stdio/puts.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int puts(const char* str) -{ - return printf("%s\n", str); -} \ No newline at end of file