1 /* $NetBSD: src/lib/libc/locale/ctypeio.c,v 1.5 2000/07/01 00:05:27 matt Exp $ */
2 /* $DragonFly: src/lib/libc/locale/Attic/ctypeio.c,v 1.1 2005/03/16 06:54:41 joerg Exp $ */
5 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christos Zoulas.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
40 #include <netinet/in.h>
42 #define _CTYPE_PRIVATE
47 __loadctype(const char *name)
50 char id[sizeof(_CTYPE_ID) - 1];
52 uint16_t *new_ctype = NULL;
53 int16_t *new_toupper = NULL, *new_tolower = NULL;
55 _DIAGASSERT(name != NULL);
57 if ((fp = fopen(name, "r")) == NULL)
60 if (fread(id, sizeof(id), 1, fp) != 1)
63 if (memcmp(id, _CTYPE_ID, sizeof(id)) != 0)
66 if (fread(&i, sizeof(uint32_t), 1, fp) != 1)
69 if ((i = ntohl(i)) != _CTYPE_REV)
72 if (fread(&len, sizeof(uint32_t), 1, fp) != 1)
75 if ((len = ntohl(len)) != _CTYPE_NUM_CHARS)
78 if ((new_ctype = malloc(sizeof(uint16_t) * (1 + len))) == NULL)
82 if (fread(&new_ctype[1], sizeof(uint16_t), len, fp) != len)
85 if ((new_toupper = malloc(sizeof(int16_t) * (1 + len))) == NULL)
89 if (fread(&new_toupper[1], sizeof(int16_t), len, fp) != len)
92 if ((new_tolower = malloc(sizeof(int16_t) * (1 + len))) == NULL)
96 if (fread(&new_tolower[1], sizeof(int16_t), len, fp) != len)
99 #if _BYTE_ORDER == _LITTLE_ENDIAN
100 for (i = 1; i <= len; i++) {
101 new_toupper[i] = ntohs(new_toupper[i]);
102 new_tolower[i] = ntohs(new_tolower[i]);
107 if (__libc_ctype_ != __libc_C_ctype_) {
108 /* LINTED const castaway ok */
109 free((void *) __libc_ctype_);
111 __libc_ctype_ = new_ctype;
112 if (__libc_toupper_tab_ != __libc_C_toupper_) {
113 /* LINTED const castaway ok */
114 free(__DECONST(void *, __libc_toupper_tab_));
116 __libc_toupper_tab_ = new_toupper;
117 if (__libc_tolower_tab_ != __libc_C_tolower_) {
118 /* LINTED const castaway ok */
119 free(__DECONST(void *, __libc_tolower_tab_));
121 __libc_tolower_tab_ = new_tolower;
133 __savectype(const char *name, uint16_t *new_ctype, int16_t *new_toupper,
134 int16_t *new_tolower)
137 uint32_t i, len = _CTYPE_NUM_CHARS;
139 _DIAGASSERT(name != NULL);
140 _DIAGASSERT(new_ctype != NULL);
141 _DIAGASSERT(new_toupper != NULL);
142 _DIAGASSERT(new_tolower != NULL);
144 if ((fp = fopen(name, "w")) == NULL)
147 if (fwrite(_CTYPE_ID, sizeof(_CTYPE_ID) - 1, 1, fp) != 1)
150 i = htonl(_CTYPE_REV);
151 if (fwrite(&i, sizeof(uint32_t), 1, fp) != 1)
155 if (fwrite(&i, sizeof(uint32_t), 1, fp) != 1)
158 if (fwrite(&new_ctype[1], sizeof(uint16_t), len, fp) != len)
161 #if BYTE_ORDER == LITTLE_ENDIAN
162 for (i = 1; i <= len; i++) {
163 new_toupper[i] = htons(new_toupper[i]);
164 new_tolower[i] = htons(new_tolower[i]);
167 if (fwrite(&new_toupper[1], sizeof(int16_t), len, fp) != len)
170 if (fwrite(&new_tolower[1], sizeof(int16_t), len, fp) != len)