From 92fec3ece8f1732c50d5e39503853d5b6a6ba2f4 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Mon, 25 Jun 2018 18:15:02 +0900 Subject: [PATCH] libc fixes --- libc/locale/setlocale.c | 2 +- libc/stdio/printf.c | 34 ++++++++++++++++++++++++++-------- libc/wchar/wcs.c | 4 ++++ libc/wchar/wcsncpy.c | 2 +- libc/wchar/wcstok.c | 2 +- 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/libc/locale/setlocale.c b/libc/locale/setlocale.c index 9a4112dc..411852cc 100644 --- a/libc/locale/setlocale.c +++ b/libc/locale/setlocale.c @@ -2,6 +2,6 @@ #include char * setlocale(int category, const char *locale) { - return NULL; /* Unsupported */ + return "en_US"; } diff --git a/libc/stdio/printf.c b/libc/stdio/printf.c index 5e3faf1a..691e6737 100644 --- a/libc/stdio/printf.c +++ b/libc/stdio/printf.c @@ -109,6 +109,7 @@ size_t xvasprintf(char * buf, const char * fmt, va_list args) { unsigned int arg_width = 0; int align = 1; /* right */ int fill_zero = 0; + int big = 0; if (*f == '-') { align = 0; ++f; @@ -126,19 +127,36 @@ size_t xvasprintf(char * buf, const char * fmt, va_list args) { arg_width += *f - '0'; ++f; } + if (*f == 'l') { + big = 1; + ++f; + } /* fmt[i] == '%' */ switch (*f) { case 's': /* String pointer -> String */ { - s = (char *)va_arg(args, char *); - if (s == NULL) { - s = "(null)"; - } size_t count = 0; - while (*s) { - *b++ = *s++; - count++; - if (arg_width && count == arg_width) break; + if (big) { + wchar_t * ws = (wchar_t *)va_arg(args, wchar_t *); + if (ws == NULL) { + ws = L"(null)"; + } + size_t count = 0; + while (*ws) { + *b++ = *ws++; + count++; + if (arg_width && count == arg_width) break; + } + } else { + s = (char *)va_arg(args, char *); + if (s == NULL) { + s = "(null)"; + } + while (*s) { + *b++ = *s++; + count++; + if (arg_width && count == arg_width) break; + } } while (count < arg_width) { *b++ = ' '; diff --git a/libc/wchar/wcs.c b/libc/wchar/wcs.c index ab52aa28..20c00123 100644 --- a/libc/wchar/wcs.c +++ b/libc/wchar/wcs.c @@ -6,6 +6,8 @@ size_t wcstombs(char * dest, const wchar_t *src, size_t n) { while (c < n && *src) { *dest = *src; c++; + src++; + dest++; } return c; } @@ -16,6 +18,8 @@ size_t mbstowcs(wchar_t * dest, const char *src, size_t n) { while (c < n && *src) { *dest = *src; c++; + src++; + dest++; } return c; } diff --git a/libc/wchar/wcsncpy.c b/libc/wchar/wcsncpy.c index b12a481c..ae902363 100644 --- a/libc/wchar/wcsncpy.c +++ b/libc/wchar/wcsncpy.c @@ -3,8 +3,8 @@ wchar_t * wcsncpy(wchar_t * dest, const wchar_t * src, size_t n) { wchar_t * out = dest; while (n > 0) { - if (!*src) break; *dest = *src; + if (!*src) break; dest++; src++; n--; diff --git a/libc/wchar/wcstok.c b/libc/wchar/wcstok.c index 19e71a60..cbbaeca7 100644 --- a/libc/wchar/wcstok.c +++ b/libc/wchar/wcstok.c @@ -32,7 +32,7 @@ wchar_t *wcspbrk(const wchar_t *wcs, const wchar_t *accept) { } wchar_t * wcschr(const wchar_t *wcs, wchar_t wc) { - while (*wcs != wc && *wcs != L'0') { + while (*wcs != wc && *wcs != 0) { wcs++; } if (!*wcs) return NULL;