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 <_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 toupper
92 int
toupper(int c)93 toupper (int c)
94 {
95 #if defined (_MB_EXTENDED_CHARSETS_ISO) || defined (_MB_EXTENDED_CHARSETS_WINDOWS)
96 if ((unsigned char) c <= 0x7f)
97 return islower (c) ? c - 'a' + 'A' : c;
98 else if (c != EOF && MB_CUR_MAX == 1 && islower (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) towupper ((wint_t) wc)) == 1)
104 c = (unsigned char) s[0];
105 }
106 return c;
107 #else
108 return islower (c) ? c - 'a' + 'A' : c;
109 #endif
110 }
111