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