1 /*
2 Copyright (c) 2016 Corinna Vinschen <corinna@vinschen.de>
3 Modified (m) 2017 Thomas Wolff: revise Unicode and locale/wchar handling
4  */
5 #define _DEFAULT_SOURCE
6 #include <_ansi.h>
7 #include <ctype.h>
8 #if defined (_MB_EXTENDED_CHARSETS_ISO) \
9     || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
10 #include <limits.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <wctype.h>
14 #include <wchar.h>
15 #include <../locale/setlocale.h>
16 #endif
17 
18 int
toupper_l(int c,struct __locale_t * locale)19 toupper_l (int c, struct __locale_t *locale)
20 {
21 #if defined (_MB_EXTENDED_CHARSETS_ISO) \
22     || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
23   if ((unsigned char) c <= 0x7f)
24     return islower_l (c, locale) ? c - 'a' + 'A' : c;
25   else if (c != EOF && __locale_mb_cur_max_l (locale) == 1
26 	   && islower_l (c, locale))
27     {
28       char s[MB_LEN_MAX] = { c, '\0' };
29       wchar_t wc;
30       mbstate_t state;
31 
32       memset (&state, 0, sizeof state);
33       if (locale->mbtowc (_REENT, &wc, s, 1, &state) >= 0
34 	  && locale->wctomb (_REENT, s,
35 			     (wchar_t) towupper_l ((wint_t) wc, locale),
36 			     &state) == 1)
37 	c = (unsigned char) s[0];
38     }
39   return c;
40 #else
41   return islower_l (c, locale) ? c - 'a' + 'A' : c;
42 #endif
43 }
44