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 <sys/cdefs.h>
38 
39 #if __POSIX_VISIBLE >= 200809 || __MISC_VISIBLE
40 #include <sys/_locale.h>
41 #endif
42 
43 _BEGIN_STD_C
44 
45 /*
46  * The small ctype code does not support locales or extended character sets. It also breaks
47  * libstdc++'s ctype implementation, so just skip it for c++
48  */
49 #if defined(__HAVE_LOCALE_INFO__) || \
50     defined (_MB_EXTENDED_CHARSETS_ISO) || \
51     defined (_MB_EXTENDED_CHARSETS_WINDOWS) || \
52     defined (__cplusplus)
53 #undef _PICOLIBC_CTYPE_SMALL
54 #define _PICOLIBC_CTYPE_SMALL 0
55 #endif
56 
57 /*
58  * The default ctype style depends upon whether we are optimizing for
59  * size and whether the library supports locale-specific ctype data
60  */
61 #if !defined(_PICOLIBC_CTYPE_SMALL)
62 #ifdef __OPTIMIZE_SIZE__
63 #define _PICOLIBC_CTYPE_SMALL 1
64 #else
65 #define _PICOLIBC_CTYPE_SMALL 0
66 #endif
67 #endif
68 
69 int isalnum (int c);
70 int isalpha (int c);
71 int iscntrl (int c);
72 int isdigit (int c);
73 int isgraph (int c);
74 int islower (int c);
75 int isprint (int c);
76 int ispunct (int c);
77 int isspace (int c);
78 int isupper (int c);
79 int isxdigit (int c);
80 int tolower (int c);
81 int toupper (int c);
82 
83 #if __ISO_C_VISIBLE >= 1999
84 int isblank (int c);
85 #endif
86 
87 #if __MISC_VISIBLE || __XSI_VISIBLE
88 int isascii (int c);
89 int toascii (int c);
90 #define _tolower(__c) ((__c) + ('a' - 'A'))
91 #define _toupper(__c) ((__c) - ('a' - 'A'))
92 #define isascii(__c)	((unsigned)(__c)<=0177)
93 #define toascii(__c)	((__c)&0177)
94 #endif
95 
96 #if __POSIX_VISIBLE >= 200809
97 int isalnum_l (int c, locale_t l);
98 int isalpha_l (int c, locale_t l);
99 int isblank_l (int c, locale_t l);
100 int iscntrl_l (int c, locale_t l);
101 int isdigit_l (int c, locale_t l);
102 int isgraph_l (int c, locale_t l);
103 int islower_l (int c, locale_t l);
104 int isprint_l (int c, locale_t l);
105 int ispunct_l (int c, locale_t l);
106 int isspace_l (int c, locale_t l);
107 int isupper_l (int c, locale_t l);
108 int isxdigit_l(int c, locale_t l);
109 int tolower_l (int c, locale_t l);
110 int toupper_l (int c, locale_t l);
111 #endif
112 
113 #if __MISC_VISIBLE
114 int isascii_l (int c, locale_t l);
115 int toascii_l (int c, locale_t l);
116 #define isascii_l(__c,__l)	((__l),(unsigned)(__c)<=0177)
117 #define toascii_l(__c,__l)	((__l),(__c)&0177)
118 #endif
119 
120 #if _PICOLIBC_CTYPE_SMALL
121 
122 #ifdef __declare_extern_inline
123 
isblank(int c)124 __declare_extern_inline(int) isblank (int c)
125 {
126     return c == ' ' || c == '\t';
127 }
128 
iscntrl(int c)129 __declare_extern_inline(int) iscntrl (int c)
130 {
131     return (0x00 <= c && c <= 0x1f) || c == 0x7f;
132 }
133 
isdigit(int c)134 __declare_extern_inline(int) isdigit (int c)
135 {
136     return '0' <= c && c <= '9';
137 }
138 
isgraph(int c)139 __declare_extern_inline(int) isgraph (int c)
140 {
141     return '!' <= c && c <= '~';
142 }
143 
islower(int c)144 __declare_extern_inline(int) islower (int c)
145 {
146     return 'a' <= c && c <= 'z';
147 }
148 
isprint(int c)149 __declare_extern_inline(int) isprint (int c)
150 {
151     return ' ' <= c && c <= '~';
152 }
153 
ispunct(int c)154 __declare_extern_inline(int) ispunct (int c)
155 {
156     return (('!' <= c && c <= '/') ||
157             (':' <= c && c <= '@') ||
158             ('[' <= c && c <= '`') ||
159             ('{' <= c && c <= '~'));
160 }
161 
isspace(int c)162 __declare_extern_inline(int) isspace (int c)
163 {
164     return c == ' ' || ('\t' <= c && c <= '\r');
165 }
166 
isupper(int c)167 __declare_extern_inline(int) isupper (int c)
168 {
169     return 'A' <= c && c <= 'Z';
170 }
171 
isxdigit(int c)172 __declare_extern_inline(int) isxdigit (int c)
173 {
174     return (isdigit(c) ||
175             ('A' <= c && c <= 'F') ||
176             ('a' <= c && c <= 'f'));
177 }
178 
isalpha(int c)179 __declare_extern_inline(int) isalpha (int c)
180 {
181     return isupper(c) || islower(c);
182 }
183 
isalnum(int c)184 __declare_extern_inline(int) isalnum (int c)
185 {
186     return isalpha(c) || isdigit(c);
187 }
188 
tolower(int c)189 __declare_extern_inline(int) tolower (int c)
190 {
191     if (isupper(c))
192         c = c - 'A' + 'a';
193     return c;
194 }
195 
toupper(int c)196 __declare_extern_inline(int) toupper (int c)
197 {
198     if (islower(c))
199         c = c - 'a' + 'A';
200     return c;
201 }
202 
203 #if __POSIX_VISIBLE >= 200809
isalnum_l(int c,locale_t l)204 __declare_extern_inline(int) isalnum_l (int c, locale_t l) { (void) l; return isalnum(c); }
isalpha_l(int c,locale_t l)205 __declare_extern_inline(int) isalpha_l (int c, locale_t l) { (void) l; return isalpha(c); }
isblank_l(int c,locale_t l)206 __declare_extern_inline(int) isblank_l (int c, locale_t l) { (void) l; return isblank(c); }
iscntrl_l(int c,locale_t l)207 __declare_extern_inline(int) iscntrl_l (int c, locale_t l) { (void) l; return iscntrl(c); }
isdigit_l(int c,locale_t l)208 __declare_extern_inline(int) isdigit_l (int c, locale_t l) { (void) l; return isdigit(c); }
isgraph_l(int c,locale_t l)209 __declare_extern_inline(int) isgraph_l (int c, locale_t l) { (void) l; return isgraph(c); }
islower_l(int c,locale_t l)210 __declare_extern_inline(int) islower_l (int c, locale_t l) { (void) l; return islower(c); }
isprint_l(int c,locale_t l)211 __declare_extern_inline(int) isprint_l (int c, locale_t l) { (void) l; return isprint(c); }
ispunct_l(int c,locale_t l)212 __declare_extern_inline(int) ispunct_l (int c, locale_t l) { (void) l; return ispunct(c); }
isspace_l(int c,locale_t l)213 __declare_extern_inline(int) isspace_l (int c, locale_t l) { (void) l; return isspace(c); }
isupper_l(int c,locale_t l)214 __declare_extern_inline(int) isupper_l (int c, locale_t l) { (void) l; return isupper(c); }
isxdigit_l(int c,locale_t l)215 __declare_extern_inline(int) isxdigit_l(int c, locale_t l) { (void) l; return isxdigit(c); }
tolower_l(int c,locale_t l)216 __declare_extern_inline(int) tolower_l (int c, locale_t l) { (void) l; return tolower(c); }
toupper_l(int c,locale_t l)217 __declare_extern_inline(int) toupper_l (int c, locale_t l) { (void) l; return toupper(c); }
218 #endif
219 
220 #endif
221 
222 #else
223 
224 #define	_U	01
225 #define	_L	02
226 #define	_N	04
227 #define	_S	010
228 #define _P	020
229 #define _C	040
230 #define _X	0100
231 #define	_B	0200
232 
233 #ifndef __CHAR_UNSIGNED__
234 #define ALLOW_NEGATIVE_CTYPE_INDEX
235 #endif
236 
237 #if defined(ALLOW_NEGATIVE_CTYPE_INDEX)
238 extern const char	_ctype_b[];
239 #define _ctype_ (_ctype_b + 127)
240 #else
241 extern const char	_ctype_[];
242 #endif
243 
244 #ifdef __HAVE_LOCALE_INFO__
245 const char *__locale_ctype_ptr (void);
246 #else
247 #define __locale_ctype_ptr()	_ctype_
248 #endif
249 
250 # define __CTYPE_PTR	(__locale_ctype_ptr ())
251 
252 #ifndef __cplusplus
__ctype_lookup(int c)253 static __inline char __ctype_lookup(int c) { return (__CTYPE_PTR + 1)[c]; }
254 
255 #define	isalpha(__c)	(__ctype_lookup(__c)&(_U|_L))
256 #define	isupper(__c)	((__ctype_lookup(__c)&(_U|_L))==_U)
257 #define	islower(__c)	((__ctype_lookup(__c)&(_U|_L))==_L)
258 #define	isdigit(__c)	(__ctype_lookup(__c)&_N)
259 #define	isxdigit(__c)	(__ctype_lookup(__c)&(_X|_N))
260 #define	isspace(__c)	(__ctype_lookup(__c)&_S)
261 #define ispunct(__c)	(__ctype_lookup(__c)&_P)
262 #define isalnum(__c)	(__ctype_lookup(__c)&(_U|_L|_N))
263 #define isprint(__c)	(__ctype_lookup(__c)&(_P|_U|_L|_N|_B))
264 #define	isgraph(__c)	(__ctype_lookup(__c)&(_P|_U|_L|_N))
265 #define iscntrl(__c)	(__ctype_lookup(__c)&_C)
266 
267 #if __ISO_C_VISIBLE >= 1999
268 #if defined(__GNUC__)
269 #define isblank(__c)                                            \
270     __extension__ ({ __typeof__ (__c) __x = (__c);		\
271             (__ctype_lookup(__x)&_B) || (int) (__x) == '\t';})
272 #endif
273 #endif
274 
275 #if __POSIX_VISIBLE >= 200809
276 #ifdef __HAVE_LOCALE_INFO__
277 const char *__locale_ctype_ptr_l (locale_t);
278 #else
279 static __inline const char *
__locale_ctype_ptr_l(locale_t _l)280 __locale_ctype_ptr_l(locale_t _l)
281 {
282 	(void)_l;
283 	return __locale_ctype_ptr();
284 }
285 #endif
286 
__ctype_lookup_l(int c,locale_t l)287 static __inline char __ctype_lookup_l(int c, locale_t l) {
288 	return (__locale_ctype_ptr_l(l)+1)[(int)(c)];
289 }
290 
291 #define	isalpha_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_U|_L))
292 #define	isupper_l(__c,__l)	((__ctype_lookup_l(__c,__l)&(_U|_L))==_U)
293 #define	islower_l(__c,__l)	((__ctype_lookup_l(__c,__l)&(_U|_L))==_L)
294 #define	isdigit_l(__c,__l)	(__ctype_lookup_l(__c,__l)&_N)
295 #define	isxdigit_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_X|_N))
296 #define	isspace_l(__c,__l)	(__ctype_lookup_l(__c,__l)&_S)
297 #define ispunct_l(__c,__l)	(__ctype_lookup_l(__c,__l)&_P)
298 #define isalnum_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_U|_L|_N))
299 #define isprint_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_P|_U|_L|_N|_B))
300 #define	isgraph_l(__c,__l)	(__ctype_lookup_l(__c,__l)&(_P|_U|_L|_N))
301 #define iscntrl_l(__c,__l)	(__ctype_lookup_l(__c,__l)&_C)
302 
303 #if defined(__GNUC__)
304 #define isblank_l(__c, __l) \
305   __extension__ ({ __typeof__ (__c) __x = (__c);		\
306         (__ctype_lookup_l(__x,__l)&_B) || (int) (__x) == '\t';})
307 #endif
308 
309 #endif /* __POSIX_VISIBLE >= 200809 */
310 
311 /* Non-gcc versions will get the library versions, and will be
312    slightly slower.  These macros are not NLS-aware so they are
313    disabled if the system supports the extended character sets. */
314 # if defined(__GNUC__)
315 #  if !defined (_MB_EXTENDED_CHARSETS_ISO) && !defined (_MB_EXTENDED_CHARSETS_WINDOWS)
316 #   define toupper(__c) \
317   __extension__ ({ __typeof__ (__c) __x = (__c);	\
318       islower (__x) ? (int) __x - 'a' + 'A' : (int) __x;})
319 #   define tolower(__c) \
320   __extension__ ({ __typeof__ (__c) __x = (__c);	\
321       isupper (__x) ? (int) __x - 'A' + 'a' : (int) __x;})
322 #  else /* _MB_EXTENDED_CHARSETS* */
323 /* Allow a gcc warning if the user passed 'char', but defer to the
324    function.  */
325 #   define toupper(__c) \
326   __extension__ ({ __typeof__ (__c) __x = (__c);	\
327       (void) __CTYPE_PTR[__x]; (toupper) (__x);})
328 #   define tolower(__c) \
329   __extension__ ({ __typeof__ (__c) __x = (__c);	\
330       (void) __CTYPE_PTR[__x]; (tolower) (__x);})
331 #  endif /* _MB_EXTENDED_CHARSETS* */
332 # endif /* __GNUC__ */
333 
334 #endif /* !__cplusplus */
335 
336 #endif /* else _PICOLIBC_CTYPE_SMALL */
337 
338 _END_STD_C
339 
340 #endif /* _CTYPE_H_ */
341