Bite the bullet and add real masks isprint and isgraph. Use this as
[dragonfly.git] / lib / libc / locale / ctypeio.c
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 $ */
3
4 /*
5  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
6  *
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.
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.
20  *
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.
31  */
32
33 #include <sys/types.h>
34
35 #include <assert.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include <netinet/in.h>
41
42 #define _CTYPE_PRIVATE
43 #include <ctype.h>
44 #include "ctypeio.h"
45
46 int
47 __loadctype(const char *name)
48 {
49         FILE *fp;
50         char id[sizeof(_CTYPE_ID) - 1];
51         uint32_t i, len;
52         uint16_t *new_ctype = NULL;
53         int16_t *new_toupper = NULL, *new_tolower = NULL;
54
55         _DIAGASSERT(name != NULL);
56
57         if ((fp = fopen(name, "r")) == NULL)
58                 return(0);
59
60         if (fread(id, sizeof(id), 1, fp) != 1)
61                 goto bad;
62
63         if (memcmp(id, _CTYPE_ID, sizeof(id)) != 0)
64                 goto bad;
65
66         if (fread(&i, sizeof(uint32_t), 1, fp) != 1) 
67                 goto bad;
68
69         if ((i = ntohl(i)) != _CTYPE_REV)
70                 goto bad;
71
72         if (fread(&len, sizeof(uint32_t), 1, fp) != 1)
73                 goto bad;
74
75         if ((len = ntohl(len)) != _CTYPE_NUM_CHARS)
76                 goto bad;
77
78         if ((new_ctype = malloc(sizeof(uint16_t) * (1 + len))) == NULL)
79                 goto bad;
80
81         new_ctype[0] = 0;
82         if (fread(&new_ctype[1], sizeof(uint16_t), len, fp) != len)
83                 goto bad;
84
85         if ((new_toupper = malloc(sizeof(int16_t) * (1 + len))) == NULL)
86                 goto bad;
87
88         new_toupper[0] = EOF;
89         if (fread(&new_toupper[1], sizeof(int16_t), len, fp) != len)
90                 goto bad;
91
92         if ((new_tolower = malloc(sizeof(int16_t) * (1 + len))) == NULL)
93                 goto bad;
94
95         new_tolower[0] = EOF;
96         if (fread(&new_tolower[1], sizeof(int16_t), len, fp) != len)
97                 goto bad;
98
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]);
103         }
104 #endif
105
106         fclose(fp);
107         if (__libc_ctype_ != __libc_C_ctype_) {
108                 /* LINTED const castaway ok */
109                 free((void *) __libc_ctype_);
110         }
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_));
115         }
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_));
120         }
121         __libc_tolower_tab_ = new_tolower;
122
123         return(1);
124 bad:
125         free(new_tolower);
126         free(new_toupper);
127         free(new_ctype);
128         fclose(fp);
129         return(0);
130 }
131
132 int
133 __savectype(const char *name, uint16_t *new_ctype, int16_t *new_toupper,
134             int16_t *new_tolower)
135 {
136         FILE *fp;
137         uint32_t i, len = _CTYPE_NUM_CHARS;
138
139         _DIAGASSERT(name != NULL);
140         _DIAGASSERT(new_ctype != NULL);
141         _DIAGASSERT(new_toupper != NULL);
142         _DIAGASSERT(new_tolower != NULL);
143
144         if ((fp = fopen(name, "w")) == NULL)
145                 return(0);
146
147         if (fwrite(_CTYPE_ID, sizeof(_CTYPE_ID) - 1, 1, fp) != 1)
148                 goto bad;
149
150         i = htonl(_CTYPE_REV);
151         if (fwrite(&i, sizeof(uint32_t), 1, fp) != 1) 
152                 goto bad;
153
154         i = htonl(len);
155         if (fwrite(&i, sizeof(uint32_t), 1, fp) != 1)
156                 goto bad;
157
158         if (fwrite(&new_ctype[1], sizeof(uint16_t), len, fp) != len)
159                 goto bad;
160
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]);
165         }
166 #endif
167         if (fwrite(&new_toupper[1], sizeof(int16_t), len, fp) != len)
168                 goto bad;
169
170         if (fwrite(&new_tolower[1], sizeof(int16_t), len, fp) != len)
171                 goto bad;
172
173         fclose(fp);
174         return(1);
175
176 bad:
177         fclose(fp);
178         return(0);
179 }