1 /*
2 Copyright (c) 1991, 1993
3 The Regents of the University of California.  All rights reserved.
4 c) UNIX System Laboratories, Inc.
5 All or some portions of this file are derived from material licensed
6 to the University of California by American Telephone and Telegraph
7 Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 the permission of UNIX System Laboratories, Inc.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13 1. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 3. Neither the name of the University nor the names of its contributors
19 may be used to endorse or promote products derived from this software
20 without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 SUCH DAMAGE.
33  */
34 #ifndef _CTYPE_H_
35 #define _CTYPE_H_
36 
37 #include "_ansi.h"
38 #include <sys/cdefs.h>
39 #include <limits.h>
40 
41 #if __POSIX_VISIBLE >= 200809 || __MISC_VISIBLE || defined (_LIBC)
42 #include <sys/_locale.h>
43 #endif
44 
45 _BEGIN_STD_C
46 
47 int isalnum (int __c);
48 int isalpha (int __c);
49 int iscntrl (int __c);
50 int isdigit (int __c);
51 int isgraph (int __c);
52 int islower (int __c);
53 int isprint (int __c);
54 int ispunct (int __c);
55 int isspace (int __c);
56 int isupper (int __c);
57 int isxdigit (int __c);
58 int tolower (int __c);
59 int toupper (int __c);
60 
61 #if  __ISO_C_VISIBLE >= 1999
62 #ifdef _DEFINING_ISBLANK
63 int isblank(int c);
64 #else
isblank(int c)65 static __inline int isblank(int c) {
66 	return c == ' ' || c == '\t';
67 }
68 #endif
69 #endif
70 
71 #if __MISC_VISIBLE || __XSI_VISIBLE
72 int isascii (int __c);
73 int toascii (int __c);
74 #define _tolower(__c) ((unsigned char)(__c) - 'A' + 'a')
75 #define _toupper(__c) ((unsigned char)(__c) - 'a' + 'A')
76 #endif
77 
78 #if __POSIX_VISIBLE >= 200809
79 extern int isalnum_l (int __c, locale_t __l);
80 extern int isalpha_l (int __c, locale_t __l);
81 extern int isblank_l (int __c, locale_t __l);
82 extern int iscntrl_l (int __c, locale_t __l);
83 extern int isdigit_l (int __c, locale_t __l);
84 extern int isgraph_l (int __c, locale_t __l);
85 extern int islower_l (int __c, locale_t __l);
86 extern int isprint_l (int __c, locale_t __l);
87 extern int ispunct_l (int __c, locale_t __l);
88 extern int isspace_l (int __c, locale_t __l);
89 extern int isupper_l (int __c, locale_t __l);
90 extern int isxdigit_l(int __c, locale_t __l);
91 extern int tolower_l (int __c, locale_t __l);
92 extern int toupper_l (int __c, locale_t __l);
93 #endif
94 
95 #if __MISC_VISIBLE
96 extern int isascii_l (int __c, locale_t __l);
97 extern int toascii_l (int __c, locale_t __l);
98 #endif
99 
100 #define	_U	01
101 #define	_L	02
102 #define	_N	04
103 #define	_S	010
104 #define _P	020
105 #define _C	040
106 #define _X	0100
107 #define	_B	0200
108 
109 #if CHAR_MIN == SCHAR_MIN
110 #define ALLOW_NEGATIVE_CTYPE_INDEX
111 #endif
112 
113 #if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
114 extern	__IMPORT const char	_ctype_b[];
115 #define _ctype_ (_ctype_b + 127)
116 #else
117 extern	__IMPORT const char	_ctype_[];
118 #endif
119 
120 #ifdef __HAVE_LOCALE_INFO__
121 const char *__locale_ctype_ptr (void);
122 #else
123 #define __locale_ctype_ptr()	_ctype_
124 #endif
125 
126 # define __CTYPE_PTR	(__locale_ctype_ptr ())
127 
128 #ifndef __cplusplus
__ctype_lookup(int c)129 static __inline char __ctype_lookup(int c) { return (__CTYPE_PTR + 1)[c]; }
130 
131 #define	isalpha(__c)	(__ctype_lookup(__c)&(_U|_L))
132 #define	isupper(__c)	((__ctype_lookup(__c)&(_U|_L))==_U)
133 #define	islower(__c)	((__ctype_lookup(__c)&(_U|_L))==_L)
134 #define	isdigit(__c)	(__ctype_lookup(__c)&_N)
135 #define	isxdigit(__c)	(__ctype_lookup(__c)&(_X|_N))
136 #define	isspace(__c)	(__ctype_lookup(__c)&_S)
137 #define ispunct(__c)	(__ctype_lookup(__c)&_P)
138 #define isalnum(__c)	(__ctype_lookup(__c)&(_U|_L|_N))
139 #define isprint(__c)	(__ctype_lookup(__c)&(_P|_U|_L|_N|_B))
140 #define	isgraph(__c)	(__ctype_lookup(__c)&(_P|_U|_L|_N))
141 #define iscntrl(__c)	(__ctype_lookup(__c)&_C)
142 
143 #if __POSIX_VISIBLE >= 200809
144 #ifdef __HAVE_LOCALE_INFO__
145 const char *__locale_ctype_ptr_l (locale_t);
146 #else
147 static __inline const char *
__locale_ctype_ptr_l(locale_t _l)148 __locale_ctype_ptr_l(locale_t _l)
149 {
150 	(void)_l;
151 	return __locale_ctype_ptr();
152 }
153 #endif
154 
__ctype_lookup_l(int c,locale_t l)155 static __inline char __ctype_lookup_l(int c, locale_t l) {
156 	return (__locale_ctype_ptr_l(l)+1)[(int)(c)];
157 }
158 
159 #define	isalpha_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_U|_L))
160 #define	isupper_l(__c,__l)	((__ctype_lookup_l(__c,__l)&(_U|_L))==_U)
161 #define	islower_l(__c,__l)	((__ctype_lookup_l(__c,__l)&(_U|_L))==_L)
162 #define	isdigit_l(__c,__l)	(__ctype_lookup_l(__c,__l)&_N)
163 #define	isxdigit_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_X|_N))
164 #define	isspace_l(__c,__l)	(__ctype_lookup_l(__c,__l)&_S)
165 #define ispunct_l(__c,__l)	(__ctype_lookup_l(__c,__l)&_P)
166 #define isalnum_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_U|_L|_N))
167 #define isprint_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_P|_U|_L|_N|_B))
168 #define	isgraph_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_P|_U|_L|_N))
169 #define iscntrl_l(__c,__l)	(__ctype_lookup_l(__c,__l)&_C)
170 
171 #if defined(__GNUC__)
172 #define isblank_l(__c, __l) \
173   __extension__ ({ __typeof__ (__c) __x = (__c);		\
174         (__ctype_lookup_l(__x,__l)&_B) || (int) (__x) == '\t';})
175 #endif
176 
177 #endif /* __POSIX_VISIBLE >= 200809 */
178 
179 #if __MISC_VISIBLE || __XSI_VISIBLE
180 #define isascii(__c)	((unsigned)(__c)<=0177)
181 #define toascii(__c)	((__c)&0177)
182 #endif
183 
184 #if __MISC_VISIBLE
185 #define isascii_l(__c,__l)	((__l),(unsigned)(__c)<=0177)
186 #define toascii_l(__c,__l)	((__l),(__c)&0177)
187 #endif
188 
189 /* Non-gcc versions will get the library versions, and will be
190    slightly slower.  These macros are not NLS-aware so they are
191    disabled if the system supports the extended character sets. */
192 # if defined(__GNUC__)
193 #  if !defined (_MB_EXTENDED_CHARSETS_ISO) && !defined (_MB_EXTENDED_CHARSETS_WINDOWS)
194 #   define toupper(__c) \
195   __extension__ ({ __typeof__ (__c) __x = (__c);	\
196       islower (__x) ? (int) __x - 'a' + 'A' : (int) __x;})
197 #   define tolower(__c) \
198   __extension__ ({ __typeof__ (__c) __x = (__c);	\
199       isupper (__x) ? (int) __x - 'A' + 'a' : (int) __x;})
200 #  else /* _MB_EXTENDED_CHARSETS* */
201 /* Allow a gcc warning if the user passed 'char', but defer to the
202    function.  */
203 #   define toupper(__c) \
204   __extension__ ({ __typeof__ (__c) __x = (__c);	\
205       (void) __CTYPE_PTR[__x]; (toupper) (__x);})
206 #   define tolower(__c) \
207   __extension__ ({ __typeof__ (__c) __x = (__c);	\
208       (void) __CTYPE_PTR[__x]; (tolower) (__x);})
209 #  endif /* _MB_EXTENDED_CHARSETS* */
210 # endif /* __GNUC__ */
211 
212 #if __POSIX_VISIBLE >= 200809
213 #endif /* __POSIX_VISIBLE >= 200809 */
214 
215 #endif /* !__cplusplus */
216 
217 _END_STD_C
218 
219 #endif /* _CTYPE_H_ */
220