1 /*
2 Copyright (c) 1989 The Regents of the University of California.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of the University nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 SUCH DAMAGE.
28  */
29 /*
30 FUNCTION
31 	<<tolower>>, <<tolower_l>>---translate characters to lowercase
32 
33 INDEX
34 	tolower
35 
36 INDEX
37 	tolower_l
38 
39 INDEX
40 	_tolower
41 
42 SYNOPSIS
43 	#include <ctype.h>
44 	int tolower(int <[c]>);
45 	int _tolower(int <[c]>);
46 
47 	#include <ctype.h>
48 	int tolower_l(int <[c]>, locale_t <[locale]>);
49 
50 
51 DESCRIPTION
52 <<tolower>> is a macro which converts uppercase characters to lowercase,
53 leaving all other characters unchanged.  It is only defined when
54 <[c]> is an integer in the range <<EOF>> to <<255>>.
55 
56 <<tolower_l>> is like <<tolower>> but performs the function based on the
57 locale specified by the locale object locale.  If <[locale]> is
58 LC_GLOBAL_LOCALE or not a valid locale object, the behaviour is undefined.
59 
60 You can use a compiled subroutine instead of the macro definition by
61 undefining this macro using `<<#undef tolower>>' or `<<#undef tolower_l>>'.
62 
63 <<_tolower>> performs the same conversion as <<tolower>>, but should
64 only be used when <[c]> is known to be an uppercase character (<<A>>--<<Z>>).
65 
66 RETURNS
67 <<tolower>>, <<tolower_l>> return the lowercase equivalent of <[c]> when
68 <[c]> is an uppercase character, and <[c]> otherwise.
69 
70 <<_tolower>> returns the lowercase equivalent of <[c]> when it is a
71 character between <<A>> and <<Z>>.  If <[c]> is not one of these
72 characters, the behaviour of <<_tolower>> is undefined.
73 
74 PORTABILITY
75 <<tolower>> is ANSI C.  <<_tolower>> is not recommended for portable programs.
76 <<tolower_l>> is POSIX-1.2008.
77 
78 No supporting OS subroutines are required.
79 */
80 
81 #include <_ansi.h>
82 #include <ctype.h>
83 #if defined (_MB_EXTENDED_CHARSETS_ISO) || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
84 #include <limits.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <wctype.h>
88 #include <wchar.h>
89 #endif
90 
91 #undef tolower
92 int
tolower(int c)93 tolower (int c)
94 {
95 #if defined (_MB_EXTENDED_CHARSETS_ISO) || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
96   if ((unsigned char) c <= 0x7f)
97     return isupper (c) ? c - 'A' + 'a' : c;
98   else if (c != EOF && MB_CUR_MAX == 1 && isupper (c))
99     {
100       char s[MB_LEN_MAX] = { c, '\0' };
101       wchar_t wc;
102       if (mbtowc (&wc, s, 1) >= 0
103 	  && wctomb (s, (wchar_t) towlower ((wint_t) wc)) == 1)
104 	c = (unsigned char) s[0];
105     }
106   return c;
107 #else
108   return isupper(c) ? (c) - 'A' + 'a' : c;
109 #endif
110 }
111