From 2ab07033ea7c92c7f2f0bbc465d6729e1f2dcfbc Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Sun, 16 Nov 2025 09:36:03 +0200 Subject: [PATCH] Workaround for MSVCRT stdio on MS-Windows for CJK locales MSVCRT implementation of stdio functions which output characters one by one fails for CJK double-byte encodings. Gnulib provides replacement functions which work around those bugs. This change makes Emacs and lib-src programs use the replacements when Emacs is linked against MSVCRT. * src/conf_post.h (fwrite, fprintf, printf, vfprintf, vprintf) [WINDOWSNT]: Redirect to Gnulib replacements when Emacs is linked against MSVCRT (as opposed to UCRT). Suggested by Bruno Haible . --- src/conf_post.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/conf_post.h b/src/conf_post.h index aaf4fb59ffb..e946f6fc90b 100644 --- a/src/conf_post.h +++ b/src/conf_post.h @@ -396,3 +396,41 @@ extern int emacs_setenv_TZ (char const *); : S_ISCHR (mode) ? DT_CHR : S_ISFIFO (mode) ? DT_FIFO \ : S_ISSOCK (mode) ? DT_SOCK : DT_UNKNOWN) #endif /* MSDOS */ + +#if defined WINDOWSNT +# if !defined _UCRT +# include +# include +# include + +/* Workarounds for MSVCRT bugs. + + The functions below are in Gnulib, but their prototypes and + redirections must be here because the MS-Windows build omits the + Gnulib stdio-h module, which does the below in Gnulib's stdio.h + file, which is not used by the MS-Windows build. */ + +extern size_t gl_consolesafe_fwrite (const void *ptr, size_t size, + size_t nmemb, FILE *fp) + ARG_NONNULL ((1, 4)); +extern int gl_consolesafe_fprintf (FILE *restrict fp, + const char *restrict format, ...) + ATTRIBUTE_FORMAT_PRINTF (2, 3) + ARG_NONNULL ((1, 2)); +extern int gl_consolesafe_printf (const char *restrict format, ...) + ATTRIBUTE_FORMAT_PRINTF (1, 2) + ARG_NONNULL ((1)); +extern int gl_consolesafe_vfprintf (FILE *restrict fp, + const char *restrict format, va_list args) + ATTRIBUTE_FORMAT_PRINTF (2, 0) + ARG_NONNULL ((1, 2)); +extern int gl_consolesafe_vprintf (const char *restrict format, va_list args) + ATTRIBUTE_FORMAT_PRINTF (1, 0) + ARG_NONNULL ((1)); +# define fwrite gl_consolesafe_fwrite +# define fprintf gl_consolesafe_fprintf +# define printf gl_consolesafe_printf +# define vfprintf gl_consolesafe_vfprintf +# define vprintf gl_consolesafe_vprintf +# endif /* !_UCRT */ +#endif /* WINDOWSNT */