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 	<<toupper>>, <<toupper_l>>---translate characters to uppercase
32 
33 INDEX
34 	toupper
35 
36 INDEX
37 	toupper_l
38 
39 INDEX
40 	_toupper
41 
42 SYNOPSIS
43 	#include <ctype.h>
44 	int toupper(int <[c]>);
45 	int _toupper(int <[c]>);
46 
47 	#include <ctype.h>
48 	int toupper_l(int <[c]>, locale_t <[locale]>);
49 
50 
51 DESCRIPTION
52 <<toupper>> is a macro which converts lowercase characters to uppercase,
53 leaving all other characters unchanged.  It is only defined when
54 <[c]> is an integer in the range <<EOF>> to <<255>>.
55 
56 <<toupper_l>> is like <<toupper>> 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 toupper>>' or `<<#undef toupper_l>>'.
62 
63 <<_toupper>> performs the same conversion as <<toupper>>, but should
64 only be used when <[c]> is known to be a lowercase character (<<a>>--<<z>>).
65 
66 RETURNS
67 <<toupper>>, <<toupper_l>> return the uppercase equivalent of <[c]> when
68 <[c]> is a lowercase character, and <[c]> otherwise.
69 
70 <<_toupper>> returns the uppercase 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 <<_toupper>> is undefined.
73 
74 PORTABILITY
75 <<toupper>> is ANSI C.  <<_toupper>> is not recommended for portable programs.
76 <<toupper_l>> is POSIX-1.2008.
77 
78 No supporting OS subroutines are required.
79 */
80 
81 #include <ctype.h>
82 #if defined (_MB_EXTENDED_CHARSETS_ISO) || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
83 #include <limits.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <wctype.h>
87 #include <wchar.h>
88 #endif
89 
90 #undef toupper
91 int
toupper(int c)92 toupper (int c)
93 {
94 #if defined (_MB_EXTENDED_CHARSETS_ISO) || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
95   if ((unsigned char) c <= 0x7f)
96     return islower (c) ? c - 'a' + 'A' : c;
97   else if (c != EOF && MB_CUR_MAX == 1 && islower (c))
98     {
99       char s[MB_LEN_MAX] = { c, '\0' };
100       wchar_t wc;
101       if (mbtowc (&wc, s, 1) >= 0
102 	  && wctomb (s, (wchar_t) towupper ((wint_t) wc)) == 1)
103 	c = (unsigned char) s[0];
104     }
105   return c;
106 #else
107   return islower (c) ? c - 'a' + 'A' : c;
108 #endif
109 }
110