about summary refs log tree commit diff
path: root/src/stdio/fputwc.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2015-06-16 05:35:31 +0000
committerRich Felker <dalias@aerifal.cx>2015-06-16 06:10:29 +0000
commit16f18d036d9a7bf590ee6eb86785c0a9658220b6 (patch)
treedeb18b50c750e47dce2e0de0061f6de93cb11957 /src/stdio/fputwc.c
parent1507ebf837334e9e07cfab1ca1c2e88449069a80 (diff)
downloadmusl-16f18d036d9a7bf590ee6eb86785c0a9658220b6.tar.gz
musl-16f18d036d9a7bf590ee6eb86785c0a9658220b6.tar.xz
musl-16f18d036d9a7bf590ee6eb86785c0a9658220b6.zip
byte-based C locale, phase 2: stdio and iconv (multibyte callers)
this patch adjusts libc components which use the multibyte functions
internally, and which depend on them operating in a particular
encoding, to make the appropriate locale changes before calling them
and restore the calling thread's locale afterwards. activating the
byte-based C locale without these changes would cause regressions in
stdio and iconv.

in the case of iconv, the current implementation was simply using the
multibyte functions as UTF-8 conversions. setting a multibyte UTF-8
locale for the duration of the iconv operation allows the code to
continue working.

in the case of stdio, POSIX requires that FILE streams have an
encoding rule bound at the time of setting wide orientation. as long
as all locales, including the C locale, used the same encoding,
treating high bytes as UTF-8, there was no need to store an encoding
rule as part of the stream's state.

a new locale field in the FILE structure points to the locale that
should be made active during fgetwc/fputwc/ungetwc on the stream. it
cannot point to the locale active at the time the stream becomes
oriented, because this locale could be mutable (the global locale) or
could be destroyed (locale_t objects produced by newlocale) before the
stream is closed. instead, a pointer to the static C or C.UTF-8 locale
object added in commit commit aeeac9ca5490d7d90fe061ab72da446c01ddf746
is used. this is valid since categories other than LC_CTYPE will not
affect these functions.
Diffstat (limited to 'src/stdio/fputwc.c')
-rw-r--r--src/stdio/fputwc.c5
1 files changed, 5 insertions, 0 deletions
diff --git a/src/stdio/fputwc.c b/src/stdio/fputwc.c
index 1bf165bf..789fe9c9 100644
--- a/src/stdio/fputwc.c
+++ b/src/stdio/fputwc.c
@@ -1,4 +1,5 @@
 #include "stdio_impl.h"
+#include "locale_impl.h"
 #include <wchar.h>
 #include <limits.h>
 #include <ctype.h>
@@ -7,8 +8,10 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
 {
 	char mbc[MB_LEN_MAX];
 	int l;
+	locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
 
 	if (f->mode <= 0) fwide(f, 1);
+	*ploc = f->locale;
 
 	if (isascii(c)) {
 		c = putc_unlocked(c, f);
@@ -20,6 +23,8 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
 		l = wctomb(mbc, c);
 		if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF;
 	}
+	if (c==WEOF) f->flags |= F_ERR;
+	*ploc = loc;
 	return c;
 }