path: root/src/stdio/fputws.c
diff options
authorRich Felker <>2015-06-16 05:35:31 +0000
committerRich Felker <>2015-06-16 06:10:29 +0000
commit16f18d036d9a7bf590ee6eb86785c0a9658220b6 (patch)
treedeb18b50c750e47dce2e0de0061f6de93cb11957 /src/stdio/fputws.c
parent1507ebf837334e9e07cfab1ca1c2e88449069a80 (diff)
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/fputws.c')
1 files changed, 5 insertions, 0 deletions
diff --git a/src/stdio/fputws.c b/src/stdio/fputws.c
index 317d65f1..0ed02f1c 100644
--- a/src/stdio/fputws.c
+++ b/src/stdio/fputws.c
@@ -1,23 +1,28 @@
#include "stdio_impl.h"
+#include "locale_impl.h"
#include <wchar.h>
int fputws(const wchar_t *restrict ws, FILE *restrict f)
unsigned char buf[BUFSIZ];
size_t l=0;
+ locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
fwide(f, 1);
+ *ploc = f->locale;
while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1)
if (__fwritex(buf, l, f) < l) {
+ *ploc = loc;
return -1;
+ *ploc = loc;
return l; /* 0 or -1 */