pastebin - collaborative debugging tool
rovema.kpaste.net RSS


thread_locale1.c - Per-thread locale demo
Posted by Anonymous on Tue 31st Jan 2023 08:25
raw | new post
view followups (newest first): thread_locale1.c - Per-thread locale demo by Anonymous

  1. /*
  2.  * thread_locale1.c - Per-thread locale demo
  3.  *
  4.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  5.  */
  6.  
  7. #define _XOPEN_SOURCE 700
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <locale.h>
  12.  
  13. #ifdef __GLIBC__
  14. /* { BSD, MacOSX, Illumos, ... } do not need this! */
  15. #define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  16. #endif
  17.  
  18. #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  19. #include <stdarg.h>
  20. #include <errno.h>
  21. #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  22.  
  23. #ifdef CURSED_GLI/*
  24.  * thread_locale1.c - Per-thread locale demo
  25.  *
  26.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  27.  */
  28.  
  29. #define _XOPEN_SOURCE 700
  30.  
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <locale.h>
  34.  
  35. #ifdef __GLIBC__
  36. /* { BSD, MacOSX, Illumos, ... } do not need this! */
  37. #define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  38. #endif
  39.  
  40. #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  41. #include <stdarg.h>
  42. #include <errno.h>
  43. #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  44.  
  45. #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  46. /* cursed glibc does not provide |printf_l()| */
  47. int vprintf_l(locale_t loc, const char *format, va_list ap)
  48. {
  49.         int ret;
  50.         int saved_errno;
  51.         locale_t prev_loc;
  52.  
  53.         prev_loc = uselocale(loc);
  54.        
  55.         ret = vprintf(format, ap);
  56.         saved_errno = errno;
  57.  
  58.         (void)uselocale(prev_loc);
  59.  
  60.         errno = saved_errno;
  61.         return ret;
  62. }
  63.  
  64. int printf_l(locale_t loc, const char *format, ...)
  65. {
  66.         int res;
  67.  
  68.         va_list args;
  69.         va_start(args, format);
  70.         res = vprintf_l(loc, format, args);
  71.         va_end(args);
  72.  
  73.         return res;
  74. }
  75. #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  76.  
  77.  
  78. int main(int ac, char *av[])
  79. {
  80.         locale_t my_en_l;
  81.         locale_t my_de_l;
  82.         double x;
  83.        
  84.         my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
  85.         my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
  86.         /*
  87.  * thread_locale1.c - Per-thread locale demo
  88.  *
  89.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  90.  */
  91.  
  92. #define _XOPEN_SOURCE 700
  93.  
  94. #include <stdlib.h>
  95. #include <stdio.h>
  96. #include <locale.h>
  97.  
  98. #ifdef __GLIBC__
  99. /* { BSD, MacOSX, Illumos, ... } do not need this! */
  100. #define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  101. #endif
  102.  
  103. #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  104. #include <stdarg.h>
  105. #include <errno.h>
  106. #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  107.  
  108. #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  109. /* cursed glibc does not provide |printf_l()| */
  110. int vprintf_l(locale_t loc, const char *format, va_list ap)
  111. {
  112.         int ret;
  113.         int saved_errno;
  114.         locale_t prev_loc;
  115.  
  116.         prev_loc = uselocale(loc);
  117.        
  118.         ret = vprintf(format, ap);
  119.         saved_errno = errno;
  120.  
  121.         (void)uselocale(prev_loc);
  122.  
  123.         errno = saved_errno;
  124.         return ret;
  125. }
  126.  
  127. int printf_l(locale_t loc, const char *format, ...)
  128. {
  129.         int res;
  130.  
  131.         va_list args;
  132.         va_start(args, format);
  133.         res = vprintf_l(loc, format, args);
  134.         va_end(args);
  135.  
  136.         return res;
  137. }
  138. #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  139.  
  140.  
  141. int main(int ac, char *av[])
  142. {
  143.         locale_t my_en_l;
  144.         locale_t my_de_l;
  145.         double x;
  146.        
  147.         my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
  148.         my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
  149.        
  150.         if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) {
  151.                 perror("Could not create locale object");
  152.                 return EXIT_FAILURE;
  153.         }
  154.  
  155.         x = 123456.789123456789;
  156.         (void)printf_l(my_en_l, "%8.3f\n", x);
  157.         (void)printf_l(my_de_l, "%8.3f\n", x);
  158.  
  159.         /* Use global locale again, to prevent use-after-free */
  160.         (void)uselocale(LC_GLOBAL_LOCALE);
  161.         freelocale(my_de_l);
  162.         freelocale(my_en_l);
  163.  
  164.         return EXIT_SUCCESS;
  165. }
  166.  
  167.  
  168.         if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) {
  169.                 perror("Could not create locale object");
  170.                 return EXIT_FAILURE;
  171.         }
  172.  
  173.         x = 123456.789123456789;
  174.         (void)printf_l(my_en_l, "%8.3f\n", x);
  175.         (void)printf_l(my_de_l, "%8.3f\n", x);
  176.  
  177.         /* Use global locale again, to prevent use-after-free */
  178.         (void)uselocale(LC_GLOBAL_LOCALE);
  179.         freelocale(my_de_l);
  180.         freelocale(my_en_l);
  181.  
  182.         return EXIT_SUCCESS;
  183. }
  184.  
  185. BC_DOES_NOT_HAVE_PRINTF_L
  186. /* cursed glibc does not provide |printf_l()| */
  187. int vprintf_l(locale_t loc, const char *format, va_list ap)
  188. {
  189.         int ret;
  190.         int saved_errno;
  191.         locale_t prev_loc;
  192.  
  193.         prev_loc = uselocale(loc);
  194.        
  195.         ret = vprintf(format, ap);
  196.         saved_errno = errno;
  197.  
  198.         (void)uselocale(prev_loc);
  199. /*
  200.  * thread_locale1.c - Per-thread locale demo
  201.  *
  202.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  203.  */
  204.  
  205. #define _XOPEN_SOURCE 700
  206.  
  207. #include <stdlib.h>
  208. #include <stdio.h>
  209. #include <locale.h>
  210.  
  211. #ifdef __GLIBC__
  212. /* { BSD, MacOSX, Illumos, ... } do not need this! */
  213. #define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  214. #endif
  215.  
  216. #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  217. #include <stdarg.h>
  218. #include <errno.h>
  219. #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  220.  
  221. #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
  222. /* cursed glibc does not provide |printf_l()| */
  223. int vprintf_l(locale_t loc, const char *format, va_list ap)
  224. {
  225.         int ret;
  226.         int saved_errno;
  227.         locale_t prev_loc;
  228.  
  229.         prev_loc = uselocale(loc);
  230.        
  231.         ret = vprintf(format, ap);
  232.         saved_errno = errno;
  233.  
  234.         (void)uselocale(prev_loc);
  235.  
  236.         errno = saved_errno;
  237.         return ret;
  238. }
  239.  
  240. int printf_l(locale_t loc, const char *format, ...)
  241. {
  242.         int res;
  243.  
  244.         va_list args;
  245.         va_start(args, format);
  246.         res = vprintf_l(loc, format, args);
  247.         va_end(args);
  248.  
  249.         return res;
  250. }
  251. #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  252.  
  253.  
  254. int main(int ac, char *av[])
  255. {
  256.         locale_t my_en_l;
  257.         locale_t my_de_l;
  258.         double x;
  259.        
  260.         my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
  261.         my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
  262.        
  263.         if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) {
  264.                 perror("Could not create locale object");
  265.                 return EXIT_FAILURE;
  266.         }
  267.  
  268.         x = 123456.789123456789;
  269.         (void)printf_l(my_en_l, "%8.3f\n", x);
  270.         (void)printf_l(my_de_l, "%8.3f\n", x);
  271.  
  272.         /* Use global locale again, to prevent use-after-free */
  273.         (void)uselocale(LC_GLOBAL_LOCALE);
  274.         freelocale(my_de_l);
  275.         freelocale(my_en_l);
  276.  
  277.         return EXIT_SUCCESS;
  278. }
  279.  
  280.  
  281.         errno = saved_errno;
  282.         return ret;
  283. }
  284.  
  285. int printf_l(locale_t loc, const char *format, ...)
  286. {
  287.         int res;
  288.  
  289.         va_list args;
  290.         va_start(args, format);
  291.         res = vprintf_l(loc, format, args);
  292.         va_end(args);
  293.  
  294.         return res;
  295. }
  296. #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
  297.  
  298.  
  299. int main(int ac, char *av[])
  300. {
  301.         locale_t my_en_l;
  302.         locale_t my_de_l;
  303.         double x;
  304.        
  305.         my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
  306.         my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
  307.        
  308.         if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) {
  309.                 perror("Could not create locale object");
  310.                 return EXIT_FAILURE;
  311.         }
  312.  
  313.         x = 123456.789123456789;
  314.         (void)printf_l(my_en_l, "%8.3f\n", x);
  315.         (void)printf_l(my_de_l, "%8.3f\n", x);
  316.  
  317.         /* Use global locale again, to prevent use-after-free */
  318.         (void)uselocale(LC_GLOBAL_LOCALE);
  319.         freelocale(my_de_l);
  320.         freelocale(my_en_l);
  321.  
  322.         return EXIT_SUCCESS;
  323. }

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at