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) || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
9 #include <limits.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <wctype.h>
13 #include <wchar.h>
14 #include "../locale/setlocale.h"
15 #endif
16 
17 int
tolower_l(int c,struct __locale_t * locale)18 tolower_l (int c, struct __locale_t *locale)
19 {
20 #if defined (_MB_EXTENDED_CHARSETS_ISO) || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
21   if ((unsigned char) c <= 0x7f)
22     return isupper_l (c, locale) ? c - 'A' + 'a' : c;
23   else if (c != EOF && __locale_mb_cur_max_l (locale) == 1
24 	   && isupper_l (c, locale))
25     {
26       char s[MB_LEN_MAX] = { c, '\0' };
27       wchar_t wc;
28       mbstate_t state;
29 
30       memset (&state, 0, sizeof state);
31       if (locale->mbtowc (_REENT, &wc, s, 1, &state) >= 0
32 	  && locale->wctomb (_REENT, s,
33 			     (wchar_t) towlower_l ((wint_t) wc, locale),
34 			     &state) == 1)
35 	c = (unsigned char) s[0];
36     }
37   return c;
38 #else
39   return isupper_l (c, locale) ? (c) - 'A' + 'a' : c;
40 #endif
41 }
42