Browse Source

libc fixes

K. Lange 4 years ago
parent
commit
92fec3ece8
5 changed files with 33 additions and 11 deletions
  1. 1 1
      libc/locale/setlocale.c
  2. 26 8
      libc/stdio/printf.c
  3. 4 0
      libc/wchar/wcs.c
  4. 1 1
      libc/wchar/wcsncpy.c
  5. 1 1
      libc/wchar/wcstok.c

+ 1 - 1
libc/locale/setlocale.c

@@ -2,6 +2,6 @@
 #include <locale.h>
 
 char * setlocale(int category, const char *locale) {
-    return NULL; /* Unsupported */
+    return "en_US";
 }
 

+ 26 - 8
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++ = ' ';

+ 4 - 0
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;
 }

+ 1 - 1
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--;

+ 1 - 1
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;