Browse Source

Ok... here's the summary:
Hopefully locale support will build when cross compiling now. Collation is
still not supported, but that's what I'm currently working on. In the
next couple of days, I'll probably put up a couple of files for download
that will save people the trouble of generating all the glibc locales.
Added *wprintf functions, although they currently don't support floating
point. That will be fixed when I rewrite _dtostr... or possibly before.
Added the wcsto{inttype} functions.
Added iconv() and a mini iconv utility. The require locale support and
only provide for conversions involving the various unicode encodings
{ UCS-4*, UCS-2*, UTF-32*, UTF-16*, UTF-8 }, the 8-bit codesets built
with the locale data, and the internal WCHAR_T.

Manuel Novoa III 21 years ago
parent
commit
c386ddb4d8

+ 6 - 0
Makefile

@@ -274,6 +274,11 @@ ifeq ($(strip $(HAVE_SHARED)),y)
 	    install -m 755 ldso/util/ldconfig.target $(PREFIX)$(TARGET_PREFIX)/sbin/ldconfig; \
 	fi;
 endif
+ifeq ($(strip $(UCLIBC_HAS_LOCALE)),y)
+	@$(MAKE) -C libc/misc/wchar iconv.target
+	install -d $(PREFIX)$(TARGET_PREFIX)/usr/bin;
+	install -m 755 libc/misc/wchar/iconv.target $(PREFIX)$(TARGET_PREFIX)/usr/bin/iconv
+endif
 
 finished2:
 	@echo
@@ -339,6 +344,7 @@ clean:
 	$(MAKE) -C test clean
 	$(MAKE) -C ldso clean
 	$(MAKE) -C libc/misc/internals clean
+	$(MAKE) -C libc/misc/wchar clean
 	$(MAKE) -C libc/unistd clean
 	$(MAKE) -C libc/sysdeps/linux/common clean
 	$(MAKE) -C extra clean

+ 5 - 19
TODO

@@ -18,19 +18,16 @@ Erik's TODO list for the next release:
 -----------------------------------------------------------------------------
 Manuel's todo:
 
-In the next couple of months, I plan on completing the wide char and locale
-support.  Here's my current list of things to implement by early November.
+I'm currently working on completing the wide char and locale support.
+Right now i'm looking at collation.  Other tasks on my list include:
 
   1) Little things that need fixing:
   ----------------------------------
   a) Use locale decimal point in *printf() and strto*d() -- slightly
      complicated by the fact that at least one locale uses a wchar
      radix that does not map to a single byte in UTF-8.
-  b) Use locale digit grouping in *printf().
-  c) Implement %lc (%C) and %ls (%S) handling for printf.
-  d) Deal with mb format string issues in printf, scanf, 
-  strftime (at least).
-  e) Implement wcwidth() and wcswidth() -- use Markus Kuhn's versions.
+  b) Use locale digit grouping in *printf() flosting point.
+  c) Deal with mb format string issues in scanf and strftime (at least).
   
   2) Implement wide char numeric conversion functions -- wcsto*().
   ----------------------------------------------------------------
@@ -38,15 +35,6 @@ support.  Here's my current list of things to implement by early November.
      strto*() functions, unless locale specific alternate digit support
      is desired.
   
-  3) Implement wide formatted output -- *wprintf():
-  -------------------------------------------------
-     The stream output variants should be easily created by appropriately
-     modifying the char *printf() functions.
-  
-     The string buffer variants may take some time to implement because
-     they need to store wchars in a buffer.  It will be necessary to hack
-     the core stdio code slightly to support this.
-  
   4) Reimplement scanf for narrow and wide streams.
   -------------------------------------------------
      The current char version of scanf() needs some cleanup.  Also,
@@ -54,9 +42,6 @@ support.  Here's my current list of things to implement by early November.
      will require reworking the implementation of matching char sets
      (enclosed in []).
   
-     Also, as with *wprintf() above, the string buffer variants of wscanf()
-     may require some additional work to the core stdio code.
-  
   5) Additional str{f|p}time issues.
   ----------------------------------
      Support locale specific alternate digits.
@@ -71,6 +56,7 @@ support.  Here's my current list of things to implement by early November.
   b) Additional clean up of ctype and wctype.
   c) Implement collation support for char and wchar -- *xfrm() and *coll().
   d) transliteration of unsupported wchars in 8-bit locales (like glibc).
+  e) Support ISO/IEC 14652 draft locale extensions (LC_PAPER, etc).
 
 
 Other stuff:

+ 3 - 5
docs/Glibc_vs_uClibc_Differences.txt

@@ -100,17 +100,13 @@ wide char support
    representation of wchar's is assumed to be 31 bit unicode values in
    native endian representation.  Also, the underlying char encoding is
    assumed to match ASCII in the range 0-0x7f.
-2) In the C locale, uClibc's mb<->wc conversion functions map 0x80-CHAR_MAX
-   onto their wide/narrow equivalents.  glibc's conversion functions treat
-   them as illegal.
 
 locale support
 --------------
 1) The target for support is SUSv3 locale functionality.  While nl_langinfo
    has been extended, similar to glibc, it only returns values for related
    locale entries.
-2) The locale code is not cross-compiler friendly.  This should be fixed soon.
-3) Currently, collation support is being implemented.
+2) Currently, collation support is being implemented.
 
 stdio
 -----
@@ -136,6 +132,8 @@ stdio
 8) uClibc's setvbuf is more restrictive about when it can be called than glibc's
    is.  The standards specify that setvbuf must occur before any other operations
    take place on the stream.
+9) Right now, %m is not handled properly by printf when the format uses positional
+   args.
 
 
 More to follow as I think of it...

+ 30 - 20
extra/locale/Makefile

@@ -26,13 +26,15 @@ codesets.txt:
 	fi;
 
 gen_wc8bit: gen_wc8bit.c
-	gcc -Os -Wall $(CFLAGS_wc8bit) gen_wc8bit.c -o gen_wc8bit
+	$(NATIVE_CC) $(NATIVE_CFLAGS) $(NATIVE_LDFLAGS) $(CFLAGS_wc8bit) $^ -o $@
+#gen_wc8bit.c -o gen_wc8bit
 
 c8tables.h: gen_wc8bit codesets.txt
 	./gen_wc8bit `cat codesets.txt`
 
 gen_wctype: gen_wctype.c
-	gcc -Os -Wall gen_wctype.c -o gen_wctype
+	$(NATIVE_CC) $(NATIVE_CFLAGS) $(NATIVE_LDFLAGS) $^ -o $@
+#gen_wctype.c -o gen_wctype
 
 # Warning! Beware tr_TR toupper/tolower exceptions!
 # Warning! Ignore the width table.  It will go away.
@@ -40,7 +42,8 @@ wctables.h: gen_wctype
 	./gen_wctype en_US
 
 gen_locale: gen_locale.c c8tables.h
-	gcc -Os -Wall gen_locale.c -o gen_locale
+	$(NATIVE_CC) $(NATIVE_CFLAGS) $(NATIVE_LDFLAGS) gen_locale.c -o $@
+#gen_locale.c -o gen_locale
 
 # TODO: if no wide char support, we should auto-disable all UTF-8 locales
 locale_tables.h: gen_locale locales.txt
@@ -64,30 +67,37 @@ locales.txt:
 	    false; \
 	fi;
 
-ifneq ($(TARGET_ARCH),$(NATIVE_ARCH))
+# ifneq ($(TARGET_ARCH),$(NATIVE_ARCH))
 
-locale.mmap: gen_mmap
-	echo Your locale.mmap file is either missing or out of date.
-	echo The developmental code can only generate one for the
-	echo target arch == native arch case.  Sorry.
-else
+# locale.mmap: gen_mmap
+# 	echo Your locale.mmap file is either missing or out of date.
+# 	echo The developmental code can only generate one for the
+# 	echo target arch == native arch case.  Sorry.
+# else
 
-gen_mmap: gen_mmap.c c8tables.h wctables.h locale_tables.h
-	gcc $(CFLAGS_mmap) -Os -Wall -static gen_mmap.c -o gen_mmap
+# gen_mmap: gen_mmap.c c8tables.h wctables.h locale_tables.h
+# 	gcc $(CFLAGS_mmap) -Os -Wall -static gen_mmap.c -o gen_mmap
 
-locale.mmap: gen_mmap
-	./gen_mmap
+# locale.mmap: gen_mmap
+# 	./gen_mmap
 
-endif
+# endif
+
+# lmmtolso: lmmtolso.c
+# 	gcc -Os -Wall lmmtolso.c -o lmmtolso
+
+# locale_data.c: lmmtolso locale.mmap
+# 	./lmmtolso
 
-lmmtolso: lmmtolso.c
-	gcc -Os -Wall lmmtolso.c -o lmmtolso
+gen_ldc: gen_ldc.c c8tables.h wctables.h locale_tables.h
+	$(NATIVE_CC) $(NATIVE_CFLAGS) $(NATIVE_LDFLAGS) $(CFLAGS_mmap) gen_ldc.c -o $@
+#gen_ldc.c -o gen_ldc
 
-locale_data.c: lmmtolso locale.mmap
-	./lmmtolso
+locale_data.c: gen_ldc
+	./gen_ldc
 
 locale_data.o: locale_data.c lt_defines.h
-	$(CC) -c locale_data.c
+	$(CC) $(CFLAGS_mmap) -c locale_data.c
 	$(STRIPTOOL) -x -R .note -R .comment locale_data.o
 
 uClibc_locale_data.h: c8tables.h wctables.h lt_defines.h locale_mmap.h
@@ -100,7 +110,7 @@ links-target: locale_data.o uClibc_locale_data.h
 
 clean:
 	rm -f *.[oa] *~ core
-	rm -f gen_wc8bit gen_wctype gen_locale
+	rm -f gen_wc8bit gen_wctype gen_locale gen_ldc
 	rm -f c8tables.h wctables.h locale_tables.h lt_defines.h
 	rm -f gen_mmap locale.mmap lmmtolso
 	rm -f locale_data.c locale_data.o  uClibc_locale_data.h

+ 1 - 1
extra/locale/README

@@ -44,7 +44,7 @@ Warning!!!   Warning!!!   Warning!!!   Warning!!!   Warning!!!   Warning!!!
 Warning!!!   Warning!!!   Warning!!!   Warning!!!   Warning!!!   Warning!!!
 
 
-1) In the toplevel dir,  'make headers uClibc_config'  .
+1) In the toplevel dir,  'make headers'.
 
 2) Create a codesets.txt file in this dir listing the codesets you want
    to support.  The easiest way to do this is to edit the output of

+ 460 - 0
extra/locale/gen_ldc.c

@@ -0,0 +1,460 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdint.h>
+#include <stddef.h>
+
+#ifndef __WCHAR_ENABLED
+#warning WHOA!!! __WCHAR_ENABLED is not defined! defining it now...
+#define __WCHAR_ENABLED
+#endif
+
+#define WANT_DATA
+#include "c8tables.h"
+#ifndef __CTYPE_HAS_8_BIT_LOCALES
+#warning __CTYPE_HAS_8_BIT_LOCALES is not defined...
+/* #define __CTYPE_HAS_8_BIT_LOCALES */
+#endif
+
+/*  #define Cctype_TBL_LEN 328 */
+/*  #define Cuplow_TBL_LEN 400 */
+/*  #define Cc2wc_TBL_LEN 1448 */
+/*  #define Cwc2c_TBL_LEN 3744 */
+
+#define WANT_WCctype_data
+#define WANT_WCuplow_data
+#define WANT_WCuplow_diff_data
+/* #define WANT_WCcomb_data */
+/*  #define WANT_WCwidth_data */
+#include "wctables.h"
+#undef WANT_WCctype_data
+#undef WANT_WCuplow_data
+#undef WANT_WCuplow_diff_data
+/* #undef WANT_WCcomb_data */
+/*  #undef WANT_WCwidth_data */
+
+ #define WCctype_TBL_LEN		(WCctype_II_LEN + WCctype_TI_LEN + WCctype_UT_LEN)
+ #define WCuplow_TBL_LEN		(WCuplow_II_LEN + WCuplow_TI_LEN + WCuplow_UT_LEN)
+ #define WCuplow_diff_TBL_LEN (2 * WCuplow_diffs)
+/*  #define WCcomb_TBL_LEN		(WCcomb_II_LEN + WCcomb_TI_LEN + WCcomb_UT_LEN) */
+
+#include "locale_tables.h"
+
+#include "locale_mmap.h"
+
+/*  #undef __PASTE2 */
+/*  #define __PASTE2(A,B)		A ## B */
+/*  #undef __PASTE3 */
+/*  #define __PASTE3(A,B,C)		A ## B ## C */
+
+
+/*  #define MAGIC_SIZE 64 */
+
+/*  #define COMMON_MMAP(X) \ */
+/*  	unsigned char	__PASTE3(lc_,X,_data)[__PASTE3(__lc_,X,_data_LEN)]; */
+
+/*  #define COMMON_MMIDX(X) \ */
+/*  	unsigned char	__PASTE3(lc_,X,_rows)[__PASTE3(__lc_,X,_rows_LEN)]; \ */
+/*  	uint16_t		__PASTE3(lc_,X,_item_offsets)[__PASTE3(__lc_,X,_item_offsets_LEN)]; \ */
+/*  	uint16_t		__PASTE3(lc_,X,_item_idx)[__PASTE3(__lc_,X,_item_idx_LEN)]; \ */
+
+/* ---------------------------------------------------------------------- */
+
+#define COMMON_OFFSETS(X) \
+	offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), \
+	offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), \
+	offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), \
+	offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)) \
+
+
+static const size_t common_tbl_offsets[CATEGORIES*4] = {
+	0, 0, 0, 0,					/* ctype */
+	COMMON_OFFSETS(numeric),
+	COMMON_OFFSETS(monetary),
+	COMMON_OFFSETS(time),
+	0, 0, 0, 0,					/* collate */
+	COMMON_OFFSETS(messages),
+};
+
+
+void out_uc(FILE *f, const unsigned char *p, size_t n, char *comment)
+{
+	size_t i;
+
+	fprintf(f, "{\t/* %s */", comment);
+	for (i = 0 ; i < n ; i++) {
+		if (!(i & 7)) {
+			fprintf(f, "\n\t");
+		}
+		if (p[i]) {
+			fprintf(f, "%#04x, ", p[i]);
+		} else {
+			fprintf(f, "%#4x, ", p[i]);
+		}
+	}
+	fprintf(f, "\n},\n");
+}
+
+void out_u16(FILE *f, const uint16_t *p, size_t n, char *comment)
+{
+	size_t i;
+
+	fprintf(f, "{\t/* %s */", comment);
+	for (i = 0 ; i < n ; i++) {
+		if (!(i & 7)) {
+			fprintf(f, "\n\t");
+		}
+		if (p[i]) {
+			fprintf(f, "%#06x, ", p[i]);
+		} else {
+			fprintf(f, "%#6x, ", p[i]);
+		}
+	}
+	fprintf(f, "\n},\n");
+}
+
+void out_i16(FILE *f, const int16_t *p, size_t n, char *comment)
+{
+	size_t i;
+
+	fprintf(f, "{\t/* %s */", comment);
+	for (i = 0 ; i < n ; i++) {
+		if (!(i & 7)) {
+			fprintf(f, "\n\t");
+		}
+		fprintf(f, "%6d, ", p[i]);
+	}
+	fprintf(f, "\n},\n");
+}
+
+void out_size_t(FILE *f, const size_t *p, size_t n, char *comment)
+{
+	size_t i;
+
+	fprintf(f, "{\t/* %s */", comment);
+	for (i = 0 ; i < n ; i++) {
+		if (!(i & 3)) {
+			fprintf(f, "\n\t");
+		}
+		if (p[i]) {
+			fprintf(f, "%#010zx, ", p[i]);
+		} else {
+			fprintf(f, "%#10zx, ", p[i]);
+		}
+	}
+	fprintf(f, "\n},\n");
+}
+
+
+int main(void)
+{
+	FILE *lso;					/* static object */
+	int i;
+	unsigned char magic[MAGIC_SIZE];
+
+	memset(magic, 0, MAGIC_SIZE);
+
+	if (!(lso = fopen("locale_data.c", "w"))) {
+		printf("can't open locale_data.c!\n");
+		return EXIT_FAILURE;
+	}
+
+	fprintf(lso,
+			"#include <stddef.h>\n"
+			"#include <stdint.h>\n"
+/* 			"#define __CTYPE_HAS_8_BIT_LOCALES\n" */
+			"#ifndef __WCHAR_ENABLED\n"
+			"#error __WCHAR_ENABLED not defined\n"
+			"#endif\n"
+			"#include \"c8tables.h\"\n"
+			"#include \"wctables.h\"\n"
+			"#include \"lt_defines.h\"\n"
+			"#include \"locale_mmap.h\"\n\n"
+			"static const __locale_mmap_t locale_mmap = {\n\n"
+			);
+	out_uc(lso, magic, MAGIC_SIZE, "magic");
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+	out_uc(lso, Cctype_data, Cctype_TBL_LEN, "tbl8ctype");
+	out_uc(lso, Cuplow_data, Cuplow_TBL_LEN, "tbl8uplow");
+#ifdef __WCHAR_ENABLED
+	out_u16(lso, Cc2wc_data, Cc2wc_TBL_LEN, "tbl8c2wc");
+	out_uc(lso, Cwc2c_data, Cwc2c_TBL_LEN, "tbl8wc2c");
+	/* translit  */
+#endif /* __WCHAR_ENABLED */
+#endif /* __CTYPE_HAS_8_BIT_LOCALES */
+#ifdef __WCHAR_ENABLED
+	out_uc(lso, WCctype_data, WCctype_TBL_LEN, "tblwctype");
+	out_uc(lso, WCuplow_data, WCuplow_TBL_LEN, "tblwuplow");
+	out_i16(lso, WCuplow_diff_data, WCuplow_diff_TBL_LEN, "tblwuplow_diff");
+/* 	const unsigned char tblwcomb[WCcomb_TBL_LEN]; */
+	/* width?? */
+#endif /* __WCHAR_ENABLED */
+	out_uc(lso, __lc_numeric_data, __lc_numeric_data_LEN, "lc_numeric_data");
+	out_uc(lso, __lc_monetary_data, __lc_monetary_data_LEN, "lc_monetary_data");
+	out_uc(lso, __lc_time_data, __lc_time_data_LEN, "lc_time_data");
+	/* TODO -- collate*/
+	out_uc(lso, __lc_messages_data, __lc_messages_data_LEN, "lc_messages_data");
+
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+	fprintf(lso, "{ /* codeset_8_bit array */\n");
+	for (i = 0 ; i < NUM_CODESETS ; i++) {
+		fprintf(lso, "{ /* codeset_8_bit[%d] */\n", i);
+		out_uc(lso, codeset_8_bit[i].idx8ctype, Cctype_IDX_LEN, "idx8ctype");
+		out_uc(lso, codeset_8_bit[i].idx8uplow, Cuplow_IDX_LEN, "idx8uplow");
+		out_uc(lso, codeset_8_bit[i].idx8c2wc, Cc2wc_IDX_LEN, "idx8c2wc");
+		out_uc(lso, codeset_8_bit[i].idx8wc2c, Cwc2c_II_LEN, "idx8wc2c");
+		fprintf(lso, "},\n");
+	}
+	fprintf(lso, "},\n");
+#endif /* __CTYPE_HAS_8_BIT_LOCALES */
+
+	out_uc(lso, __lc_numeric_rows, __lc_numeric_rows_LEN, "lc_numeric_rows");
+	out_u16(lso, __lc_numeric_item_offsets, __lc_numeric_item_offsets_LEN, "lc_numeric_item_offsets");
+	out_u16(lso, __lc_numeric_item_idx, __lc_numeric_item_idx_LEN, "lc_numeric_item_idx");
+
+	out_uc(lso, __lc_monetary_rows, __lc_monetary_rows_LEN, "lc_monetary_rows");
+	out_u16(lso, __lc_monetary_item_offsets, __lc_monetary_item_offsets_LEN, "lc_monetary_item_offsets");
+	out_u16(lso, __lc_monetary_item_idx, __lc_monetary_item_idx_LEN, "lc_monetary_item_idx");
+
+	out_uc(lso, __lc_time_rows, __lc_time_rows_LEN, "lc_time_rows");
+	out_u16(lso, __lc_time_item_offsets, __lc_time_item_offsets_LEN, "lc_time_item_offsets");
+	out_u16(lso, __lc_time_item_idx, __lc_time_item_idx_LEN, "lc_time_item_idx");
+
+	/* TODO -- collate*/
+
+	out_uc(lso, __lc_messages_rows, __lc_messages_rows_LEN, "lc_messages_rows");
+	out_u16(lso, __lc_messages_item_offsets, __lc_messages_item_offsets_LEN, "lc_messages_item_offsets");
+	out_u16(lso, __lc_messages_item_idx, __lc_messages_item_idx_LEN, "lc_messages_item_idx");
+
+	{
+		unsigned char co_buf[CATEGORIES] = {
+			1, __lc_numeric_item_offsets_LEN, __lc_monetary_item_offsets_LEN,
+			__lc_time_item_offsets_LEN, 0, __lc_messages_item_offsets_LEN
+		};
+		out_uc(lso, co_buf, CATEGORIES, "lc_common_item_offsets_LEN");
+	}
+	
+	out_size_t(lso, common_tbl_offsets, CATEGORIES * 4, "lc_common_tbl_offsets");
+	/* offsets from start of locale_mmap_t */
+	/* rows, item_offsets, item_idx, data */
+
+#ifdef NUM_LOCALES
+	out_uc(lso, __locales, NUM_LOCALES * WIDTH_LOCALES, "locales");
+	out_uc(lso, __locale_names5, 5 * NUM_LOCALE_NAMES, "locale_names5");
+#ifdef LOCALE_AT_MODIFIERS_LENGTH
+	out_uc(lso, __locale_at_modifiers, LOCALE_AT_MODIFIERS_LENGTH, "locale_at_modifiers");
+#else
+#error LOCALE_AT_MODIFIERS_LENGTH not defined!
+#endif /* LOCALE_AT_MODIFIERS_LENGTH */
+#endif /* NUM_LOCALES */
+
+	out_uc(lso, lc_names, lc_names_LEN, "lc_names");
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+	out_uc(lso, (const unsigned char*) CODESET_LIST, sizeof(CODESET_LIST), "codeset_list");
+#endif /* __CTYPE_HAS_8_BIT_LOCALES */
+
+	fprintf(lso,
+			"\n};\n\n"
+			"const __locale_mmap_t *__locale_mmap = &locale_mmap;\n\n"
+			);
+
+	if (ferror(lso) || fclose(lso)) {
+		printf("error writing!\n");
+		return EXIT_FAILURE;
+	}
+
+	return EXIT_SUCCESS;
+}
+
+
+
+/* ---------------------------------------------------------------------- */
+
+#if 0
+#define WRITE_COMMON_MMAP(X) \
+	fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)), SEEK_SET); \
+	for (i=0 ; i < __PASTE3(__lc_,X,_data_LEN) ; i++) { \
+		putc(__PASTE3(__lc_,X,_data)[i], fp); \
+	}
+
+#define WRITE_COMMON_MMIDX(X) \
+	fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), SEEK_SET); \
+	for (i=0 ; i < __PASTE3(__lc_,X,_rows_LEN) ; i++) { \
+		putc(__PASTE3(__lc_,X,_rows)[i], fp); \
+	} \
+	fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), SEEK_SET); \
+	for (i=0 ; i < __PASTE3(__lc_,X,_item_offsets_LEN) ; i++) { \
+		putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_offsets)[i]))[0], fp); \
+		putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_offsets)[i]))[1], fp); \
+	} \
+	fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), SEEK_SET); \
+	for (i=0 ; i < __PASTE3(__lc_,X,_item_idx_LEN) ; i++) { \
+		putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_idx)[i]))[0], fp); \
+		putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_idx)[i]))[1], fp); \
+	}
+
+#define WRITE_WC_DATA(X) \
+	fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tblw,X)), SEEK_SET); \
+	for (i=0 ; i < __PASTE3(WC,X,_TBL_LEN) ; i++) { \
+		putc(__PASTE3(WC,X,_data)[i], fp); \
+	}
+
+#define WRITE_WC_I16_DATA(X) \
+	fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tblw,X)), SEEK_SET); \
+	for (i=0 ; i < __PASTE3(WC,X,_TBL_LEN) ; i++) { \
+		putc( ((unsigned char *) &(__PASTE3(WC,X,_data)[i]))[0], fp); \
+		putc( ((unsigned char *) &(__PASTE3(WC,X,_data)[i]))[1], fp); \
+	}
+
+#define WRITE_C_DATA(X) \
+	fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tbl8,X)), SEEK_SET); \
+	for (i=0 ; i < __PASTE3(C,X,_TBL_LEN) ; i++) { \
+		putc(__PASTE3(C,X,_data)[i], fp); \
+	}
+
+#define WRITE_C_U16_DATA(X) \
+	fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tbl8,X)), SEEK_SET); \
+	for (i=0 ; i < __PASTE3(C,X,_TBL_LEN) ; i++) { \
+		putc( ((unsigned char *) &(__PASTE3(C,X,_data)[i]))[0], fp); \
+		putc( ((unsigned char *) &(__PASTE3(C,X,_data)[i]))[1], fp); \
+	}
+
+/**********************************************************************/
+
+#define COMMON_OFFSETS(X) \
+	offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), \
+	offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), \
+	offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), \
+	offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)) \
+
+
+static const size_t common_tbl_offsets[CATEGORIES*4] = {
+	0, 0, 0, 0,					/* ctype */
+	COMMON_OFFSETS(numeric),
+	COMMON_OFFSETS(monetary),
+	COMMON_OFFSETS(time),
+	0, 0, 0, 0,					/* collate */
+	COMMON_OFFSETS(messages),
+};
+
+
+
+
+
+int main(void)
+{
+	FILE *fp;
+	size_t i;
+	unsigned char *p;
+
+	if (!(fp = fopen("locale.mmap", "w"))) {
+		printf("error - can't open locale.mmap for writing!");
+		return EXIT_FAILURE;
+	}
+
+	for (i=0 ; i < sizeof(__locale_mmap_t) ; i++) {
+		putc(0, fp);			/* Zero out the file. */
+	}
+
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+	WRITE_C_DATA(ctype);
+	WRITE_C_DATA(uplow);
+#ifdef __WCHAR_ENABLED
+	WRITE_C_U16_DATA(c2wc);
+	WRITE_C_DATA(wc2c);
+	/* translit  */
+#endif /* __WCHAR_ENABLED */
+#endif /* __CTYPE_HAS_8_BIT_LOCALES */
+
+#ifdef __WCHAR_ENABLED
+	WRITE_WC_DATA(ctype);
+	WRITE_WC_DATA(uplow);
+	WRITE_WC_I16_DATA(uplow_diff);
+/* 	WRITE_WC_DATA(comb); */
+	/* width?? */
+#endif /* __WCHAR_ENABLED */
+
+	WRITE_COMMON_MMAP(numeric);
+	WRITE_COMMON_MMAP(monetary);
+	WRITE_COMMON_MMAP(time);
+	/* TODO -- collate*/
+	WRITE_COMMON_MMAP(messages);
+
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+	fseek(fp, (long) offsetof(__locale_mmap_t, codeset_8_bit), SEEK_SET); \
+	p = (unsigned char *) codeset_8_bit;
+	for (i=0 ; i < sizeof(codeset_8_bit) ; i++) {
+		putc(p[i], fp);
+	}
+#endif /* __CTYPE_HAS_8_BIT_LOCALES */
+
+	WRITE_COMMON_MMIDX(numeric);
+	WRITE_COMMON_MMIDX(monetary);
+	WRITE_COMMON_MMIDX(time);
+	/* TODO -- collate*/
+	WRITE_COMMON_MMIDX(messages);
+
+	fseek(fp, (long) offsetof(__locale_mmap_t, lc_common_item_offsets_LEN), SEEK_SET);
+	putc(1, fp);				/* ctype -- (codeset) handled specially */
+	putc(__lc_numeric_item_offsets_LEN, fp);
+	putc(__lc_monetary_item_offsets_LEN, fp);
+	putc(__lc_time_item_offsets_LEN, fp);
+	putc(0, fp);				/* collate */
+	putc(__lc_messages_item_offsets_LEN, fp);
+
+	fseek(fp, (long) offsetof(__locale_mmap_t, lc_common_tbl_offsets), SEEK_SET);
+	for (i=0 ; i < sizeof(common_tbl_offsets) ; i++) {
+		putc(((unsigned char *)common_tbl_offsets)[i], fp);
+	}
+
+#ifdef NUM_LOCALES
+	fseek(fp, (long) offsetof(__locale_mmap_t, locales), SEEK_SET);
+	for (i=0 ; i < (NUM_LOCALES * WIDTH_LOCALES) ; i++) {
+		putc(__locales[i], fp);
+	}
+
+	fseek(fp, (long) offsetof(__locale_mmap_t, locale_names5), SEEK_SET);
+	for (i=0 ; i < 5 * NUM_LOCALE_NAMES ; i++) {
+		putc(__locale_names5[i], fp);
+	}
+
+#ifdef LOCALE_AT_MODIFIERS_LENGTH
+	fseek(fp, (long) offsetof(__locale_mmap_t, locale_at_modifiers), SEEK_SET);
+	for (i=0 ; i < LOCALE_AT_MODIFIERS_LENGTH ; i++) {
+		putc(__locale_at_modifiers[i], fp);
+	}
+#endif /* LOCALE_AT_MODIFIERS_LENGTH */
+#endif /* NUM_LOCALES */
+
+	fseek(fp, (long) offsetof(__locale_mmap_t, lc_names), SEEK_SET);
+	for (i=0 ; i < lc_names_LEN ; i++) {
+		putc(lc_names[i], fp);
+	}
+
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+	fseek(fp, (long) offsetof(__locale_mmap_t, codeset_list), SEEK_SET);
+	for (i=0 ; i < sizeof(CODESET_LIST) ; i++) {
+		putc((unsigned char)(CODESET_LIST[i]), fp);
+	}
+#endif /* __CTYPE_HAS_8_BIT_LOCALES */
+
+
+	if (ferror(fp) || (fclose(fp) == EOF)) {
+		printf("error - stream in error state or fclose failed!");
+		return EXIT_FAILURE;
+	}
+
+	printf("sizeof(__locale_mmap_t) = %zd\n", sizeof(__locale_mmap_t));
+
+	return EXIT_SUCCESS;
+}
+#endif
+
+/* TODO:
+ * collate data (8-bit weighted single char only)
+ * @ mappings!
+ * codeset list? yes, since we'll want to be able to inspect them...
+ * that means putting some header stuff in magic
+ * fix ctype LEN defines in gen_c8tables
+ */

+ 3 - 1
extra/locale/gen_locale.c

@@ -10,7 +10,6 @@
 #include <nl_types.h>
 #include <stdint.h>
 
-#define __CTYPE_HAS_8_BIT_LOCALES
 #include "c8tables.h"
 
 
@@ -101,6 +100,9 @@ static void do_locale_names(void)
 		fprintf(ofp, "static const unsigned char __locales[%d];\n", (3 + CATEGORIES));
 		fprintf(ofp, "static const unsigned char __locale_names5[5];\n");
 	} else {
+		if (default_utf8) {
+			fprintf(ofp, "#define __CTYPE_HAS_UTF_8_LOCALES\t\t\t1\n");
+		}
 		fprintf(ofp, "#define CATEGORIES\t\t\t%d\n", CATEGORIES);
 		fprintf(ofp, "#define WIDTH_LOCALES\t\t\t%d\n", 3+CATEGORIES);
 		fprintf(ofp, "#define NUM_LOCALES\t\t\t%d\n", num_locales);

+ 3 - 3
extra/locale/gen_mmap.c

@@ -16,13 +16,13 @@
 #define WANT_WCctype_data
 #define WANT_WCuplow_data
 #define WANT_WCuplow_diff_data
-#define WANT_WCcomb_data
+/* #define WANT_WCcomb_data */
 /*  #define WANT_WCwidth_data */
 #include "wctables.h"
 #undef WANT_WCctype_data
 #undef WANT_WCuplow_data
 #undef WANT_WCuplow_diff_data
-#undef WANT_WCcomb_data
+/* #undef WANT_WCcomb_data */
 /*  #undef WANT_WCwidth_data */
 
 /*  #define WCctype_TBL_LEN		(WCctype_II_LEN + WCctype_TI_LEN + WCctype_UT_LEN) */
@@ -149,7 +149,7 @@ int main(void)
 	WRITE_WC_DATA(ctype);
 	WRITE_WC_DATA(uplow);
 	WRITE_WC_I16_DATA(uplow_diff);
-	WRITE_WC_DATA(comb);
+/* 	WRITE_WC_DATA(comb); */
 	/* width?? */
 #endif /* __WCHAR_ENABLED */
 

+ 8 - 28
extra/locale/gen_wc8bit.c

@@ -7,6 +7,14 @@
 #include <wctype.h>
 #include <limits.h>
 
+#ifndef __UCLIBC__
+#ifndef _WCTYPE_H
+#define _WCTYPE_H
+#endif
+#include "../../libc/sysdeps/linux/common/bits/uClibc_ctype.h"
+#endif
+
+
 /*  #define CTYPE_PACKED */
 #define UPLOW_IDX_SHIFT		3
 /* best if 2 unpacked or 3 packed */
@@ -63,34 +71,6 @@ typedef struct {
 	unsigned char c2wc_idx[C2WC_IDX_LEN];
 } charset_data;
 
-/* Taking advantage of the C99 mutual-exclusion guarantees for the various
- * (w)ctype classes, including the descriptions of printing and control
- * (w)chars, we can place each in one of the following mutually-exlusive
- * subsets.  Since there are less than 16, we can store the data for
- * each (w)chars in a nibble. In contrast, glibc uses an unsigned int
- * per (w)char, with one bit flag for each is* type.  While this allows
- * a simple '&' operation to determine the type vs. a range test and a
- * little special handling for the "blank" and "xdigit" types in my
- * approach, it also uses 8 times the space for the tables on the typical
- * 32-bit archs we supported.*/
-enum {
-	__CTYPE_unclassified = 0,
-	__CTYPE_alpha_nonupper_nonlower,
-	__CTYPE_alpha_lower,
-	__CTYPE_alpha_upper_lower,
-	__CTYPE_alpha_upper,
-	__CTYPE_digit,
-	__CTYPE_punct,
-	__CTYPE_graph,
-	__CTYPE_print_space_nonblank,
-	__CTYPE_print_space_blank,
-	__CTYPE_space_nonblank_noncntrl,
-	__CTYPE_space_blank_noncntrl,
-	__CTYPE_cntrl_space_nonblank,
-	__CTYPE_cntrl_space_blank,
-	__CTYPE_cntrl_nonspace,
-};
-
 int main(int argc, char **argv)
 {
 	FILE *fp;

+ 56 - 20
extra/locale/gen_wctype.c

@@ -10,6 +10,12 @@
 #include <wchar.h>
 #include <ctype.h>
 
+#ifndef __UCLIBC__
+#ifndef _WCTYPE_H
+#define _WCTYPE_H
+#endif
+#include "../../libc/sysdeps/linux/common/bits/uClibc_ctype.h"
+#endif
 
 /*       0x9 : space  blank */
 /*       0xa : space */
@@ -61,6 +67,7 @@
 #define RANGE 0xffffUL			/* Restrict for 16-bit wchar_t... */
 #endif
 
+#if 0
 /* Classification codes. */
 
 static const char *typename[] = {
@@ -81,7 +88,9 @@ static const char *typename[] = {
 	"C_cntrl_nonspace",
 	"empty_slot"
 };
+#endif
 
+#if 0
 /* Taking advantage of the C99 mutual-exclusion guarantees for the various
  * (w)ctype classes, including the descriptions of printing and control
  * (w)chars, we can place each in one of the following mutually-exlusive
@@ -109,21 +118,8 @@ enum {
 	__CTYPE_cntrl_space_blank,
 	__CTYPE_cntrl_nonspace,
 };
+#endif
 
-/* Some macros that test for various (w)ctype classes when passed one of the
- * designator values enumerated above. */
-#define __CTYPE_isalnum(D)		((unsigned int)(D-1) <= (__CTYPE_digit-1))
-#define __CTYPE_isalpha(D)		((unsigned int)(D-1) <= (__CTYPE_alpha_upper-1))
-#define __CTYPE_isblank(D) \
-	((((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5) && (D & 1))
-#define __CTYPE_iscntrl(D)		(((unsigned int)(D - __CTYPE_cntrl_space_nonblank)) <= 2)
-#define __CTYPE_isdigit(D)		(D == __CTYPE_digit)
-#define __CTYPE_isgraph(D)		((unsigned int)(D-1) <= (__CTYPE_graph-1))
-#define __CTYPE_islower(D)		(((unsigned int)(D - __CTYPE_alpha_lower)) <= 1)
-#define __CTYPE_isprint(D)		((unsigned int)(D-1) <= (__CTYPE_print_space_blank-1))
-#define __CTYPE_ispunct(D)		(D == __CTYPE_punct)
-#define __CTYPE_isspace(D)		(((unsigned int)(D - __CTYPE_print_space_nonblank)) <= 5)
-#define __CTYPE_isupper(D)		(((unsigned int)(D - __CTYPE_alpha_upper_lower)) <= 1)
 #define __CTYPE_isxdigit(D,X) \
 	(__CTYPE_isdigit(D) || (((unsigned int)(((X)|0x20) - 'a')) <= 5))
 
@@ -232,6 +228,7 @@ int main(int argc, char **argv)
 	table_data ultable;
 	table_data combtable;
 	table_data widthtable;
+	long int last_comb = 0;
 
 	unsigned char wct[(RANGE/2)+1];	/* wctype table (nibble per wchar) */
 	unsigned char ult[RANGE+1];	/* upper/lower table */
@@ -241,8 +238,32 @@ int main(int argc, char **argv)
 	wctype_t is_comb, is_comb3;
 
 	long int typecount[16];
+	const char *typename[16];
+	static const char empty_slot[] = "empty_slot";
 	int built = 0;
 
+#define INIT_TYPENAME(X) typename[__CTYPE_##X] = "C_" #X
+
+	for (i=0 ; i < 16 ; i++) {
+		typename[i] = empty_slot;
+	}
+
+	INIT_TYPENAME(unclassified);
+	INIT_TYPENAME(alpha_nonupper_nonlower);
+	INIT_TYPENAME(alpha_lower);
+	INIT_TYPENAME(alpha_upper_lower);
+	INIT_TYPENAME(alpha_upper);
+	INIT_TYPENAME(digit);
+	INIT_TYPENAME(punct);
+	INIT_TYPENAME(graph);
+	INIT_TYPENAME(print_space_nonblank);
+	INIT_TYPENAME(print_space_blank);
+	INIT_TYPENAME(space_nonblank_noncntrl);
+	INIT_TYPENAME(space_blank_noncntrl);
+	INIT_TYPENAME(cntrl_space_nonblank);
+	INIT_TYPENAME(cntrl_space_blank);
+	INIT_TYPENAME(cntrl_nonspace);
+
 	setvbuf(stdout, NULL, _IONBF, 0);
 
 	while (--argc) {
@@ -393,14 +414,27 @@ int main(int argc, char **argv)
 /*  					} */
 				}
 #endif
-
+#if 0
+				if (iswctype(c,is_comb) || iswctype(c,is_comb3)) {
+					if (!last_comb) {
+						printf("%#8x - ", c);
+						last_comb = c;
+					} else if (last_comb + 1 < c) {
+						printf("%#8x\n%#8x - ", last_comb, c);
+						last_comb = c;
+					} else {
+						last_comb = c;
+					}
+				}
+#endif
 			}
 #endif
 
 			combt[c/4] |= ((((!!iswctype(c,is_comb)) << 1) | !!iswctype(c,is_comb3))
 						   << ((c & 3) << 1));
 /*  			comb3t[c/8] |= ((!!iswctype(c,is_comb3)) << (c & 7)); */
-			widtht[c/4] |= (wcwidth(c) << ((c & 3) << 1));
+
+/* 			widtht[c/4] |= (wcwidth(c) << ((c & 3) << 1)); */
 
 			if (c & 1) {	/* Use the high nibble for odd numbered wchars. */
 				d <<= 4;
@@ -483,6 +517,7 @@ int main(int argc, char **argv)
 		newopt(ult, RANGE+1, n, &ultable);
 
 
+#if 0
 		printf("optimizing comb table..\n");
 		smallest = SIZE_MAX;
 		combtable.ii = NULL;
@@ -503,8 +538,10 @@ int main(int argc, char **argv)
 		smallest = SIZE_MAX;
 		newopt(combt, sizeof(combt), n, &combtable);
 		combtable.ti_shift += 4; /* correct for 4 entries per */
+#endif
 
 
+#if 0
 		printf("optimizing width table..\n");
 		smallest = SIZE_MAX;
 		widthtable.ii = NULL;
@@ -525,7 +562,7 @@ int main(int argc, char **argv)
 		smallest = SIZE_MAX;
 		newopt(widtht, sizeof(widtht), n, &widthtable);
 		widthtable.ti_shift += 4; /* correct for 4 entries per */
-
+#endif
 
 #if 0
 		printf("optimizing comb3 table..\n");
@@ -553,7 +590,6 @@ int main(int argc, char **argv)
 		dump_table_data(&cttable);
 		dump_table_data(&ultable);
 		dump_table_data(&combtable);
-		dump_table_data(&widthtable);
 		}
 
 		printf("verifying for %s...\n", *argv);
@@ -715,8 +751,8 @@ int main(int argc, char **argv)
 		fprintf(fp, "#endif /* WANT_WCuplow_diff_data */\n\n");
 
 
-		output_table(fp, "comb", &combtable);
-		output_table(fp, "width", &widthtable);
+/* 		output_table(fp, "comb", &combtable); */
+/* 		output_table(fp, "width", &widthtable); */
 
 		fclose(fp);
 	}

+ 2 - 2
extra/locale/locale_mmap.h

@@ -8,7 +8,7 @@
 #define WCctype_TBL_LEN		(WCctype_II_LEN + WCctype_TI_LEN + WCctype_UT_LEN)
 #define WCuplow_TBL_LEN		(WCuplow_II_LEN + WCuplow_TI_LEN + WCuplow_UT_LEN)
 #define WCuplow_diff_TBL_LEN (2 * WCuplow_diffs)
-#define WCcomb_TBL_LEN		(WCcomb_II_LEN + WCcomb_TI_LEN + WCcomb_UT_LEN)
+/* #define WCcomb_TBL_LEN		(WCcomb_II_LEN + WCcomb_TI_LEN + WCcomb_UT_LEN) */
 #endif
 
 #undef __PASTE2
@@ -41,7 +41,7 @@ typedef struct {
 	const unsigned char tblwctype[WCctype_TBL_LEN];
 	const unsigned char tblwuplow[WCuplow_TBL_LEN];
 	const int16_t tblwuplow_diff[WCuplow_diff_TBL_LEN];
-	const unsigned char tblwcomb[WCcomb_TBL_LEN];
+/* 	const unsigned char tblwcomb[WCcomb_TBL_LEN]; */
 	/* width?? */
 #endif /* __WCHAR_ENABLED */
 

+ 54 - 0
include/iconv.h

@@ -0,0 +1,54 @@
+/* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifndef _ICONV_H
+#define _ICONV_H	1
+
+#include <features.h>
+#define __need_size_t
+#include <stddef.h>
+
+#ifndef __UCLIBC_HAS_LOCALE__
+#error Attempted to include iconv.h when uClibc built without locale support.
+#endif
+
+__BEGIN_DECLS
+
+/* Identifier for conversion method from one codeset to another.  */
+typedef void *iconv_t;
+
+
+/* Allocate descriptor for code conversion from codeset FROMCODE to
+   codeset TOCODE.  */
+extern iconv_t iconv_open (__const char *__tocode, __const char *__fromcode)
+     __THROW;
+
+/* Convert at most *INBYTESLEFT bytes from *INBUF according to the
+   code conversion algorithm specified by CD and place up to
+   *OUTBYTESLEFT bytes in buffer at *OUTBUF.  */
+extern size_t iconv (iconv_t __cd, char **__restrict __inbuf,
+		     size_t *__restrict __inbytesleft,
+		     char **__restrict __outbuf,
+		     size_t *__restrict __outbytesleft);
+
+/* Free resources allocated for descriptor CD for code conversion.  */
+extern int iconv_close (iconv_t __cd) __THROW;
+
+__END_DECLS
+
+#endif /* iconv.h */

+ 1 - 1
ldso/util/Makefile

@@ -74,5 +74,5 @@ ldd.target: ldd.c
 	$(STRIPTOOL) -x -R .note -R .comment $@
 
 clean:
-	rm -f $(TARGETS) *.o *~ core ./elf.h
+	rm -f $(TARGETS) *.o *~ core ./elf.h *.target
 

+ 2 - 2
libc/misc/locale/locale.c

@@ -454,8 +454,8 @@ void _locale_init(void)
 		= (const unsigned char *) &__locale_mmap->tblwuplow;
 	__global_locale.tblwuplow_diff
 		= (const uint16_t *) &__locale_mmap->tblwuplow_diff;
-	__global_locale.tblwcomb
-		= (const unsigned char *) &__locale_mmap->tblwcomb;
+/* 	__global_locale.tblwcomb */
+/* 		= (const unsigned char *) &__locale_mmap->tblwcomb; */
 	/* width?? */
 #endif /* __WCHAR_ENABLED */
 

+ 7 - 0
libc/misc/time/time.c

@@ -90,6 +90,9 @@
  * Defining __TIME_TZ_OPT_SPEED will cause a tzset() to keep a copy of the
  * last TZ setting string and do a "fast out" if the current string is the
  * same.
+ *
+ * Nov 21, 2002   Fix an error return case in _time_mktime.
+ * 
  */
 
 
@@ -2022,6 +2025,10 @@ time_t _time_mktime(struct tm *timeptr, int store_on_success)
 
 	localtime_r(&t, (struct tm *)p);
 
+	if (t < 0) {
+	    return -1;
+	}
+
 	if (store_on_success) {
 		memcpy(timeptr, p, sizeof(struct tm));
 	}

+ 10 - 5
libc/misc/wchar/Makefile

@@ -23,12 +23,17 @@
 
 TOPDIR=../../../
 include $(TOPDIR)Rules.mak
+TARGET_CC = $(TOPDIR)extra/gcc-uClibc/$(TARGET_ARCH)-uclibc-gcc
 
 MSRC1=  wchar.c
 MOBJ1=  btowc.o wctob.o mbsinit.o mbrlen.o mbrtowc.o wcrtomb.o mbsrtowcs.o \
 	wcsrtombs.o _wchar_utf8sntowcs.o _wchar_wcsntoutf8s.o \
 	__mbsnrtowcs.o __wcsnrtombs.o wcwidth.o wcswidth.o
 
+ifeq ($(UCLIBC_HAS_LOCALE),y)
+	MOBJ1 += iconv.o
+endif
+
 MSRC2=  wstdio.c
 MOBJ2=  fwide.o \
 	fgetwc.o getwchar.o fgetws.o \
@@ -37,10 +42,6 @@ MOBJ2=  fwide.o \
 # getwc (fgetwc alias) getwc_unlocked (fgetwc_unlocked alias)
 # putwc (fputwc alias) putwc_unlocked (fputwc_unlocked alias)
 
-# wcwidth wcswidth
-# wcstod wcstof wcstold
-# wcstol wcstoul wcstoq wcstouq wcstoll wcstoull
-# fwprintf wprintf swprintf vfwprintf vwprintf vswprintf
 # fwscanf  wscanf  swscanf  vfwscanf  vwscanf  vswscanf
 # wcsftime
 
@@ -61,6 +62,10 @@ $(MOBJ2): $(MSRC2)
 	$(CC) $(CFLAGS) -DL_$* $< -c -o $*.o
 	$(STRIPTOOL) -x -R .note -R .comment $*.o
 
+iconv.target: wchar.c
+	$(TARGET_CC) $(CFLAGS) -DL_iconv_main wchar.c -o $@
+	$(STRIPTOOL) -x -R .note -R .comment $@
+
 clean:
-	rm -f *.[oa] *~ core
+	rm -f *.[oa] *~ core iconv.target
 

+ 624 - 34
libc/misc/wchar/wchar.c

@@ -1,3 +1,4 @@
+
 /*  Copyright (C) 2002     Manuel Novoa III
  *
  *  This library is free software; you can redistribute it and/or
@@ -79,6 +80,12 @@
  *   framework.  Minimally tested at the moment, but the stub C-locale
  *   version (which most people would probably be using) should be fine.
  *
+ * Nov 21, 2002
+ *
+ * Revert the wc<->mb changes from earlier this month involving the C-locale.
+ * Add a couple of ugly hacks to support *wprintf.
+ * Add a mini iconv() and iconv implementation (requires locale support).
+ *
  * Manuel
  */
 
@@ -97,13 +104,17 @@
 
 #ifdef __UCLIBC_HAS_LOCALE__
 #define ENCODING (__global_locale.encoding)
-#ifdef __UCLIBC_MJN3_ONLY__
-#warning implement __CTYPE_HAS_UTF_8_LOCALES!
+#ifndef __CTYPE_HAS_UTF_8_LOCALES
+#warning __CTYPE_HAS_UTF_8_LOCALES not set!
 #endif
-#define __CTYPE_HAS_UTF_8_LOCALES
 #else
-#undef __CTYPE_HAS_8_BIT_LOCALES
-#undef __CTYPE_HAS_UTF_8_LOCALES
+#define ENCODING (__ctype_encoding_7_bit)
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+#error __CTYPE_HAS_8_BIT_LOCALES is defined!
+#endif
+#ifdef __CTYPE_HAS_UTF_8_LOCALES
+#error __CTYPE_HAS_UTF_8_LOCALES is defined!
+#endif
 #undef L__wchar_utf8sntowcs
 #undef L__wchar_wcsntoutf8s
 #endif
@@ -114,7 +125,7 @@
 #define UTF_8_MAX_LEN 3
 #endif
 
-/*  #define KUHN */
+#define KUHN 1
 
 /* Implementation-specific work functions. */
 
@@ -157,17 +168,14 @@ wint_t btowc(int c)
 
 #else  /*  __CTYPE_HAS_8_BIT_LOCALES */
 
-#ifdef __CTYPE_HAS_UTF_8_LOCALES
-	if (ENCODING == __ctype_encoding_utf8) {
-		return (((unsigned int)c) < 0x80) ? c : WEOF;
-	}
-#endif /* __CTYPE_HAS_UTF_8_LOCALES */
-
 #ifdef __UCLIBC_HAS_LOCALE__
-	assert(ENCODING == __ctype_encoding_7_bit);
-#endif
+	assert((ENCODING == __ctype_encoding_7_bit)
+		   || (ENCODING == __ctype_encoding_utf8));
+#endif /* __UCLIBC_HAS_LOCALE__ */
 
-	return (((unsigned int)c) <= UCHAR_MAX) ? c : WEOF;
+	/* If we don't have 8-bit locale support, then this is trivial since
+	 * anything outside of 0-0x7f is illegal in C/POSIX and UTF-8 locales. */
+	return (((unsigned int)c) < 0x80) ? c : WEOF;
 
 #endif /*  __CTYPE_HAS_8_BIT_LOCALES */
 }
@@ -188,17 +196,18 @@ int wctob(wint_t c)
 
 #else  /*  __CTYPE_HAS_8_BIT_LOCALES */
 
-#ifdef __CTYPE_HAS_UTF_8_LOCALES
-	if (ENCODING == __ctype_encoding_utf8) {
-		return ((c >= 0) && (c < 0x80)) ? c : EOF;
-	}
-#endif /* __CTYPE_HAS_UTF_8_LOCALES */
-
 #ifdef __UCLIBC_HAS_LOCALE__
-	assert(ENCODING == __ctype_encoding_7_bit);
-#endif
+	assert((ENCODING == __ctype_encoding_7_bit)
+		   || (ENCODING == __ctype_encoding_utf8));
+#endif /* __UCLIBC_HAS_LOCALE__ */
+
+	/* If we don't have 8-bit locale support, then this is trivial since
+	 * anything outside of 0-0x7f is illegal in C/POSIX and UTF-8 locales. */
+	
+	/* TODO: need unsigned version of wint_t... */
+/*  	return (((unsigned int)c) < 0x80) ? c : WEOF; */
+	return ((c >= 0) && (c < 0x80)) ? c : EOF;
 
-	return ((c >= 0) && (c <= UCHAR_MAX)) ? c : EOF;
 #endif /*  __CTYPE_HAS_8_BIT_LOCALES */
 }
 
@@ -359,9 +368,19 @@ size_t _wchar_utf8sntowcs(wchar_t *__restrict pwc, size_t wn,
 	assert(ps != NULL);
 
 	incr = 1;
-	if (!pwc) {
+	/* NOTE: The following is an AWFUL HACK!  In order to support %s in
+	 * wprintf, we need to be able to compute the number of wchars needed
+	 * for the mbs conversion, not to exceed the precision specified.
+	 * But if dst is NULL, the return value is the length assuming a
+	 * sufficiently sized buffer.  So, we allow passing of (wchar_t *) ps
+	 * as pwc in order to flag that we really want the length, subject
+	 * to the restricted buffer size and no partial conversions.
+	 * See mbsnrtowcs() as well. */
+	if (!pwc || (pwc == ((wchar_t *)ps))) {
+		if (!pwc) {
+			wn = SIZE_MAX;
+		}
 		pwc = wcbuf;
-		wn = SIZE_MAX;
 		incr = 0;
 	}
 
@@ -406,7 +425,7 @@ size_t _wchar_utf8sntowcs(wchar_t *__restrict pwc, size_t wn,
 			}
 		BAD:
 #ifdef DECODER
-			wc = 0xfffd;
+			wc = 0xfffdU;
 			goto COMPLETE;
 #else
 			ps->mask = mask;
@@ -635,9 +654,19 @@ size_t __mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
 	}
 #endif
 	incr = 1;
-	if (!dst) {
+	/* NOTE: The following is an AWFUL HACK!  In order to support %s in
+	 * wprintf, we need to be able to compute the number of wchars needed
+	 * for the mbs conversion, not to exceed the precision specified.
+	 * But if dst is NULL, the return value is the length assuming a
+	 * sufficiently sized buffer.  So, we allow passing of ((wchar_t *)ps)
+	 * as dst in order to flag that we really want the length, subject
+	 * to the restricted buffer size and no partial conversions.
+	 * See _wchar_utf8sntowcs() as well. */
+	if (!dst || (dst == ((wchar_t *)ps))) {
+		if (!dst) {
+			len = SIZE_MAX;
+		}
 		dst = wcbuf;
-		len = SIZE_MAX;
 		incr = 0;
 	}
 
@@ -659,8 +688,7 @@ size_t __mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
 						  (__global_locale.idx8c2wc[wc >> Cc2wc_IDX_SHIFT]
 						   << Cc2wc_IDX_SHIFT) + (wc & (Cc2wc_ROW_LEN - 1))];
 				if (!wc) {
-					__set_errno(EILSEQ);
-					return (size_t) -1;
+					goto BAD;
 				}
 			}
 			if (!(*dst = wc)) {
@@ -687,6 +715,13 @@ size_t __mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
 			s = NULL;
 			break;
 		}
+		if (*dst >= 0x80) {
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+		BAD:
+#endif
+			__set_errno(EILSEQ);
+			return (size_t) -1;
+		}
 		++s;
 		dst += incr;
 		--count;
@@ -772,7 +807,7 @@ size_t __wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
 									+ (wc & ((1 << Cwc2c_TT_SHIFT)-1))];
 				}
 
-/*  #define __WCHAR_REPLACEMENT_CHAR '?' */
+#define __WCHAR_REPLACEMENT_CHAR '?'
 #ifdef __WCHAR_REPLACEMENT_CHAR
 				*dst = (unsigned char) ( u ? u : __WCHAR_REPLACEMENT_CHAR );
 #else  /* __WCHAR_REPLACEMENT_CHAR */
@@ -798,13 +833,12 @@ size_t __wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
 #endif
 
 	while (count) {
-		if (*s > UCHAR_MAX) {
+		if (*s >= 0x80) {
 #if defined(__CTYPE_HAS_8_BIT_LOCALES) && !defined(__WCHAR_REPLACEMENT_CHAR)
 		BAD:
 #endif
 			__set_errno(EILSEQ);
 			return (size_t) -1;
-
 		}
 		if ((*dst = (unsigned char) *s) == 0) {
 			s = NULL;
@@ -1068,3 +1102,559 @@ int wcwidth(wchar_t wc)
 
 #endif
 /**********************************************************************/
+
+
+typedef struct {
+	mbstate_t tostate;
+	mbstate_t fromstate;
+	int tocodeset;
+	int fromcodeset;
+	int frombom;
+	int tobom;
+	int fromcodeset0;
+	int frombom0;
+	int tobom0;
+	int skip_invalid_input;		/* To support iconv -c option. */
+} _UC_iconv_t;
+
+
+
+#ifdef L_iconv
+
+#include <iconv.h>
+#include <string.h>
+#include <endian.h>
+#include <byteswap.h>
+
+#if (__BYTE_ORDER != __BIG_ENDIAN) && (__BYTE_ORDER != __LITTLE_ENDIAN)
+#error unsupported endianness for iconv
+#endif
+
+#ifndef __CTYPE_HAS_8_BIT_LOCALES
+#error currently iconv requires 8 bit locales
+#endif
+#ifndef __CTYPE_HAS_UTF_8_LOCALES
+#error currently iconv requires UTF-8 locales
+#endif
+
+
+enum {
+	IC_WCHAR_T = 0xe0,
+	IC_MULTIBYTE = 0xe0,
+#if __BYTE_ORDER == __BIG_ENDIAN
+	IC_UCS_4 =	0xec,
+	IC_UTF_32 = 0xe4,
+	IC_UCS_2 =	0xe2,
+	IC_UTF_16 = 0xea,
+#else
+	IC_UCS_4 =	0xed,
+	IC_UTF_32 = 0xe5,
+	IC_UCS_2 =	0xe3,
+	IC_UTF_16 = 0xeb,
+#endif
+	IC_UTF_8 = 2,
+	IC_ASCII = 1
+};
+
+/* For the multibyte
+ * bit 0 means swap endian
+ * bit 1 means 2 byte
+ * bit 2 means 4 byte
+ *
+ */
+
+const unsigned char codesets[] =
+	"\x0a\xe0""WCHAR_T\x00"		/* superset of UCS-4 but platform-endian */
+#if __BYTE_ORDER == __BIG_ENDIAN
+	"\x08\xec""UCS-4\x00"		/* always BE */
+	"\x0a\xec""UCS-4BE\x00"
+	"\x0a\xed""UCS-4LE\x00"
+	"\x09\fe4""UTF-32\x00"		/* platform endian with BOM */
+	"\x0b\xe4""UTF-32BE\x00"
+	"\x0b\xe5""UTF-32LE\x00"
+	"\x08\xe2""UCS-2\x00"		/* always BE */
+	"\x0a\xe2""UCS-2BE\x00"
+	"\x0a\xe3""UCS-2LE\x00"
+	"\x09\xea""UTF-16\x00"		/* platform endian with BOM */
+	"\x0b\xea""UTF-16BE\x00"
+	"\x0b\xeb""UTF-16LE\x00"
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+	"\x08\xed""UCS-4\x00"		/* always BE */
+	"\x0a\xed""UCS-4BE\x00"
+	"\x0a\xec""UCS-4LE\x00"
+	"\x09\xf4""UTF-32\x00"		/* platform endian with BOM */
+	"\x0b\xe5""UTF-32BE\x00"
+	"\x0b\xe4""UTF-32LE\x00"
+	"\x08\xe3""UCS-2\x00"		/* always BE */
+	"\x0a\xe3""UCS-2BE\x00"
+	"\x0a\xe2""UCS-2LE\x00"
+	"\x09\xfa""UTF-16\x00"		/* platform endian with BOM */
+	"\x0b\xeb""UTF-16BE\x00"
+	"\x0b\xea""UTF-16LE\x00"
+#endif
+	"\x08\x02""UTF-8\x00"
+	"\x0b\x01""US-ASCII\x00"
+	"\x07\x01""ASCII";			/* Must be last! (special case to save a nul) */
+
+static int find_codeset(const char *name)
+{
+	const unsigned char *s;
+	int codeset;
+
+	for (s = codesets ; *s ; s += *s) {
+		if (!strcasecmp(s+2, name)) {
+			return s[1];
+		}
+	}
+
+	/* The following is ripped from find_locale in locale.c. */
+
+	/* TODO: maybe CODESET_LIST + *s ??? */
+	/* 7bit is 1, UTF-8 is 2, 8-bit is >= 3 */
+	codeset = 2;
+	s = CODESET_LIST;
+	do {
+		++codeset;		/* Increment codeset first. */
+		if (!strcasecmp(CODESET_LIST+*s, name)) {
+			return codeset;
+		}
+	} while (*++s);
+
+	return 0;			/* No matching codeset! */
+}
+
+iconv_t iconv_open(const char *tocode, const char *fromcode)
+{
+	register _UC_iconv_t *px;
+	int tocodeset, fromcodeset;
+
+	if (((tocodeset = find_codeset(tocode)) != 0)
+		&& ((fromcodeset = find_codeset(fromcode)) != 0)) {
+		if ((px = malloc(sizeof(_UC_iconv_t))) != NULL) {
+			px->tocodeset = tocodeset;
+			px->tobom0 = px->tobom = (tocodeset & 0x10) >> 4;
+			px->fromcodeset0 = px->fromcodeset = fromcodeset;
+			px->frombom0 = px->frombom = (fromcodeset & 0x10) >> 4;
+			px->skip_invalid_input = px->tostate.mask = px->fromstate.mask = 0;
+			return (iconv_t) px;
+		}
+	} else {
+		__set_errno(EINVAL);
+	}
+	return (iconv_t)(-1);
+}
+
+int iconv_close(iconv_t cd)
+{
+	free(cd);
+
+	return 0;
+}
+
+size_t iconv(iconv_t cd, char **__restrict inbuf,
+			 size_t *__restrict inbytesleft,
+		     char **__restrict outbuf, size_t *__restrict outbytesleft)
+{
+	_UC_iconv_t *px = (_UC_iconv_t *) cd;
+	size_t nrcount, r;
+	wchar_t wc, wc2;
+	int inci, inco;
+
+	assert(px != (_UC_iconv_t *)(-1));
+	assert(sizeof(wchar_t) == 4);
+
+	if (!inbuf || !*inbuf) {	/* Need to reinitialze conversion state. */
+		/* Note: For shift-state encodings we possibly need to output the
+		 * shift sequence to return to initial state! */
+		if ((px->fromcodeset & 0xf0) == 0xe0) {
+		}
+		px->tostate.mask = px->fromstate.mask = 0;
+		px->fromcodeset = px->fromcodeset0;
+		px->tobom = px->tobom0;
+		px->frombom = px->frombom0;
+		return 0;
+	}
+
+	nrcount = 0;
+	while (*inbytesleft) {
+		if (!*outbytesleft) {
+		TOO_BIG:
+			__set_errno(E2BIG);
+			return (size_t) -1;
+		}
+
+		inci = inco = 1;
+		if (px->fromcodeset >= IC_MULTIBYTE) {
+			inci = (px->fromcodeset == IC_WCHAR_T) ? 4: (px->fromcodeset & 6);
+			if (*inbytesleft < inci) goto INVALID;
+			wc = (((unsigned int)((unsigned char)((*inbuf)[0]))) << 8)
+				+ ((unsigned char)((*inbuf)[1]));
+			if (inci == 4) {
+				wc = (((unsigned int)((unsigned char)((*inbuf)[2]))) << 8)
+					+ ((unsigned char)((*inbuf)[3])) + (wc << 16);
+				if (!(px->fromcodeset & 1)) wc = bswap_32(wc);
+			} else {
+				if (!(px->fromcodeset & 1)) wc = bswap_16(wc);
+				if (((px->fromcodeset & IC_UTF_16) == IC_UTF_16)
+					 && (((__uwchar_t)(wc - 0xd800U)) < (0xdc00U - 0xd800U))
+					) {			/* surrogate */
+					wc =- 0xd800U;
+					if (*inbytesleft < 4) goto INVALID;
+					wc2 = (((unsigned int)((unsigned char)((*inbuf)[2]))) << 8)
+						+ ((unsigned char)((*inbuf)[3]));
+					if (!(px->fromcodeset & 1)) wc = bswap_16(wc2);
+					if (((__uwchar_t)(wc2 -= 0xdc00U)) < (0xe0000U - 0xdc00U)) {
+						goto ILLEGAL;
+					}
+					inci = 4;	/* Change inci here in case skipping illegals. */
+					wc = 0x10000UL + (wc << 10) + wc2;
+				}
+			}
+
+			if (px->frombom) {
+				px->frombom = 0;
+				if ((wc == 0xfeffU)
+					|| (wc == ((inci == 4)
+							   ? (((wchar_t) 0xfffe0000UL))
+							   : ((wchar_t)(0xfffeUL))))
+					) {
+					if (wc != 0xfeffU) {
+						px->fromcodeset ^= 1; /* toggle endianness */
+						wc = 0xfeffU;
+					}
+					if (!px->frombom) {
+						goto BOM_SKIP_OUTPUT;
+					}
+					goto GOT_BOM;
+				}
+			}
+
+			if (px->fromcodeset != IC_WCHAR_T) {
+				if (((__uwchar_t) wc) > (((px->fromcodeset & IC_UCS_4) == IC_UCS_4)
+										 ? 0x7fffffffUL : 0x10ffffUL)
+#ifdef KUHN
+					|| (((__uwchar_t)(wc - 0xfffeU)) < 2)
+					|| (((__uwchar_t)(wc - 0xd800U)) < (0xe000U - 0xd800U))
+#endif
+					) {
+					goto ILLEGAL;
+				}
+			}
+		} else if (px->fromcodeset == IC_UTF_8) {
+			const char *p = *inbuf;
+			r = _wchar_utf8sntowcs(&wc, 1, &p, *inbytesleft, &px->fromstate, 0);
+			if (((ssize_t) r) <= 0) { /* either EILSEQ or incomplete or nul */
+				if (((ssize_t) r) < 0) { /* either EILSEQ or incomplete or nul */
+					assert((r == (size_t)(-1)) || (r == (size_t)(-2)));
+					if (r == (size_t)(-2)) {
+					INVALID:
+						__set_errno(EINVAL);
+					} else {
+						px->fromstate.mask = 0;
+						inci = 1;
+					ILLEGAL:
+						if (px->skip_invalid_input) {
+							px->skip_invalid_input = 2;	/* flag for iconv utility */
+							goto BOM_SKIP_OUTPUT;
+						}
+						__set_errno(EILSEQ);
+					}
+					return (size_t)(-1);
+				}
+#ifdef __UCLIBC_MJN3_ONLY__
+#warning optimize this
+#endif
+				if (p != NULL) { /* incomplet char case */
+					goto INVALID;
+				}
+				p = *inbuf + 1;	/* nul */
+			}
+			inci = p - *inbuf;
+		} else if ((wc = ((unsigned char)(**inbuf))) >= 0x80) {	/* Non-ASCII... */
+			if (px->fromcodeset == IC_ASCII) { /* US-ASCII codeset */
+				goto ILLEGAL;
+			} else {			/* some other 8-bit ascii-extension codeset */
+				const codeset_8_bit_t *c8b
+					= __locale_mmap->codeset_8_bit + px->fromcodeset - 3;
+				wc -= 0x80;
+				wc = __global_locale.tbl8c2wc[
+							 (c8b->idx8c2wc[wc >> Cc2wc_IDX_SHIFT]
+							  << Cc2wc_IDX_SHIFT) + (wc & (Cc2wc_ROW_LEN - 1))];
+				if (!wc) {
+					goto ILLEGAL;
+				}
+			}
+		} else {
+			fprintf(stderr, "ARG!! %d\n", px->fromcodeset);
+		}
+
+
+		if (px->tobom) {
+			inci = 0;
+			wc = 0xfeffU;
+	GOT_BOM:
+			px->tobom = 0;
+		}
+
+		if (px->tocodeset >= IC_MULTIBYTE) {
+			inco = (px->tocodeset == IC_WCHAR_T) ? 4: (px->tocodeset & 6);
+			if (*outbytesleft < inci) goto TOO_BIG;
+			if (px->tocodeset != IC_WCHAR_T) {
+				if (((__uwchar_t) wc) > (((px->tocodeset & IC_UCS_4) == IC_UCS_4)
+										 ? 0x7fffffffUL : 0x10ffffUL)
+#ifdef KUHN
+					|| (((__uwchar_t)(wc - 0xfffeU)) < 2)
+					|| (((__uwchar_t)(wc - 0xd800U)) < (0xe000U - 0xd800U))
+#endif
+					) {
+				REPLACE_32:
+					wc = 0xfffd;
+					++nrcount;
+				}
+			}
+			if (inco == 4) {
+				if (px->tocodeset & 1) wc = bswap_32(wc);
+			} else {
+				if (((__uwchar_t)wc ) > 0xffffU) {
+					if ((px->tocodeset & IC_UTF_16) != IC_UTF_16) {
+						goto REPLACE_32;
+					}
+					if (*outbytesleft < (inco = 4)) goto TOO_BIG;
+					wc2 = 0xdc00U + (wc & 0x3ff);
+					wc = 0xd800U + ((wc >> 10) & 0x3ff);
+					if (px->tocodeset & 1) {
+						wc = bswap_16(wc);
+						wc2 = bswap_16(wc2);
+					}
+					wc += (wc2 << 16);
+				} else if (px->tocodeset & 1) wc = bswap_16(wc);
+			}				
+			(*outbuf)[0] = (char)((unsigned char)(wc));
+			(*outbuf)[1] = (char)((unsigned char)(wc >> 8));
+			if (inco == 4) {
+				(*outbuf)[2] = (char)((unsigned char)(wc >> 16));
+				(*outbuf)[3] = (char)((unsigned char)(wc >> 24));
+			}
+		} else if (px->tocodeset == IC_UTF_8) {
+			const wchar_t *pw = &wc;
+			do {
+				r = _wchar_wcsntoutf8s(*outbuf, *outbytesleft, &pw, 1);
+				if (r != (size_t)(-1)) {
+#ifdef __UCLIBC_MJN3_ONLY__
+#warning what happens for a nul?
+#endif
+					if (r == 0) {
+						if (wc != 0) {
+							goto TOO_BIG;
+						}
+						++r;
+					}
+					break;
+				}
+				wc = 0xfffdU;
+				++nrcount;
+			} while (1);
+			inco = r;
+		} else if (((__uwchar_t)(wc)) < 0x80) {
+		CHAR_GOOD:
+				**outbuf = wc;
+		} else {
+			if ((px->tocodeset != 0x01) && (wc <= Cwc2c_DOMAIN_MAX)) {
+				const codeset_8_bit_t *c8b
+					= __locale_mmap->codeset_8_bit + px->tocodeset - 3;
+				__uwchar_t u;
+				u = c8b->idx8wc2c[wc >> (Cwc2c_TI_SHIFT + Cwc2c_TT_SHIFT)];
+				u = __global_locale.tbl8wc2c[(u << Cwc2c_TI_SHIFT)
+						 + ((wc >> Cwc2c_TT_SHIFT)
+							& ((1 << Cwc2c_TI_SHIFT)-1))];
+				wc = __global_locale.tbl8wc2c[Cwc2c_TI_LEN
+						 + (u << Cwc2c_TT_SHIFT)
+						 + (wc & ((1 << Cwc2c_TT_SHIFT)-1))];
+				if (wc) {
+					goto CHAR_GOOD;
+				}
+			}
+			**outbuf = '?';
+			++nrcount;
+		}
+
+		*outbuf += inco;
+		*outbytesleft -= inco;
+	BOM_SKIP_OUTPUT:
+		*inbuf += inci;
+		*inbytesleft -= inci;
+	}
+	return nrcount;
+}
+
+#endif
+/**********************************************************************/
+#ifdef L_iconv_main
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+#include <iconv.h>
+#include <stdarg.h>
+#include <libgen.h>
+
+extern const unsigned char codesets[];
+
+#define IBUF BUFSIZ
+#define OBUF BUFSIZ
+
+char *progname;
+int hide_errors;
+
+static void error_msg(const char *fmt, ...) 
+	 __attribute__ ((noreturn, format (printf, 1, 2)));
+
+static void error_msg(const char *fmt, ...) 
+{
+	va_list arg;
+
+	if (!hide_errors) {
+		fprintf(stderr, "%s: ", progname);
+		va_start(arg, fmt);
+		vfprintf(stderr, fmt, arg);
+		va_end(arg);
+	}
+
+	exit(EXIT_FAILURE);
+}
+
+int main(int argc, char **argv)
+{
+	FILE *ifile;
+	FILE *ofile = stdout;
+	const char *p;
+	const char *s;
+	static const char opt_chars[] = "tfocsl";
+	                              /* 012345 */
+	const char *opts[sizeof(opt_chars)]; /* last is infile name */
+	iconv_t ic;
+	char ibuf[IBUF];
+	char obuf[OBUF];
+	char *pi;
+	char *po;
+	size_t ni, no, r, pos;
+
+	hide_errors = 0;
+
+	for (s = opt_chars ; *s ; s++) {
+		opts[ s - opt_chars ] = NULL;
+	}
+
+	progname = *argv;
+	while (--argc) {
+		p = *++argv;
+		if ((*p != '-') || (*++p == 0)) {
+			break;
+		}
+		do {
+			if ((s = strchr(opt_chars,*p)) == NULL) {
+			USAGE:
+				s = basename(progname);
+				fprintf(stderr,
+						"%s [-cs] -f fromcode -t tocode [-o outputfile] [inputfile ...]\n"
+						"  or\n%s -l\n", s, s);
+				return EXIT_FAILURE;
+			}
+			if ((s - opt_chars) < 3) {
+				if ((--argc == 0) || opts[s - opt_chars]) {
+					goto USAGE;
+				}
+				opts[s - opt_chars] = *++argv;
+			} else {
+				opts[s - opt_chars] = p;
+			}
+		} while (*++p);
+	}
+
+	if (opts[5]) {				/* -l */
+		fprintf(stderr, "Recognized codesets:\n");
+		for (s = codesets ; *s ; s += *s) {
+			fprintf(stderr,"  %s\n", s+2);
+		}
+		s = CODESET_LIST;
+		do {
+			fprintf(stderr,"  %s\n", CODESET_LIST+ (unsigned char)(*s));
+		} while (*++s);
+
+		return EXIT_SUCCESS;
+	}
+
+	if (opts[4]) {
+		hide_errors = 1;
+	}
+
+	if (!opts[0] || !opts[1]) {
+		goto USAGE;
+	}
+	if ((ic = iconv_open(opts[0],opts[1])) == ((iconv_t)(-1))) {
+		error_msg( "unsupported codeset in %s -> %s conversion\n", opts[0], opts[1]);
+	}
+	if (opts[3]) {				/* -c */
+		((_UC_iconv_t *) ic)->skip_invalid_input = 1;
+	}
+
+	if ((s = opts[2]) != NULL) {
+		if (!(ofile = fopen(s, "w"))) {
+			error_msg( "couldn't open %s for writing\n", s);
+		}
+	}
+
+	pos = ni = 0;
+	do {
+		if (!argc || ((**argv == '-') && !((*argv)[1]))) {
+			ifile = stdin;		/* we don't check for duplicates */
+		} else if (!(ifile = fopen(*argv, "r"))) {
+			error_msg( "couldn't open %s for reading\n", *argv);
+		}
+
+		while ((r = fread(ibuf + ni, 1, IBUF - ni, ifile)) > 0) {
+			pos += r;
+			ni += r;
+			no = OBUF;
+			pi = ibuf;
+			po = obuf;
+			if ((r = iconv(ic, &pi, &ni, &po, &no)) == ((size_t)(-1))) {
+				if ((errno != EINVAL) && (errno != E2BIG)) {
+					error_msg( "iconv failed at pos %lu : %m\n", (unsigned long) (pos - ni));
+				}
+			}
+			if ((r = OBUF - no) > 0) {
+				if (fwrite(obuf, 1, OBUF - no, ofile) < r) {
+					error_msg( "write error\n");
+				}
+			}
+			if (ni) {			/* still bytes in buffer! */
+				memmove(ibuf, pi, ni);
+			}
+		}
+
+		if (ferror(ifile)) {
+			error_msg( "read error\n");
+		}
+
+		++argv;
+
+		if (ifile != stdin) {
+			fclose(ifile);
+		}
+
+	} while (--argc > 0);
+
+	iconv_close(ic);
+
+	if (ni) {
+		error_msg( "incomplete sequence\n");
+	}
+
+	return (((_UC_iconv_t *) ic)->skip_invalid_input < 2)
+		? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+#endif
+/**********************************************************************/

+ 19 - 2
libc/misc/wchar/wstdio.c

@@ -26,6 +26,13 @@
  *
  *  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION! */
 
+/* Nov 21, 2002
+ *
+ * Reimplement fputwc and fputws in terms of internal function _wstdio_fwrite.
+ */
+
+
+
 
 /*
  * ANSI/ISO C99 says
@@ -288,6 +295,9 @@ UNLOCKED(wchar_t *,fgetws,(wchar_t *__restrict ws, int n,
 
 UNLOCKED(wint_t,fputwc,(wchar_t wc, FILE *stream),(wc, stream))
 {
+#if 1
+	return _wstdio_fwrite(&wc, 1, stream) ? wc : WEOF;
+#else
 	size_t n;
 	char buf[MB_LEN_MAX];
 
@@ -298,9 +308,10 @@ UNLOCKED(wint_t,fputwc,(wchar_t wc, FILE *stream),(wc, stream))
 	}
 	stream->modeflags |= __FLAG_WIDE;
 
-	return (((n = wcrtomb(buf, wc, &stream->state)) != ((size_t)-1)) /* EILSEQ */
-			&& (_stdio_fwrite(buf, n, stream) != n))/* Didn't write everything. */
+	return (((n = wcrtomb(buf, wc, &stream->state)) != ((size_t)-1)) /* !EILSEQ */
+			&& (_stdio_fwrite(buf, n, stream) == n))/* and wrote everything. */
 		? wc : WEOF;
+#endif
 }
 
 strong_alias(fputwc_unlocked,putwc_unlocked);
@@ -324,6 +335,11 @@ UNLOCKED_STREAM(wint_t,putwchar,(wchar_t wc),(wc),stdout)
 UNLOCKED(int,fputws,(const wchar_t *__restrict ws,
 					 register FILE *__restrict stream),(ws, stream))
 {
+#if 1
+	size_t n = wcslen(ws);
+
+	return (_wstdio_fwrite(ws, n, stream) == n) ? 0 : -1;
+#else
 	size_t n;
 	char buf[64];
 
@@ -347,6 +363,7 @@ UNLOCKED(int,fputws,(const wchar_t *__restrict ws,
 	}
 
 	return 1;
+#endif
 }
 
 #endif

+ 8 - 2
libc/stdio/Makefile

@@ -51,13 +51,19 @@ MSRC2= printf.c
 MOBJ2=  vsnprintf.o vdprintf.o vasprintf.o vprintf.o vsprintf.o \
 	fprintf.o  snprintf.o  dprintf.o  asprintf.o  printf.o  sprintf.o
 
+ifeq ($(UCLIBC_HAS_WCHAR),y)
+	MOBJ += _wstdio_fwrite.o
+	MOBJ2 += fwprintf.o wprintf.o swprintf.o vwprintf.o vswprintf.o \
+		vfwprintf.o
+endif
+
 ifneq ($(USE_OLD_VFPRINTF),y)
 	MOBJ2 += _ppfs_init.o _ppfs_prepargs.o _ppfs_setargs.o \
-		 _ppfs_parsespec.o _do_one_spec.o vfprintf.o \
+		 _ppfs_parsespec.o vfprintf.o \
 		 _store_inttype.o _load_inttype.o \
 		 register_printf_function.o parse_printf_format.o
 endif
-
+# _do_one_spec.o 
 
 
 ifeq ($(UCLIBC_HAS_FLOATS),y)

+ 881 - 501
libc/stdio/printf.c

@@ -59,6 +59,10 @@
  * Add printf wchar support for %lc (%C) and %ls (%S).
  * Require printf format strings to be valid multibyte strings beginning and
  *   ending in their initial shift state, as per the stds.
+ *
+ * Nov 21, 2002
+ * Add *wprintf functions.  Currently they don't support floating point
+ *   conversions.  That will wait until the rewrite of _dtostr.
  */
 
 /* TODO:
@@ -177,7 +181,8 @@ enum {
 	FLAG_MINUS		=	0x08,	/* must be 2 * FLAG_ZERO */
 	FLAG_HASH		=	0x10,
 	FLAG_THOUSANDS	=	0x20,
-	FLAG_I18N		=	0x40	/* only works for d, i, u */
+	FLAG_I18N		=	0x40,	/* only works for d, i, u */
+	FLAG_WIDESTREAM =   0x80
 };	  
 
 /**********************************************************************/
@@ -359,15 +364,14 @@ typedef struct {
 extern size_t _dtostr(FILE * fp, long double x, struct printf_info *info);
 #endif
 
-#define _outnstr(stream, string, len)	_stdio_fwrite(string, len, stream)	/* TODO */
-
-extern int _do_one_spec(FILE * __restrict stream, ppfs_t *ppfs, int *count);
-
 extern int _ppfs_init(ppfs_t *ppfs, const char *fmt0); /* validates */
 extern void _ppfs_prepargs(ppfs_t *ppfs, va_list arg); /* sets posargptrs */
 extern void _ppfs_setargs(ppfs_t *ppfs); /* sets argptrs for current spec */
 extern int _ppfs_parsespec(ppfs_t *ppfs); /* parses specifier */
 
+extern void _store_inttype(void *dest, int desttype, uintmax_t val);
+extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
+
 /**********************************************************************/
 #ifdef L_parse_printf_format
 
@@ -433,72 +437,6 @@ size_t parse_printf_format(register const char *template,
 	return count;
 }
 
-#endif
-/**********************************************************************/
-#ifdef L_vfprintf
-
-/* We only support ascii digits (or their USC equivalent codes) in
- * precision and width settings in *printf (wide) format strings.
- * In other words, we don't currently support glibc's 'I' flag.
- * We do accept it, but it is currently ignored. */
-
-int vfprintf(FILE * __restrict stream, register const char * __restrict format,
-			 va_list arg)
-{
-	ppfs_t ppfs;
-	int count, r;
-	register const char *s;
-
-	__STDIO_THREADLOCK(stream);
-
-	count = 0;
-	s = format;
-
-	if (_ppfs_init(&ppfs, format) < 0) { /* Bad format string. */
-		_outnstr(stream, ppfs.fmtpos, strlen(ppfs.fmtpos));
-		count = -1;
-	} else {
-		_ppfs_prepargs(&ppfs, arg);	/* This did a va_copy!!! */
-
-		do {
-			while (*format && (*format != '%')) {
-				++format;
-			}
-
-			if (format-s) {		/* output any literal text in format string */
-				if ( (r = _outnstr(stream, s, format-s)) < 0) {
-					count = -1;
-					break;
-				}
-				count += r;
-			}
-
-			if (!*format) {			/* we're done */
-				break;
-			}
-		
-			if (format[1] != '%') {	/* if we get here, *format == '%' */
-				/* TODO: _do_one_spec needs to know what the output funcs are!!! */
-				ppfs.fmtpos = ++format;
-				/* TODO: check -- should only fail on stream error */
-				if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
-					count = -1;
-					break;
-				}
-				s = format = ppfs.fmtpos;
-			} else {			/* %% means literal %, so start new string */
-				s = ++format;
-				++format;
-			}
-		} while (1);
-
-		va_end(ppfs.arg);		/* Need to clean up after va_copy! */
-	}
-
-	__STDIO_THREADUNLOCK(stream);
-
-	return count;
-}
 #endif
 /**********************************************************************/
 #ifdef L__ppfs_init
@@ -514,9 +452,7 @@ int _ppfs_init(register ppfs_t *ppfs, const char *fmt0)
 	memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
 	--ppfs->maxposarg;			/* set to -1 */
 	ppfs->fmtpos = fmt0;
-#if defined(__UCLIBC_HAS_WCHAR__) && defined(__UCLIBC_HAS_LOCALE__)
-	/* Note: We don't need to check if we don't have wide chars or we only
-	 * support the C locale. */
+#ifdef __UCLIBC_HAS_WCHAR__
 	{
 		mbstate_t mbstate;
 		const char *p;
@@ -527,7 +463,7 @@ int _ppfs_init(register ppfs_t *ppfs, const char *fmt0)
 			return -1;
 		}
 	}
-#endif /* defined(__UCLIBC_HAS_WCHAR__) && defined(__UCLIBC_HAS_LOCALE__) */
+#endif /* __UCLIBC_HAS_WCHAR__ */
 	/* now set all argtypes to no-arg */
 	{
 #if 1
@@ -810,6 +746,7 @@ static char _bss_custom_printf_spec[MAX_USER_SPEC]; /* 0-init'd for us.  */
 
 char *_custom_printf_spec = _bss_custom_printf_spec;
 printf_arginfo_function *_custom_printf_arginfo[MAX_USER_SPEC];
+printf_function _custom_printf_handler[MAX_USER_SPEC];
 
 extern int _ppfs_parsespec(ppfs_t *ppfs)
 {
@@ -833,17 +770,44 @@ extern int _ppfs_parsespec(ppfs_t *ppfs)
 	static const short spec_or_mask[] = SPEC_OR_MASK;
 	static const short spec_and_mask[] = SPEC_AND_MASK;
 	static const char qual_chars[] = QUAL_CHARS;
+#ifdef __UCLIBC_HAS_WCHAR__
+	char buf[32];
+#endif /* __UCLIBC_HAS_WCHAR__ */
 
 	/* WIDE note: we can test against '%' here since we don't allow */
 	/* WIDE note: other mappings of '%' in the wide char set. */
 	preci = -1;
-	width = flags = dpoint = 0;
 	argnumber[0] = 0;
 	argnumber[1] = 0;
 	argtype[0] = __PA_NOARG;
 	argtype[1] = __PA_NOARG;
 	maxposarg = ppfs->maxposarg;
+#ifdef __UCLIBC_HAS_WCHAR__
+	/* This is somewhat lame, but saves a lot of code.  If we're dealing with
+	 * a wide stream, that means the format is a wchar string.  So, copy it
+	 * char-by-char into a normal char buffer for processing.  Make the buffer
+	 * (buf) big enough so that any reasonable format specifier will fit.
+	 * While there a legal specifiers that won't, the all involve duplicate
+	 * flags or outrageous field widths/precisions. */
+	width = dpoint = 0;
+	if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
+		fmt = ppfs->fmtpos;
+	} else {
+		fmt = buf + 1;
+		i = 0;
+		do {
+			if ((buf[i] = (char) (((wchar_t *) ppfs->fmtpos)[i-1]))
+				!= (((wchar_t *) ppfs->fmtpos)[i-1])
+				) {
+				return -1;
+			}
+		} while (buf[i++]);
+		buf[sizeof(buf)-1] = 0;
+	}
+#else  /* __UCLIBC_HAS_WCHAR__ */
+	width = flags = dpoint = 0;
 	fmt = ppfs->fmtpos;
+#endif /* __UCLIBC_HAS_WCHAR__ */
 
 	assert(fmt[-1] == '%');
 	assert(fmt[0] != '%');
@@ -877,6 +841,16 @@ extern int _ppfs_parsespec(ppfs_t *ppfs)
 			/* Now fall through to check flags. */
 		} else {
 			if (maxposarg > 0) {
+#ifdef __STDIO_PRINTF_M_SUPPORT
+#ifdef __UCLIBC_MJN3_ONLY__
+#warning TODO: Support prec and width for %m when positional args used
+				/* Actually, positional arg processing will fail in general
+				 * for specifiers that don't require an arg. */
+#endif
+				if (*fmt == 'm') {
+					goto PREC_WIDTH;
+				}
+#endif /* __STDIO_PRINTF_M_SUPPORT */
 				return -1;
 			}
 			maxposarg = 0;		/* Possible redundant store, but cuts size. */
@@ -1045,9 +1019,19 @@ extern int _ppfs_parsespec(ppfs_t *ppfs)
 	}
 
 	ppfs->maxposarg = maxposarg;
-	ppfs->fmtpos = ++fmt;
 	ppfs->conv_num = conv_num;
 
+#ifdef __UCLIBC_HAS_WCHAR__
+	if ((flags = ppfs->info._flags & FLAG_WIDESTREAM) == 0) {
+		ppfs->fmtpos = ++fmt;
+	} else {
+		ppfs->fmtpos = (const char *) (((const wchar_t *)(ppfs->fmtpos))
+									   + (fmt - buf) );
+	}
+#else  /* __UCLIBC_HAS_WCHAR__ */
+	ppfs->fmtpos = ++fmt;
+#endif /* __UCLIBC_HAS_WCHAR__ */
+
  	return ppfs->num_data_args + 2;
 }
 
@@ -1094,337 +1078,165 @@ int register_printf_function(int spec, printf_function handler,
 }
 #endif
 /**********************************************************************/
-#ifdef L__do_one_spec
+#ifdef L_vsnprintf
 
-printf_function _custom_printf_handler[MAX_USER_SPEC];
+#ifdef __STDIO_BUFFERS
+int vsnprintf(char *__restrict buf, size_t size,
+			  const char * __restrict format, va_list arg)
+{
+	FILE f;
+	int rv;
 
-extern void _store_inttype(void *dest, int desttype, uintmax_t val);
-extern uintmax_t _load_inttype(int desttype, const void *src, int uflag);
-extern void _charpad(FILE * __restrict stream, char padchar, size_t numpad);
-extern void output(FILE * __restrict stream, const char *s);
+#ifdef __STDIO_GETC_MACRO
+	f.bufgetc =
+#endif
+	f.bufpos = f.bufread = f.bufstart = buf;
 
-#define output(F,S)			fputs(S,F)
+	if (size > SIZE_MAX - (size_t) buf) {
+		size = SIZE_MAX - (size_t) buf;
+	}
+#ifdef __STDIO_PUTC_MACRO
+	f.bufputc =
+#endif
+	f.bufend = buf + size;
 
-#undef putc
-void _charpad(FILE * __restrict stream, char padchar, size_t numpad)
-{
-	/* TODO -- Use a buffer to cut down on function calls... */
-	char pad[1];
+#if 0							/* shouldn't be necessary */
+/*  #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
+	f.cookie = &(f.filedes);
+	f.gcs.read = 0;
+	f.gcs.write = 0;
+	f.gcs.seek = 0;
+	f.gcs.close = 0;
+#endif
+	f.filedes = -2;				/* for debugging */
+	f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
 
-	*pad = padchar;
-	while (numpad) {
-		_stdio_fwrite(pad, 1, stream);
-		--numpad;
+#ifdef __STDIO_MBSTATE
+	__INIT_MBSTATE(&(f.state));
+#endif /* __STDIO_MBSTATE */
+
+#ifdef __STDIO_THREADSAFE
+	f.user_locking = 0;
+	__stdio_init_mutex(&f.lock);
+#endif
+
+	rv = vfprintf(&f, format, arg);
+	if (size) {
+		if (f.bufpos == f.bufend) {
+			--f.bufpos;
+		}
+		*f.bufpos = 0;
 	}
+	return rv;
 }
+#else  /* __STDIO_BUFFERS */
+#ifdef __STDIO_GLIBC_CUSTOM_STREAMS
 
-/* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
+typedef struct {
+	size_t pos;
+	size_t len;
+	unsigned char *buf;
+	FILE *fp;
+} __snpf_cookie;
 
-int _do_one_spec(FILE * __restrict stream, register ppfs_t *ppfs, int *count)
-{
-	static const char spec_base[] = SPEC_BASE;
-	static const char prefix[] = "+\0-\0 \0000x\0000X";
-	/*                            0  2  4  6   9 11*/
-	enum {
-		PREFIX_PLUS = 0,
-		PREFIX_MINUS = 2,
-		PREFIX_SPACE = 4,
-		PREFIX_LWR_X = 6,
-		PREFIX_UPR_X = 9,
-		PREFIX_NONE = 11
-	};
+#define COOKIE ((__snpf_cookie *) cookie)
 
-#ifdef __va_arg_ptr
-	const void * const *argptr;
-#else
-	const void * argptr[MAX_ARGS_PER_SPEC];
-#endif
-	int *argtype;
-#ifdef __UCLIBC_HAS_WCHAR__
-	const wchar_t *ws = NULL;
-	mbstate_t mbstate;
-#endif /* __UCLIBC_HAS_WCHAR__ */
-	size_t slen;
-	int base;
-	int numpad;
-	int alphacase;
-	int numfill = 0;			/* TODO: fix */
-	int prefix_num = PREFIX_NONE;
-	char padchar = ' ';
-#ifdef __UCLIBC_MJN3_ONLY__
-#warning REMINDER: buf size
-#endif
-	/* TODO: buf needs to be big enough for any possible error return strings
-	 * and also for any locale-grouped long long integer strings generated.
-	 * This should be large enough for any of the current archs/locales, but
-	 * eventually this should be handled robustly. */
-	char buf[128];
+static ssize_t snpf_write(register void *cookie, const char *buf,
+						  size_t bufsize)
+{
+	size_t count;
+	register char *p;
 
-#ifdef NDEBUG
-	_ppfs_parsespec(ppfs);
-#else
-	if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
-		abort();
-	}
-#endif
-	_ppfs_setargs(ppfs);
+	/* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
 
-	argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
-	/* Deal with the argptr vs argvalue issue. */
-#ifdef __va_arg_ptr
-	argptr = (const void * const *) ppfs->argptr;
-	if (ppfs->maxposarg > 0) {	/* Using positional args... */
-		argptr += ppfs->argnumber[2] - 1;
-	}
-#else
-	/* Need to build a local copy... */
-	{
-		register argvalue_t *p = ppfs->argvalue;
-		int i;
-		if (ppfs->maxposarg > 0) {	/* Using positional args... */
-			p += ppfs->argnumber[2] - 1;
+	if (COOKIE->len > COOKIE->pos) {
+		count = COOKIE->len - COOKIE->pos - 1; /* Leave space for nul. */
+		if (count > bufsize) {
+			count = bufsize;
 		}
-		for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
-			argptr[i] = (void *) p++;
+
+		p = COOKIE->buf + COOKIE->pos;
+		while (count) {
+			*p++ = *buf++;
+			--count;
 		}
+		*p = 0;
 	}
-#endif
-	{
-		register char *s;		/* TODO: Should s be unsigned char * ? */
 
-		if (ppfs->conv_num == CONV_n) {
-			_store_inttype(*(void **)*argptr,
-						   ppfs->info._flags & __PA_INTMASK,
-						   (intmax_t) (*count));
-			return 0;
-		}
-		if (ppfs->conv_num <= CONV_i) {	/* pointer or (un)signed int */
-			alphacase = __UIM_LOWER;
-			if (((base = spec_base[(int)(ppfs->conv_num - CONV_p)]) == 10)
-				&& (PRINT_INFO_FLAG_VAL(&(ppfs->info),group))
-				) {
-				alphacase = __UIM_GROUP;
-			}
-			if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
-				if (ppfs->conv_num == CONV_X) {
-					alphacase = __UIM_UPPER;
-				}
-				if (ppfs->conv_num == CONV_p) { /* pointer */
-					prefix_num = PREFIX_LWR_X;
-				} else {		/* unsigned int */
-				}
-			} else {			/* signed int */
-				base = -base;
-			}
-			if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
-				padchar = ppfs->info.pad;
-			}
-			s = _uintmaxtostr(buf + sizeof(buf) - 1,
-							  (uintmax_t)
-							  _load_inttype(*argtype & __PA_INTMASK,
-											*argptr, base), base, alphacase);
-			if (ppfs->conv_num > CONV_u) { /* signed int */
-				if (*s == '-') {
-					PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
-					++s;		/* handle '-' in the prefix string */
-					prefix_num = PREFIX_MINUS;
-				} else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
-					prefix_num = PREFIX_PLUS;
-				} else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
-					prefix_num = PREFIX_SPACE;
-				}
-			}
-			slen = (char *)(buf + sizeof(buf) - 1) - s;
-			numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
-			if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
-				if (ppfs->conv_num <= CONV_x) {	/* x or p */
-					prefix_num = PREFIX_LWR_X;
-				}
-				if (ppfs->conv_num == CONV_X) {
-					prefix_num = PREFIX_UPR_X;
-				}
-				if ((ppfs->conv_num == CONV_o) && (numfill <= slen)) {
-					numfill = ((*s == '0') ? 1 : slen + 1);
-				}
-			}
-			if (*s == '0') {
-				if (prefix_num >= PREFIX_LWR_X) {
-					prefix_num = PREFIX_NONE;
-				}
-				if (ppfs->conv_num == CONV_p) {/* null pointer */
-					s = "(nil)";
-					slen = 5;
-					numfill = 0;
-				} else if (numfill == 0) {	/* if precision 0, no output */
-					slen = 0;
-				}
-			}
-			numfill = ((numfill > slen) ? numfill - slen : 0);
-		} else if (ppfs->conv_num <= CONV_A) {	/* floating point */
-#ifdef __STDIO_PRINTF_FLOAT
-			*count += _dtostr(stream,
-							  (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
-							   ? *(long double *) *argptr
-							   : (long double) (* (double *) *argptr)),
-							  &ppfs->info);
-			return 0;
-#else  /* __STDIO_PRINTF_FLOAT */
-			return -1;			/* TODO -- try to continue? */
-#endif /* __STDIO_PRINTF_FLOAT */
-		} else if (ppfs->conv_num <= CONV_S) {	/* wide char or string */
-#ifdef __UCLIBC_HAS_WCHAR__
-			mbstate.mask = 0;	/* Initialize the mbstate. */
-			if (ppfs->conv_num == CONV_S) { /* wide string */
-				if (!(ws = *((const wchar_t **) *argptr))) {
-					goto NULL_STRING;
-				}
-				/* We use an awful uClibc-specific hack here, passing
-				 * (char*) &ws as the conversion destination.  This signals
-				 * uClibc's wcsrtombs that we want a "restricted" length
-				 * such that the mbs fits in a buffer of the specified
-				 * size with no partial conversions. */
-				if ((slen = wcsrtombs((char *) &ws, &ws, /* Use awful hack! */
-									  ((ppfs->info.prec >= 0)
-									   ? ppfs->info.prec
-									   : SIZE_MAX), &mbstate))
-					== ((size_t)-1)
-					) {
-					return -1;	/* EILSEQ */
-				}
-			} else {			/* wide char */
-				s = buf;
-				slen = wcrtomb(s, (*((const wchar_t *) *argptr)), &mbstate);
-				if (slen == ((size_t)-1)) {
-					return -1;	/* EILSEQ */
-				}
-				s[slen] = 0;	/* TODO - Is this necessary? */
-			}
-#else  /* __UCLIBC_HAS_WCHAR__ */
-			return -1;
-#endif /* __UCLIBC_HAS_WCHAR__ */
-		} else if (ppfs->conv_num <= CONV_s) {	/* char or string */
-			if (ppfs->conv_num == CONV_s) { /* string */
-				s = *((char **) (*argptr));
-				if (s) {
-				SET_STRING_LEN:
-					slen = strnlen(s, ((ppfs->info.prec >= 0)
-									   ? ppfs->info.prec : SIZE_MAX));
-				} else {
-				NULL_STRING:
-					s = "(null)";
-					slen = 6;
-				}
-			} else {			/* char */
-				s = buf;
-				*s = (unsigned char)(*((const int *) *argptr));
-				s[1] = 0;
-				slen = 1;
-			}
-#ifdef __STDIO_PRINTF_M_SUPPORT
-		} else if (ppfs->conv_num == CONV_m) {
-			s = _glibc_strerror_r(errno, buf, sizeof(buf));
-			goto SET_STRING_LEN;
-#endif
-		} else {
-			assert(ppfs->conv_num == CONV_custom0);
+	COOKIE->pos += bufsize;
 
-			s = _custom_printf_spec;
-			do {
-				if (*s == ppfs->info.spec) {
-					int rv;
-					/* TODO -- check return value for sanity? */
-					rv = (*_custom_printf_handler
-						  [(int)(s-_custom_printf_spec)])
-						(stream, &ppfs->info, argptr);
-					if (rv < 0) {
-						return -1;
-					}
-					*count += rv;
-					return 0;
-				}
-			} while (++s < (_custom_printf_spec + MAX_USER_SPEC));
-			assert(0);
-			return -1;
-		}
+	return bufsize;
+}
 
-		{
-			size_t t;
+#undef COOKIE
 
-			t = slen + numfill;
-			if (prefix_num != PREFIX_NONE) {
-				t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
-			}
-			numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
-			*count += t + numpad;
-		}
-		if (padchar == '0') { /* TODO: check this */
-			numfill += numpad;
-			numpad = 0;
-		}
+int vsnprintf(char *__restrict buf, size_t size,
+			  const char * __restrict format, va_list arg)
+{
+	FILE f;
+	__snpf_cookie cookie;
+	int rv;
 
-		/* Now handle the output itself. */
-		if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
-			_charpad(stream, ' ', numpad);
-			numpad = 0;
-		}
-		output(stream, prefix + prefix_num);
-		_charpad(stream, '0', numfill);
-#ifdef __UCLIBC_HAS_WCHAR__
-		if (!ws) {
-			_outnstr(stream, s, slen);
-		} else {				/* wide string */
-			size_t t;
-			mbstate.mask = 0;	/* Initialize the mbstate. */
-			while (slen) {
-				t = (slen <= sizeof(buf)) ? slen : sizeof(buf);
-				t = wcsrtombs(buf, &ws, t, &mbstate);
-				assert (t != ((size_t)(-1)));
-				_outnstr(stream, buf, t);
-				slen -= t;
-			}
-			ws = NULL;			/* Reset ws. */
-		}
-#else  /* __UCLIBC_HAS_WCHAR__ */
-		_outnstr(stream, s, slen);
-#endif /* __UCLIBC_HAS_WCHAR__ */
-		_charpad(stream, ' ', numpad);
-	}
+	cookie.buf = buf;
+	cookie.len = size;
+	cookie.pos = 0;
+	cookie.fp = &f;
 
-	return 0;
+	f.cookie = &cookie;
+	f.gcs.write = snpf_write;
+	f.gcs.read = NULL;
+	f.gcs.seek = NULL;
+	f.gcs.close = NULL;
+
+	f.filedes = -1;				/* For debugging. */
+	f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
+
+#ifdef __STDIO_MBSTATE
+	__INIT_MBSTATE(&(f.state));
+#endif /* __STDIO_MBSTATE */
+
+#ifdef __STDIO_THREADSAFE
+	f.user_locking = 0;
+	__stdio_init_mutex(&f.lock);
+#endif
+
+	rv = vfprintf(&f, format, arg);
+
+	return rv;
 }
+
+#else  /* __STDIO_GLIBC_CUSTOM_STREAMS */
+#warning skipping vsnprintf since no buffering and no custom streams!
+#endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
+#endif /* __STDIO_BUFFERS */
 #endif
 /**********************************************************************/
-#ifdef L_vsnprintf
+#ifdef L_vdprintf
 
-#ifdef __STDIO_BUFFERS
-int vsnprintf(char *__restrict buf, size_t size,
-			  const char * __restrict format, va_list arg)
+int vdprintf(int filedes, const char * __restrict format, va_list arg)
 {
 	FILE f;
 	int rv;
+#ifdef __STDIO_BUFFERS
+	char buf[64];				/* TODO: provide _optional_ buffering? */
 
 #ifdef __STDIO_GETC_MACRO
 	f.bufgetc =
 #endif
 	f.bufpos = f.bufread = f.bufstart = buf;
-
-	if (size > SIZE_MAX - (size_t) buf) {
-		size = SIZE_MAX - (size_t) buf;
-	}
 #ifdef __STDIO_PUTC_MACRO
-	f.bufputc =
+	f.bufputc = 
 #endif
-	f.bufend = buf + size;
-
-#if 0							/* shouldn't be necessary */
-/*  #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
+	f.bufend = buf + sizeof(buf);
+#endif /* __STDIO_BUFFERS */
+#ifdef __STDIO_GLIBC_CUSTOM_STREAMS
 	f.cookie = &(f.filedes);
-	f.gcs.read = 0;
-	f.gcs.write = 0;
-	f.gcs.seek = 0;
-	f.gcs.close = 0;
+	f.gcs.read = _cs_read;
+	f.gcs.write = _cs_write;
+	f.gcs.seek = 0;				/* The internal seek func handles normals. */
+	f.gcs.close = _cs_close;
 #endif
-	f.filedes = -2;				/* for debugging */
+	f.filedes = filedes;
 	f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
 
 #ifdef __STDIO_MBSTATE
@@ -1437,136 +1249,8 @@ int vsnprintf(char *__restrict buf, size_t size,
 #endif
 
 	rv = vfprintf(&f, format, arg);
-	if (size) {
-		if (f.bufpos == f.bufend) {
-			--f.bufpos;
-		}
-		*f.bufpos = 0;
-	}
-	return rv;
-}
-#else  /* __STDIO_BUFFERS */
-#ifdef __STDIO_GLIBC_CUSTOM_STREAMS
-
-typedef struct {
-	size_t pos;
-	size_t len;
-	unsigned char *buf;
-	FILE *fp;
-} __snpf_cookie;
-
-#define COOKIE ((__snpf_cookie *) cookie)
-
-static ssize_t snpf_write(register void *cookie, const char *buf,
-						  size_t bufsize)
-{
-	size_t count;
-	register char *p;
-
-	/* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
-
-	if (COOKIE->len > COOKIE->pos) {
-		count = COOKIE->len - COOKIE->pos - 1; /* Leave space for nul. */
-		if (count > bufsize) {
-			count = bufsize;
-		}
-
-		p = COOKIE->buf + COOKIE->pos;
-		while (count) {
-			*p++ = *buf++;
-			--count;
-		}
-		*p = 0;
-	}
-
-	COOKIE->pos += bufsize;
-
-	return bufsize;
-}
-
-#undef COOKIE
-
-int vsnprintf(char *__restrict buf, size_t size,
-			  const char * __restrict format, va_list arg)
-{
-	FILE f;
-	__snpf_cookie cookie;
-	int rv;
-
-	cookie.buf = buf;
-	cookie.len = size;
-	cookie.pos = 0;
-	cookie.fp = &f;
-
-	f.cookie = &cookie;
-	f.gcs.write = snpf_write;
-	f.gcs.read = NULL;
-	f.gcs.seek = NULL;
-	f.gcs.close = NULL;
-
-	f.filedes = -1;				/* For debugging. */
-	f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
-
-#ifdef __STDIO_MBSTATE
-	__INIT_MBSTATE(&(f.state));
-#endif /* __STDIO_MBSTATE */
-
-#ifdef __STDIO_THREADSAFE
-	f.user_locking = 0;
-	__stdio_init_mutex(&f.lock);
-#endif
-
-	rv = vfprintf(&f, format, arg);
-
-	return rv;
-}
-
-#else  /* __STDIO_GLIBC_CUSTOM_STREAMS */
-#warning skipping vsnprintf since no buffering and no custom streams!
-#endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
-#endif /* __STDIO_BUFFERS */
-#endif
-/**********************************************************************/
-#ifdef L_vdprintf
-
-int vdprintf(int filedes, const char * __restrict format, va_list arg)
-{
-	FILE f;
-	int rv;
-#ifdef __STDIO_BUFFERS
-	char buf[64];				/* TODO: provide _optional_ buffering? */
-
-#ifdef __STDIO_GETC_MACRO
-	f.bufgetc =
-#endif
-	f.bufpos = f.bufread = f.bufstart = buf;
-#ifdef __STDIO_PUTC_MACRO
-	f.bufputc = 
-#endif
-	f.bufend = buf + sizeof(buf);
-#endif /* __STDIO_BUFFERS */
-#ifdef __STDIO_GLIBC_CUSTOM_STREAMS
-	f.cookie = &(f.filedes);
-	f.gcs.read = _cs_read;
-	f.gcs.write = _cs_write;
-	f.gcs.seek = 0;				/* The internal seek func handles normals. */
-	f.gcs.close = _cs_close;
-#endif
-	f.filedes = filedes;
-	f.modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
-
-#ifdef __STDIO_MBSTATE
-	__INIT_MBSTATE(&(f.state));
-#endif /* __STDIO_MBSTATE */
-
-#ifdef __STDIO_THREADSAFE
-	f.user_locking = 0;
-	__stdio_init_mutex(&f.lock);
-#endif
-
-	rv = vfprintf(&f, format, arg);
-
-	return fflush(&f) ? -1 : rv;
+
+	return fflush(&f) ? -1 : rv;
 }
 
 #endif
@@ -1751,6 +1435,124 @@ int sprintf(char *__restrict buf, const char * __restrict format, ...)
 #endif
 #endif
 /**********************************************************************/
+#ifdef L_vswprintf
+
+#ifdef __STDIO_BUFFERS
+int vswprintf(wchar_t *__restrict buf, size_t size,
+			  const wchar_t * __restrict format, va_list arg)
+{
+	FILE f;
+	int rv;
+
+#ifdef __STDIO_GETC_MACRO
+	f.bufgetc =
+#endif
+	f.bufpos = f.bufread = f.bufstart = (char *) buf;
+
+/* 	if (size > SIZE_MAX - (size_t) buf) { */
+/* 		size = SIZE_MAX - (size_t) buf; */
+/* 	} */
+#ifdef __STDIO_PUTC_MACRO
+	f.bufputc =
+#endif
+	f.bufend = (char *)(buf + size);
+
+#if 0							/* shouldn't be necessary */
+/*  #ifdef __STDIO_GLIBC_CUSTOM_STREAMS */
+	f.cookie = &(f.filedes);
+	f.gcs.read = 0;
+	f.gcs.write = 0;
+	f.gcs.seek = 0;
+	f.gcs.close = 0;
+#endif
+	f.filedes = -3;				/* for debugging */
+	f.modeflags = (__FLAG_WIDE|__FLAG_WRITEONLY|__FLAG_WRITING);
+
+#ifdef __STDIO_MBSTATE
+	__INIT_MBSTATE(&(f.state));
+#endif /* __STDIO_MBSTATE */
+
+#ifdef __STDIO_THREADSAFE
+	f.user_locking = 0;
+	__stdio_init_mutex(&f.lock);
+#endif
+
+	rv = vfwprintf(&f, format, arg);
+
+	/* NOTE: Return behaviour differs from snprintf... */
+	if (f.bufpos == f.bufend) {
+		rv = -1;
+		if (size) {
+			f.bufpos = (char *)(((wchar_t *) f.bufpos) - 1);
+		}
+	}
+	if (size) {
+		*((wchar_t *) f.bufpos) = 0;
+	}
+	return rv;
+}
+#else  /* __STDIO_BUFFERS */
+#warning skipping vswprintf since no buffering!
+#endif /* __STDIO_BUFFERS */
+#endif
+/**********************************************************************/
+#ifdef L_swprintf
+#ifdef __STDIO_BUFFERS
+
+int swprintf(wchar_t *__restrict buf, size_t size,
+			 const wchar_t * __restrict format, ...)
+{
+	va_list arg;
+	int rv;
+
+	va_start(arg, format);
+	rv = vswprintf(buf, size, format, arg);
+	va_end(arg);
+	return rv;
+}
+
+#else  /* __STDIO_BUFFERS */
+#warning skipping vsWprintf since no buffering!
+#endif /* __STDIO_BUFFERS */
+#endif
+/**********************************************************************/
+#ifdef L_fwprintf
+
+int fwprintf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
+{
+	va_list arg;
+	int rv;
+
+	va_start(arg, format);
+	rv = vfwprintf(stream, format, arg);
+	va_end(arg);
+
+	return rv;
+}
+
+#endif
+/**********************************************************************/
+#ifdef L_vwprintf
+int vwprintf(const wchar_t * __restrict format, va_list arg)
+{
+	return vfwprintf(stdout, format, arg);
+}
+#endif
+/**********************************************************************/
+#ifdef L_wprintf
+int wprintf(const wchar_t * __restrict format, ...)
+{
+	va_list arg;
+	int rv;
+
+	va_start(arg, format);
+	rv = vfwprintf(stdout, format, arg);
+	va_end(arg);
+
+	return rv;
+}
+#endif
+/**********************************************************************/
 #ifdef L__dtostr
 /*
  * Copyright (C) 2000, 2001 Manuel Novoa III
@@ -2298,3 +2100,581 @@ extern uintmax_t _load_inttype(int desttype, register const void *src,
 
 #endif
 /**********************************************************************/
+#if defined(L_vfprintf) || defined(L_vfwprintf)
+
+/* We only support ascii digits (or their USC equivalent codes) in
+ * precision and width settings in *printf (wide) format strings.
+ * In other words, we don't currently support glibc's 'I' flag.
+ * We do accept it, but it is currently ignored. */
+
+
+#ifdef L_vfprintf
+
+#define VFPRINTF vfprintf
+#define FMT_TYPE char
+#define OUTNSTR _outnstr
+#define STRLEN  strlen
+#define _PPFS_init _ppfs_init
+#define OUTPUT(F,S)			fputs(S,F)
+#define _outnstr(stream, string, len)	_stdio_fwrite(string, len, stream)
+
+#else  /* L_vfprintf */
+
+#define VFPRINTF vfwprintf
+#define FMT_TYPE wchar_t
+#define OUTNSTR _outnwcs
+#define STRLEN  wcslen
+#define _PPFS_init _ppwfs_init
+#define OUTPUT(F,S)			fputws(S,F)
+#define _outnwcs(stream, wstring, len)	_wstdio_fwrite(wstring, len, stream)
+
+static void _outnstr(FILE *stream, const char *s, size_t wclen)
+{
+	/* NOTE!!! len here is the number of wchars we want to generate!!! */
+	wchar_t wbuf[64];
+	mbstate_t mbstate;
+	size_t todo, r;
+
+	mbstate.mask = 0;
+	todo = wclen;
+	
+	while (todo) {
+		r = mbsrtowcs(wbuf, &s, sizeof(wbuf)/sizeof(wbuf[0]), &mbstate);
+		assert(((ssize_t)r) > 0);
+		_outnwcs(stream, wbuf, r);
+		todo -= r;
+	}
+}
+
+static int _ppwfs_init(register ppfs_t *ppfs, const wchar_t *fmt0)
+{
+	static const wchar_t invalid_wcs[] = L"Invalid wide format string.";
+	int r;
+
+	/* First, zero out everything... argnumber[], argtype[], argptr[] */
+	memset(ppfs, 0, sizeof(ppfs_t)); /* TODO: nonportable???? */
+	--ppfs->maxposarg;			/* set to -1 */
+	ppfs->fmtpos = (const char *) fmt0;
+	ppfs->info._flags = FLAG_WIDESTREAM;
+
+	{
+		mbstate_t mbstate;
+		const wchar_t *p;
+		mbstate.mask = 0;	/* Initialize the mbstate. */
+		p = fmt0;
+		if (wcsrtombs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
+			ppfs->fmtpos = (const char *) invalid_wcs;
+			return -1;
+		}
+	}
+
+	/* now set all argtypes to no-arg */
+	{
+#if 1
+		/* TODO - use memset here since already "paid for"? */
+		register int *p = ppfs->argtype;
+		
+		r = MAX_ARGS;
+		do {
+			*p++ = __PA_NOARG;
+		} while (--r);
+#else
+		/* TODO -- get rid of this?? */
+		register char *p = (char *) ((MAX_ARGS-1) * sizeof(int));
+
+		do {
+			*((int *)(((char *)ppfs) + ((int)p) + offsetof(ppfs_t,argtype))) = __PA_NOARG;
+			p -= sizeof(int);
+		} while (p);
+#endif
+	}
+
+	/*
+	 * Run through the entire format string to validate it and initialize
+	 * the positional arg numbers (if any).
+	 */
+	{
+		register const wchar_t *fmt = fmt0;
+
+		while (*fmt) {
+			if ((*fmt == '%') && (*++fmt != '%')) {
+				ppfs->fmtpos = (const char *) fmt; /* back up to the '%' */
+				if ((r = _ppfs_parsespec(ppfs)) < 0) {
+					return -1;
+				}
+				fmt = (const wchar_t *) ppfs->fmtpos; /* update to one past end of spec */
+			} else {
+				++fmt;
+			}
+		}
+		ppfs->fmtpos = (const char *) fmt0; /* rewind */
+	}
+
+	/* If we have positional args, make sure we know all the types. */
+	{
+		register int *p = ppfs->argtype;
+		r = ppfs->maxposarg;
+		while (--r >= 0) {
+			if ( *p == __PA_NOARG ) { /* missing arg type!!! */
+				return -1;
+			}
+			++p;
+		}
+	}
+
+	return 0;
+}
+
+#endif /* L_vfprintf */
+
+static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
+{
+	/* TODO -- Use a buffer to cut down on function calls... */
+	FMT_TYPE pad[1];
+
+	*pad = padchar;
+	while (numpad) {
+		OUTNSTR(stream, pad, 1);
+		--numpad;
+	}
+}
+
+/* TODO -- Dynamically allocate work space to accomodate stack-poor archs? */
+static int _do_one_spec(FILE * __restrict stream,
+						 register ppfs_t *ppfs, int *count)
+{
+	static const char spec_base[] = SPEC_BASE;
+#ifdef L_vfprintf
+	static const char prefix[] = "+\0-\0 \0000x\0000X";
+	/*                            0  2  4  6   9 11*/
+#else  /* L_vfprintf */
+	static const wchar_t prefix[] = L"+\0-\0 \0000x\0000X";
+#endif /* L_vfprintf */
+	enum {
+		PREFIX_PLUS = 0,
+		PREFIX_MINUS = 2,
+		PREFIX_SPACE = 4,
+		PREFIX_LWR_X = 6,
+		PREFIX_UPR_X = 9,
+		PREFIX_NONE = 11
+	};
+
+#ifdef __va_arg_ptr
+	const void * const *argptr;
+#else
+	const void * argptr[MAX_ARGS_PER_SPEC];
+#endif
+	int *argtype;
+#ifdef __UCLIBC_HAS_WCHAR__
+	const wchar_t *ws = NULL;
+	mbstate_t mbstate;
+#endif /* __UCLIBC_HAS_WCHAR__ */
+	size_t slen;
+#ifdef L_vfprintf
+#define SLEN slen
+#else
+	size_t SLEN;
+	wchar_t wbuf[2];
+#endif
+	int base;
+	int numpad;
+	int alphacase;
+	int numfill = 0;			/* TODO: fix */
+	int prefix_num = PREFIX_NONE;
+	char padchar = ' ';
+#ifdef __UCLIBC_MJN3_ONLY__
+#warning REMINDER: buf size
+#endif
+	/* TODO: buf needs to be big enough for any possible error return strings
+	 * and also for any locale-grouped long long integer strings generated.
+	 * This should be large enough for any of the current archs/locales, but
+	 * eventually this should be handled robustly. */
+	char buf[128];
+
+#ifdef NDEBUG
+	_ppfs_parsespec(ppfs);
+#else
+	if (_ppfs_parsespec(ppfs) < 0) { /* TODO: just for debugging */
+		abort();
+	}
+#endif
+	_ppfs_setargs(ppfs);
+
+	argtype = ppfs->argtype + ppfs->argnumber[2] - 1;
+	/* Deal with the argptr vs argvalue issue. */
+#ifdef __va_arg_ptr
+	argptr = (const void * const *) ppfs->argptr;
+	if (ppfs->maxposarg > 0) {	/* Using positional args... */
+		argptr += ppfs->argnumber[2] - 1;
+	}
+#else
+	/* Need to build a local copy... */
+	{
+		register argvalue_t *p = ppfs->argvalue;
+		int i;
+		if (ppfs->maxposarg > 0) {	/* Using positional args... */
+			p += ppfs->argnumber[2] - 1;
+		}
+		for (i = 0 ; i < ppfs->num_data_args ; i++ ) {
+			argptr[i] = (void *) p++;
+		}
+	}
+#endif
+	{
+		register char *s;		/* TODO: Should s be unsigned char * ? */
+
+		if (ppfs->conv_num == CONV_n) {
+			_store_inttype(*(void **)*argptr,
+						   ppfs->info._flags & __PA_INTMASK,
+						   (intmax_t) (*count));
+			return 0;
+		}
+		if (ppfs->conv_num <= CONV_i) {	/* pointer or (un)signed int */
+			alphacase = __UIM_LOWER;
+			if (((base = spec_base[(int)(ppfs->conv_num - CONV_p)]) == 10)
+				&& (PRINT_INFO_FLAG_VAL(&(ppfs->info),group))
+				) {
+				alphacase = __UIM_GROUP;
+			}
+			if (ppfs->conv_num <= CONV_u) { /* pointer or unsigned int */
+				if (ppfs->conv_num == CONV_X) {
+					alphacase = __UIM_UPPER;
+				}
+				if (ppfs->conv_num == CONV_p) { /* pointer */
+					prefix_num = PREFIX_LWR_X;
+				} else {		/* unsigned int */
+				}
+			} else {			/* signed int */
+				base = -base;
+			}
+			if (ppfs->info.prec < 0) { /* Ignore '0' flag if prec specified. */
+				padchar = ppfs->info.pad;
+			}
+			s = _uintmaxtostr(buf + sizeof(buf) - 1,
+							  (uintmax_t)
+							  _load_inttype(*argtype & __PA_INTMASK,
+											*argptr, base), base, alphacase);
+			if (ppfs->conv_num > CONV_u) { /* signed int */
+				if (*s == '-') {
+					PRINT_INFO_SET_FLAG(&(ppfs->info),showsign);
+					++s;		/* handle '-' in the prefix string */
+					prefix_num = PREFIX_MINUS;
+				} else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),showsign)) {
+					prefix_num = PREFIX_PLUS;
+				} else if (PRINT_INFO_FLAG_VAL(&(ppfs->info),space)) {
+					prefix_num = PREFIX_SPACE;
+				}
+			}
+			slen = (char *)(buf + sizeof(buf) - 1) - s;
+#ifdef L_vfwprintf
+			{
+				const char *q = s;
+				mbstate.mask = 0; /* Initialize the mbstate. */
+				SLEN = mbsrtowcs(NULL, &q, 0, &mbstate);
+			}
+#endif
+			numfill = ((ppfs->info.prec < 0) ? 1 : ppfs->info.prec);
+			if (PRINT_INFO_FLAG_VAL(&(ppfs->info),alt)) {
+				if (ppfs->conv_num <= CONV_x) {	/* x or p */
+					prefix_num = PREFIX_LWR_X;
+				}
+				if (ppfs->conv_num == CONV_X) {
+					prefix_num = PREFIX_UPR_X;
+				}
+				if ((ppfs->conv_num == CONV_o) && (numfill <= SLEN)) {
+					numfill = ((*s == '0') ? 1 : SLEN + 1);
+				}
+			}
+			if (*s == '0') {
+				if (prefix_num >= PREFIX_LWR_X) {
+					prefix_num = PREFIX_NONE;
+				}
+				if (ppfs->conv_num == CONV_p) {/* null pointer */
+					s = "(nil)";
+#ifdef L_vfwprintf
+					SLEN =
+#endif
+					slen = 5;
+					numfill = 0;
+				} else if (numfill == 0) {	/* if precision 0, no output */
+#ifdef L_vfwprintf
+					SLEN =
+#endif
+					slen = 0;
+				}
+			}
+			numfill = ((numfill > SLEN) ? numfill - SLEN : 0);
+		} else if (ppfs->conv_num <= CONV_A) {	/* floating point */
+#ifdef L_vfwprintf
+#ifdef __UCLIBC_MJN3_ONLY__
+#warning fix dtostr
+#endif
+			return -1;
+#else  /* L_vfwprintf */
+#ifdef __STDIO_PRINTF_FLOAT
+			*count += _dtostr(stream,
+							  (PRINT_INFO_FLAG_VAL(&(ppfs->info),is_long_double)
+							   ? *(long double *) *argptr
+							   : (long double) (* (double *) *argptr)),
+							  &ppfs->info);
+			return 0;
+#else  /* __STDIO_PRINTF_FLOAT */
+			return -1;			/* TODO -- try to continue? */
+#endif /* __STDIO_PRINTF_FLOAT */
+#endif /* L_vfwprintf */
+		} else if (ppfs->conv_num <= CONV_S) {	/* wide char or string */
+#ifdef L_vfprintf
+
+#ifdef __UCLIBC_HAS_WCHAR__
+			mbstate.mask = 0;	/* Initialize the mbstate. */
+			if (ppfs->conv_num == CONV_S) { /* wide string */
+				if (!(ws = *((const wchar_t **) *argptr))) {
+					goto NULL_STRING;
+				}
+				/* We use an awful uClibc-specific hack here, passing
+				 * (char*) &ws as the conversion destination.  This signals
+				 * uClibc's wcsrtombs that we want a "restricted" length
+				 * such that the mbs fits in a buffer of the specified
+				 * size with no partial conversions. */
+				if ((slen = wcsrtombs((char *) &ws, &ws, /* Use awful hack! */
+									  ((ppfs->info.prec >= 0)
+									   ? ppfs->info.prec
+									   : SIZE_MAX), &mbstate))
+					== ((size_t)-1)
+					) {
+					return -1;	/* EILSEQ */
+				}
+			} else {			/* wide char */
+				s = buf;
+				slen = wcrtomb(s, (*((const wchar_t *) *argptr)), &mbstate);
+				if (slen == ((size_t)-1)) {
+					return -1;	/* EILSEQ */
+				}
+				s[slen] = 0;	/* TODO - Is this necessary? */
+			}
+#else  /* __UCLIBC_HAS_WCHAR__ */
+			return -1;
+#endif /* __UCLIBC_HAS_WCHAR__ */
+		} else if (ppfs->conv_num <= CONV_s) {	/* char or string */
+			if (ppfs->conv_num == CONV_s) { /* string */
+				s = *((char **) (*argptr));
+				if (s) {
+				SET_STRING_LEN:
+					slen = strnlen(s, ((ppfs->info.prec >= 0)
+									   ? ppfs->info.prec : SIZE_MAX));
+				} else {
+#ifdef __UCLIBC_HAS_WCHAR__
+				NULL_STRING:
+#endif
+					s = "(null)";
+					slen = 6;
+				}
+			} else {			/* char */
+				s = buf;
+				*s = (unsigned char)(*((const int *) *argptr));
+				s[1] = 0;
+				slen = 1;
+			}
+
+#else  /* L_vfprintf */
+
+			if (ppfs->conv_num == CONV_S) { /* wide string */
+				ws = *((wchar_t **) (*argptr));
+				if (!ws) {
+					goto NULL_STRING;
+				}
+				SLEN = wcsnlen(ws, ((ppfs->info.prec >= 0)
+									? ppfs->info.prec : SIZE_MAX));
+			} else {			/* wide char */
+				*wbuf = (wchar_t)(*((const wint_t *) *argptr));
+			CHAR_CASE:
+				ws = wbuf;
+				wbuf[1] = 0;
+				SLEN = 1;
+			}
+
+		} else if (ppfs->conv_num <= CONV_s) {	/* char or string */
+
+			if (ppfs->conv_num == CONV_s) { /* string */
+#ifdef __UCLIBC_MJN3_ONLY__
+#warning Fix %s for vfwprintf... output upto illegal sequence?
+#endif
+				s = *((char **) (*argptr));
+				if (s) {
+				SET_STRING_LEN:
+					/* We use an awful uClibc-specific hack here, passing
+					 * (wchar_t*) &mbstate as the conversion destination.
+					 *  This signals uClibc's mbsrtowcs that we want a
+					 * "restricted" length such that the mbs fits in a buffer
+					 * of the specified size with no partial conversions. */
+					{
+						const char *q = s;
+						mbstate.mask = 0;	/* Initialize the mbstate. */
+						SLEN = mbsrtowcs((wchar_t *) &mbstate, &q,
+										 ((ppfs->info.prec >= 0)
+										  ? ppfs->info.prec : SIZE_MAX),
+										 &mbstate);
+					}
+					if (SLEN == ((size_t)(-1))) {
+						return -1;	/* EILSEQ */
+					}
+				} else {
+				NULL_STRING:
+					s = "(null)";
+					SLEN = slen = 6;
+				}
+			} else {			/* char */
+				*wbuf = btowc( (unsigned char)(*((const int *) *argptr)) );
+				goto CHAR_CASE;
+			}
+
+#endif /* L_vfprintf */
+
+#ifdef __STDIO_PRINTF_M_SUPPORT
+		} else if (ppfs->conv_num == CONV_m) {
+			s = _glibc_strerror_r(errno, buf, sizeof(buf));
+			goto SET_STRING_LEN;
+#endif
+		} else {
+			assert(ppfs->conv_num == CONV_custom0);
+
+			s = _custom_printf_spec;
+			do {
+				if (*s == ppfs->info.spec) {
+					int rv;
+					/* TODO -- check return value for sanity? */
+					rv = (*_custom_printf_handler
+						  [(int)(s-_custom_printf_spec)])
+						(stream, &ppfs->info, argptr);
+					if (rv < 0) {
+						return -1;
+					}
+					*count += rv;
+					return 0;
+				}
+			} while (++s < (_custom_printf_spec + MAX_USER_SPEC));
+			assert(0);
+			return -1;
+		}
+
+		{
+			size_t t;
+
+			t = SLEN + numfill;
+			if (prefix_num != PREFIX_NONE) {
+				t += ((prefix_num < PREFIX_LWR_X) ? 1 : 2);
+			}
+			numpad = ((ppfs->info.width > t) ? (ppfs->info.width - t) : 0);
+			*count += t + numpad;
+		}
+		if (padchar == '0') { /* TODO: check this */
+			numfill += numpad;
+			numpad = 0;
+		}
+
+		/* Now handle the output itself. */
+		if (!PRINT_INFO_FLAG_VAL(&(ppfs->info),left)) {
+			_charpad(stream, ' ', numpad);
+			numpad = 0;
+		}
+		OUTPUT(stream, prefix + prefix_num);
+		_charpad(stream, '0', numfill);
+
+#ifdef L_vfprintf
+
+#ifdef __UCLIBC_HAS_WCHAR__
+		if (!ws) {
+			_outnstr(stream, s, slen);
+		} else {				/* wide string */
+			size_t t;
+			mbstate.mask = 0;	/* Initialize the mbstate. */
+			while (slen) {
+				t = (slen <= sizeof(buf)) ? slen : sizeof(buf);
+				t = wcsrtombs(buf, &ws, t, &mbstate);
+				assert (t != ((size_t)(-1)));
+				_outnstr(stream, buf, t);
+				slen -= t;
+			}
+			ws = NULL;			/* Reset ws. */
+		}
+#else  /* __UCLIBC_HAS_WCHAR__ */
+		_outnstr(stream, s, slen);
+#endif /* __UCLIBC_HAS_WCHAR__ */
+
+#else  /* L_vfprintf */
+
+		if (!ws) {
+			_outnstr(stream, s, SLEN);
+		} else {
+			_outnwcs(stream, ws, SLEN);
+			ws = NULL;			/* Reset ws. */
+		}
+
+#endif /* L_vfprintf */
+		_charpad(stream, ' ', numpad);
+	}
+
+	return 0;
+}
+
+int VFPRINTF (FILE * __restrict stream,
+			  register const FMT_TYPE * __restrict format,
+			  va_list arg)
+{
+	ppfs_t ppfs;
+	int count, r;
+	register const FMT_TYPE *s;
+
+	__STDIO_THREADLOCK(stream);
+
+	count = 0;
+	s = format;
+
+	if (_PPFS_init(&ppfs, format) < 0) { /* Bad format string. */
+		OUTNSTR(stream, (const FMT_TYPE *) ppfs.fmtpos,
+				STRLEN((const FMT_TYPE *)(ppfs.fmtpos)));
+		count = -1;
+	} else {
+		_ppfs_prepargs(&ppfs, arg);	/* This did a va_copy!!! */
+
+		do {
+			while (*format && (*format != '%')) {
+				++format;
+			}
+
+			if (format-s) {		/* output any literal text in format string */
+				if ( (r = OUTNSTR(stream, s, format-s)) < 0) {
+					count = -1;
+					break;
+				}
+				count += r;
+			}
+
+			if (!*format) {			/* we're done */
+				break;
+			}
+		
+			if (format[1] != '%') {	/* if we get here, *format == '%' */
+				/* TODO: _do_one_spec needs to know what the output funcs are!!! */
+				ppfs.fmtpos = (const char *)(++format);
+				/* TODO: check -- should only fail on stream error */
+				if ( (r = _do_one_spec(stream, &ppfs, &count)) < 0) {
+					count = -1;
+					break;
+				}
+				s = format = (const FMT_TYPE *) ppfs.fmtpos;
+			} else {			/* %% means literal %, so start new string */
+				s = ++format;
+				++format;
+			}
+		} while (1);
+
+		va_end(ppfs.arg);		/* Need to clean up after va_copy! */
+	}
+
+	__STDIO_THREADUNLOCK(stream);
+
+	return count;
+}
+#endif
+/**********************************************************************/

+ 58 - 0
libc/stdio/stdio.c

@@ -45,6 +45,9 @@
  *     function pointers are set to that it is a "normal" file with a
  *     file descriptor of -1.  Note: The cookie pointer is reset to NULL
  *     if the FILE struct is free'd by fclose.
+ *
+ *  Nov 21, 2002
+ *  Added internal function _wstdio_fwrite.
  */
 
 /* Before we include anything, convert L_ctermid to L_ctermid_function
@@ -3449,3 +3452,58 @@ char *_uintmaxtostr(register char * __restrict bufend, uintmax_t uval,
 
 #endif
 /**********************************************************************/
+#ifdef L__wstdio_fwrite
+
+#include <wchar.h>
+
+size_t _wstdio_fwrite(const wchar_t *__restrict ws, size_t n,
+					  register FILE *__restrict stream)
+{
+	size_t r, count;
+	char buf[64];
+	const wchar_t *pw;
+
+#ifdef __STDIO_BUFFERS
+	if (stream->filedes == -3) { /* Special case to support {v}swprintf. */
+		count = ((wchar_t *)(stream->bufend)) - ((wchar_t *)(stream->bufpos));
+		if (count > n) {
+			count = n;
+		}
+		if (count) {
+			wmemcpy((wchar_t *)(stream->bufpos), ws, count);
+			stream->bufpos = (char *)(((wchar_t *)(stream->bufpos)) + count);
+		}
+		return n;
+	}
+#endif
+
+	if (stream->modeflags & __FLAG_NARROW) {
+		stream->modeflags |= __FLAG_ERROR;
+		__set_errno(EBADF);
+		return 0;
+	}
+	stream->modeflags |= __FLAG_WIDE;
+
+	pw = ws;
+	count = 0;
+	while (n > count) {
+		r = wcsnrtombs(buf, &pw, n, sizeof(buf), &stream->state);
+		if (r != ((size_t) -1)) { /* No encoding errors */
+			if (!r) {
+				++r;		  /* 0 is returned when nul is reached. */
+				pw = ws + count + r; /* pw was set to NULL, so correct. */
+			}
+			if (_stdio_fwrite(buf, r, stream) == r) {
+				count = pw - ws;
+				continue;
+			}
+		}
+		break;
+	}
+
+	/* Note: The count is incorrect if 0 < _stdio_fwrite return < r!!! */
+	return count;
+}
+
+#endif
+/**********************************************************************/

+ 7 - 4
libc/stdlib/Makefile

@@ -35,18 +35,21 @@ endif
 
 MSRC = stdlib.c
 MOBJ = abs.o labs.o atoi.o atol.o strtol.o strtoul.o _stdlib_strto_l.o \
-       qsort.o bsearch.o
-MOBJ +=  llabs.o atoll.o strtoll.o strtoull.o _stdlib_strto_ll.o
+       qsort.o bsearch.o \
+       llabs.o atoll.o strtoll.o strtoull.o _stdlib_strto_ll.o
+# (aliases) strtoq.o strtouq.o
 ifeq ($(UCLIBC_HAS_FLOATS),y)
 	MOBJ += atof.o
 endif
 
 ifeq ($(UCLIBC_HAS_WCHAR),y)
 	MOBJ += mblen.o mbtowc.o wctomb.o mbstowcs.o wcstombs.o \
-		_stdlib_mb_cur_max.o 
+		_stdlib_mb_cur_max.o _stdlib_wcsto_l.o _stdlib_wcsto_ll.o \
+		wcstol.o wcstoul.o wcstoll.o wcstoull.o
+# (aliases) wcstoq.o wcstouq.o
+# wcstod wcstof wcstold
 endif
 
-
 MSRC2=atexit.c
 MOBJ2=atexit.o on_exit.o __exit_handler.o exit.o
 CSRC =	abort.c getenv.c mkdtemp.c mktemp.c realpath.c mkstemp.c mkstemp64.c \

+ 169 - 46
libc/stdlib/stdlib.c

@@ -26,8 +26,10 @@
  *  ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION!   ATTENTION! */
 
 /* Oct 29, 2002
- *
  * Fix a couple of 'restrict' bugs in mbstowcs and wcstombs.
+ *
+ * Nov 21, 2002
+ * Add wscto{inttype} functions.
  */
 
 #define _ISOC99_SOURCE			/* for ULLONG primarily... */
@@ -52,10 +54,46 @@
 #define atoll __ignore_atoll
 #define strtoll __ignore_strtoll
 #define strtoull __ignore_strtoull
+#define wcstoll __ignore_wcstoll
+#define wcstoull __ignore_wcstoull
 #endif
 
 #include <stdlib.h>
 
+#ifdef __UCLIBC_HAS_WCHAR__
+
+#include <locale.h>
+#include <wchar.h>
+#include <wctype.h>
+
+/* TODO: clean up the following... */
+
+#if WCHAR_MAX > 0xffffUL
+#define UTF_8_MAX_LEN 6
+#else
+#define UTF_8_MAX_LEN 3
+#endif
+
+#ifdef __UCLIBC_HAS_LOCALE__
+#define ENCODING (__global_locale.encoding)
+#ifndef __CTYPE_HAS_UTF_8_LOCALES
+#warning __CTYPE_HAS_UTF_8_LOCALES not set!
+#endif
+#else
+#ifdef __UCLIBC_MJN3_ONLY__
+#warning devel checks
+#endif
+#define ENCODING (__ctype_encoding_7_bit)
+#ifdef __CTYPE_HAS_8_BIT_LOCALES
+#error __CTYPE_HAS_8_BIT_LOCALES is defined!
+#endif
+#ifdef __CTYPE_HAS_UTF_8_LOCALES
+#error __CTYPE_HAS_UTF_8_LOCALES is defined!
+#endif
+#endif
+
+#endif
+
 #if UINT_MAX == ULONG_MAX
 #undef atoi
 #undef abs
@@ -65,7 +103,11 @@
 #undef atoll
 #undef strtoll
 #undef strtoull
-#endif
+#undef wcstoll
+#undef wcstoull
+#endif /* __UCLIBC_HAS_WCHAR__ */
+
+/**********************************************************************/
 
 extern unsigned long
 _stdlib_strto_l(register const char * __restrict str,
@@ -77,6 +119,18 @@ _stdlib_strto_ll(register const char * __restrict str,
 				 char ** __restrict endptr, int base, int sflag);
 #endif
 
+#ifdef __UCLIBC_HAS_WCHAR__
+extern unsigned long
+_stdlib_wcsto_l(register const wchar_t * __restrict str,
+				wchar_t ** __restrict endptr, int base, int sflag);
+
+#if defined(ULLONG_MAX)
+extern unsigned long long
+_stdlib_wcsto_ll(register const wchar_t * __restrict str,
+				 wchar_t ** __restrict endptr, int base, int sflag);
+#endif
+#endif /* __UCLIBC_HAS_WCHAR__ */
+
 /**********************************************************************/
 #ifdef L_atof
 
@@ -205,6 +259,7 @@ long strtol(const char * __restrict str, char ** __restrict endptr, int base)
 #if (ULLONG_MAX == UINTMAX_MAX)
 strong_alias(strtoll,strtoimax)
 #endif
+strong_alias(strtoll,strtoq)
 
 long long strtoll(const char * __restrict str,
 				  char ** __restrict endptr, int base)
@@ -241,6 +296,7 @@ unsigned long strtoul(const char * __restrict str,
 #if (ULLONG_MAX == UINTMAX_MAX)
 strong_alias(strtoull,strtoumax)
 #endif
+strong_alias(strtoull,strtouq)
 
 unsigned long long strtoull(const char * __restrict str,
 							char ** __restrict endptr, int base)
@@ -271,28 +327,41 @@ unsigned long long strtoull(const char * __restrict str,
 #endif
 
 /**********************************************************************/
+#ifdef L__stdlib_wcsto_l
+#define L__stdlib_strto_l
+#endif
+
 #ifdef L__stdlib_strto_l
 
+#ifdef L__stdlib_wcsto_l
+#define _stdlib_strto_l _stdlib_wcsto_l
+#define Wchar wchar_t
+#define ISSPACE iswspace
+#else
+#define Wchar char
+#define ISSPACE isspace
+#endif
+
 /* This is the main work fuction which handles both strtol (sflag = 1) and
  * strtoul (sflag = 0). */
 
-unsigned long _stdlib_strto_l(register const char * __restrict str,
-							  char ** __restrict endptr, int base, int sflag)
+unsigned long _stdlib_strto_l(register const Wchar * __restrict str,
+							  Wchar ** __restrict endptr, int base, int sflag)
 {
     unsigned long number, cutoff;
 #if _STRTO_ENDPTR
-    const char *fail_char;
+    const Wchar *fail_char;
 #define SET_FAIL(X)       fail_char = (X)
 #else
 #define SET_FAIL(X)       ((void)(X)) /* Keep side effects. */
 #endif
     unsigned char negative, digit, cutoff_digit;
 
-	assert((sflag == 0) || (sflag == 1));
+	assert(((unsigned int)sflag) <= 1);
 
 	SET_FAIL(str);
 
-    while (isspace(*str)) {		/* Skip leading whitespace. */
+    while (ISSPACE(*str)) {		/* Skip leading whitespace. */
 		++str;
     }
 
@@ -350,7 +419,7 @@ unsigned long _stdlib_strto_l(register const char * __restrict str,
 
 #if _STRTO_ENDPTR
     if (endptr) {
-		*endptr = (char *) fail_char;
+		*endptr = (Wchar *) fail_char;
     }
 #endif
 
@@ -369,20 +438,33 @@ unsigned long _stdlib_strto_l(register const char * __restrict str,
 
 #endif
 /**********************************************************************/
+#ifdef L__stdlib_wcsto_ll
+#define L__stdlib_strto_ll
+#endif
+
 #ifdef L__stdlib_strto_ll
 
 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
 
+#ifdef L__stdlib_wcsto_ll
+#define _stdlib_strto_ll _stdlib_wcsto_ll
+#define Wchar wchar_t
+#define ISSPACE iswspace
+#else
+#define Wchar char
+#define ISSPACE isspace
+#endif
+
 /* This is the main work fuction which handles both strtoll (sflag = 1) and
  * strtoull (sflag = 0). */
 
-unsigned long long _stdlib_strto_ll(register const char * __restrict str,
-									char ** __restrict endptr, int base,
+unsigned long long _stdlib_strto_ll(register const Wchar * __restrict str,
+									Wchar ** __restrict endptr, int base,
 									int sflag)
 {
     unsigned long long number;
 #if _STRTO_ENDPTR
-    const char *fail_char;
+    const Wchar *fail_char;
 #define SET_FAIL(X)       fail_char = (X)
 #else
 #define SET_FAIL(X)       ((void)(X)) /* Keep side effects. */
@@ -390,11 +472,11 @@ unsigned long long _stdlib_strto_ll(register const char * __restrict str,
 	unsigned int n1;
     unsigned char negative, digit;
 
-	assert((sflag == 0) || (sflag == 1));
+	assert(((unsigned int)sflag) <= 1);
 
 	SET_FAIL(str);
 
-    while (isspace(*str)) {		/* Skip leading whitespace. */
+    while (ISSPACE(*str)) {		/* Skip leading whitespace. */
 		++str;
     }
 
@@ -461,7 +543,7 @@ unsigned long long _stdlib_strto_ll(register const char * __restrict str,
 
 #if _STRTO_ENDPTR
     if (endptr) {
-		*endptr = (char *) fail_char;
+		*endptr = (Wchar *) fail_char;
     }
 #endif
 
@@ -627,38 +709,6 @@ void ssort (void  *base,
 #endif
 
 #endif
-/**********************************************************************/
-/* Multibyte and wchar stuff follows. */
-
-#ifdef __UCLIBC_HAS_WCHAR__
-
-#include <locale.h>
-#include <wchar.h>
-
-/* TODO: clean up the following... */
-
-#if WCHAR_MAX > 0xffffUL
-#define UTF_8_MAX_LEN 6
-#else
-#define UTF_8_MAX_LEN 3
-#endif
-
-#ifdef __UCLIBC_HAS_LOCALE__
-#define ENCODING (__global_locale.encoding)
-#ifdef __UCLIBC_MJN3_ONLY__
-#warning implement __CTYPE_HAS_UTF_8_LOCALES!
-#endif
-#define __CTYPE_HAS_UTF_8_LOCALES
-#else
-#define ENCODING (__ctype_encoding_7_bit)
-#undef __CTYPE_HAS_8_BIT_LOCALES
-#undef __CTYPE_HAS_UTF_8_LOCALES
-#undef L__wchar_utf8sntowcs
-#undef L__wchar_wcsntoutf8s
-#endif
-
-#endif
-
 /**********************************************************************/
 #ifdef L__stdlib_mb_cur_max
 
@@ -774,4 +824,77 @@ size_t wcstombs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
 
 #endif
 /**********************************************************************/
+#ifdef L_wcstol
+
+#if ULONG_MAX == UINTMAX_MAX
+strong_alias(wcstol,wcstoimax)
+#endif
+
+#if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
+strong_alias(wcstol,wcstoll)
+#endif
+
+long wcstol(const wchar_t * __restrict str, wchar_t ** __restrict endptr, int base)
+{
+    return _stdlib_wcsto_l(str, endptr, base, 1);
+}
+
+#endif
+/**********************************************************************/
+#ifdef L_wcstoll
+
+#if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
+
+#if (ULLONG_MAX == UINTMAX_MAX)
+strong_alias(wcstoll,wcstoimax)
+#endif
+strong_alias(wcstoll,wcstoq)
+
+long long wcstoll(const wchar_t * __restrict str,
+				  wchar_t ** __restrict endptr, int base)
+{
+    return (long long) _stdlib_wcsto_ll(str, endptr, base, 1);
+}
+
+#endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
+
+#endif
+/**********************************************************************/
+#ifdef L_wcstoul
+
+#if ULONG_MAX == UINTMAX_MAX
+strong_alias(wcstoul,wcstoumax)
+#endif
+
+#if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
+strong_alias(wcstoul,wcstoull)
+#endif
+
+unsigned long wcstoul(const wchar_t * __restrict str,
+					  wchar_t ** __restrict endptr, int base)
+{
+    return _stdlib_wcsto_l(str, endptr, base, 0);
+}
+
+#endif
+/**********************************************************************/
+#ifdef L_wcstoull
+
+#if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
+
+#if (ULLONG_MAX == UINTMAX_MAX)
+strong_alias(wcstoull,wcstoumax)
+#endif
+strong_alias(wcstoull,wcstouq)
+
+unsigned long long wcstoull(const wchar_t * __restrict str,
+							wchar_t ** __restrict endptr, int base)
+{
+    return _stdlib_wcsto_ll(str, endptr, base, 0);
+}
+
+#endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
+
+#endif
+/**********************************************************************/
 

+ 1 - 1
libc/sysdeps/linux/common/bits/uClibc_locale.h

@@ -157,7 +157,7 @@ typedef struct {
 #ifdef __WCHAR_ENABLED
 	const unsigned char *tblwctype;
 	const unsigned char *tblwuplow;
-	const unsigned char *tblwcomb;
+/* 	const unsigned char *tblwcomb; */
 	const int16_t *tblwuplow_diff; /* yes... signed */
 	/* width?? */
 #endif /* __WCHAR_ENABLED */

+ 5 - 0
libc/sysdeps/linux/common/bits/uClibc_stdio.h

@@ -418,6 +418,11 @@ extern void __stdio_validate_FILE(FILE *stream);
 #define __stdio_validate_FILE(stream)		((void)0)
 #endif
 
+#ifdef __STDIO_WIDE
+extern size_t _wstdio_fwrite(const wchar_t *__restrict ws, size_t n,
+							 register FILE *__restrict stream);
+#endif
+
 /**********************************************************************
  * UTILITY functions
  **********************************************************************/