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