- /*
- * thread_multi_locale1.c - Per-thread locale demo
- *
- * Written by Roland Mainz <roland.mainz@nrubsig.org>
- */
- #define _XOPEN_SOURCE 700
- #include <stdlib.h>
- #include <stdio.h>
- #include <locale.h>
- #ifdef __GLIBC__
- /* { BSD, MacOSX, Illumos, ... } do not need this! */
- #define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
- #endif
- #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
- #include <stdarg.h>
- #include <errno.h>
- #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
- #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
- /* cursed glibc does not provide |printf_l()| */
- int vprintf_l(locale_t loc, const char *format, va_list ap)
- {
- locale_t prev_loc;
- int ret;
- int saved_errno;
- prev_loc = uselocale(loc);
- saved_errno = errno;
- (void)uselocale(prev_loc);
- errno = saved_errno;
- return ret;
- }
- int printf_l(locale_t loc, const char *format, ...)
- {
- va_list args;
- int res;
- res = vprintf_l(loc, format, args);
- return res;
- }
- #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
- int main(int ac, char *av[])
- {
- locale_t my_en_l;
- locale_t my_de_l;
- double x;
- my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
- my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
- if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) {
- return EXIT_FAILURE;
- }
- x = 123456.789123456789;
- (void)printf_l(my_en_l, "%8.8f\n", x);
- (void)printf_l(my_de_l, "%8.8f\n", x);
- /*
- * If we use |uselocale()| we must set the current thread
- * locale to something else before freeing the current
- * locale_t object!!
- */
- freelocale(my_de_l);
- freelocale(my_en_l);
- return EXIT_SUCCESS;
- }
thread_locale1.c - Per-thread locale demo
Posted by Anonymous on Tue 31st Jan 2023 12:43
raw | new post
view followups (newest first): thread_locale1.c - Per-thread locale demo by Anonymous
modification of post by Anonymous (view diff)
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.