libutil: add O_NOCTTY back to old pty open code
[minix.git] / lib / libc / locale / bsdctype.c
blob5e499370aa50aa0a22d61a3bf901a74eae01d73f
1 /* $NetBSD: bsdctype.c,v 1.9 2010/06/20 02:23:15 tnozaki Exp $ */
3 /*-
4 * Copyright (c)2008 Citrus Project,
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: bsdctype.c,v 1.9 2010/06/20 02:23:15 tnozaki Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include <sys/endian.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include "bsdctype_local.h"
44 #include "runetype_misc.h"
46 const _BSDCTypeLocale _DefaultBSDCTypeLocale = {
47 _C_ctype_,
48 _C_tolower_,
49 _C_toupper_
52 const _BSDCTypeLocale *_CurrentBSDCTypeLocale = &_DefaultBSDCTypeLocale;
54 typedef struct {
55 _BSDCTypeLocale bl;
56 unsigned char blp_ctype_tab [_CTYPE_NUM_CHARS + 1];
57 short blp_tolower_tab[_CTYPE_NUM_CHARS + 1];
58 short blp_toupper_tab[_CTYPE_NUM_CHARS + 1];
59 } _BSDCTypeLocalePriv;
61 static __inline void
62 _bsdctype_init_priv(_BSDCTypeLocalePriv *blp)
64 #if _CTYPE_CACHE_SIZE != _CTYPE_NUM_CHARS
65 int i;
67 for (i = _CTYPE_CACHE_SIZE; i < _CTYPE_NUM_CHARS; ++i) {
68 blp->blp_ctype_tab [i + 1] = 0;
69 blp->blp_tolower_tab[i + 1] = i;
70 blp->blp_toupper_tab[i + 1] = i;
72 #endif
73 blp->blp_ctype_tab [0] = 0;
74 blp->blp_tolower_tab[0] = EOF;
75 blp->blp_toupper_tab[0] = EOF;
76 blp->bl.bl_ctype_tab = &blp->blp_ctype_tab [0];
77 blp->bl.bl_tolower_tab = &blp->blp_tolower_tab[0];
78 blp->bl.bl_toupper_tab = &blp->blp_toupper_tab[0];
81 static __inline int
82 _bsdctype_read_file(const char * __restrict var, size_t lenvar,
83 _BSDCTypeLocalePriv * __restrict blp)
85 const _FileBSDCTypeLocale *fbl;
86 uint32_t value;
87 int i;
89 _DIAGASSERT(blp != NULL);
91 if (lenvar < sizeof(*fbl))
92 return EFTYPE;
93 fbl = (const _FileBSDCTypeLocale *)(const void *)var;
94 if (memcmp(&fbl->fbl_id[0], _CTYPE_ID, sizeof(fbl->fbl_id)))
95 return EFTYPE;
96 value = be32toh(fbl->fbl_rev);
97 if (value != _CTYPE_REV)
98 return EFTYPE;
99 value = be32toh(fbl->fbl_num_chars);
100 if (value != _CTYPE_CACHE_SIZE)
101 return EFTYPE;
102 for (i = 0; i < _CTYPE_CACHE_SIZE; ++i) {
103 blp->blp_ctype_tab [i + 1] = fbl->fbl_ctype_tab[i];
104 blp->blp_tolower_tab[i + 1] = be16toh(fbl->fbl_tolower_tab[i]);
105 blp->blp_toupper_tab[i + 1] = be16toh(fbl->fbl_toupper_tab[i]);
107 return 0;
110 static __inline int
111 _bsdctype_read_runetype(const char * __restrict var, size_t lenvar,
112 _BSDCTypeLocalePriv * __restrict blp)
114 const _FileRuneLocale *frl;
115 int i;
117 _DIAGASSERT(blp != NULL);
119 if (lenvar < sizeof(*frl))
120 return EFTYPE;
121 lenvar -= sizeof(*frl);
122 frl = (const _FileRuneLocale *)(const void *)var;
123 if (memcmp(_RUNECT10_MAGIC, &frl->frl_magic[0], sizeof(frl->frl_magic)))
124 return EFTYPE;
125 if (frl->frl_encoding[0] != 'N' || frl->frl_encoding[1] != 'O' ||
126 frl->frl_encoding[2] != 'N' || frl->frl_encoding[3] != 'E' ||
127 frl->frl_encoding[4] != '\0') /* XXX */
128 return EFTYPE;
129 if (be32toh(frl->frl_runetype_ext.frr_nranges) != 0 ||
130 be32toh(frl->frl_maplower_ext.frr_nranges) != 0 ||
131 be32toh(frl->frl_mapupper_ext.frr_nranges) != 0)
132 return EFTYPE;
133 if (lenvar < be32toh((uint32_t)frl->frl_variable_len))
134 return EFTYPE;
135 for (i = 0; i < _CTYPE_CACHE_SIZE; ++i) {
136 blp->blp_ctype_tab [i + 1] = (unsigned char)
137 _runetype_to_ctype((_RuneType)
138 be32toh(frl->frl_runetype[i]));
139 blp->blp_tolower_tab[i + 1] = (short)
140 be32toh((uint32_t)frl->frl_maplower[i]);
141 blp->blp_toupper_tab[i + 1] = (short)
142 be32toh((uint32_t)frl->frl_mapupper[i]);
144 return 0;
148 _bsdctype_load(const char * __restrict var, size_t lenvar,
149 _BSDCTypeLocale ** __restrict pbl)
151 int ret;
152 _BSDCTypeLocalePriv *blp;
154 _DIAGASSERT(var != NULL || lenvar < 1);
155 _DIAGASSERT(pbl != NULL);
157 if (lenvar < 1)
158 return EFTYPE;
159 blp = malloc(sizeof(*blp));
160 if (blp == NULL)
161 return errno;
162 _bsdctype_init_priv(blp);
163 switch (*var) {
164 case 'B':
165 _bsdctype_read_file(var, lenvar, blp);
166 break;
167 case 'R':
168 _bsdctype_read_runetype(var, lenvar, blp);
169 break;
170 default:
171 ret = EFTYPE;
173 if (ret)
174 free(blp);
175 else
176 *pbl = &blp->bl;
177 return ret;