Annotation of src/lib/libc/locale/ctypeio.c, Revision 1.1
1.1 ! kleink 1: /* $NetBSD$ */
! 2:
! 3: /*
! 4: * Copyright (c) 1997 Christos Zoulas. All rights reserved.
! 5: *
! 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
! 9: * 1. Redistributions of source code must retain the above copyright
! 10: * notice, this list of conditions and the following disclaimer.
! 11: * 2. Redistributions in binary form must reproduce the above copyright
! 12: * notice, this list of conditions and the following disclaimer in the
! 13: * documentation and/or other materials provided with the distribution.
! 14: * 3. All advertising materials mentioning features or use of this software
! 15: * must display the following acknowledgement:
! 16: * This product includes software developed by Christos Zoulas.
! 17: * 4. The name of the author may not be used to endorse or promote products
! 18: * derived from this software without specific prior written permission.
! 19: *
! 20: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
! 21: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
! 22: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
! 23: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
! 24: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
! 25: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
! 26: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
! 27: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
! 28: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
! 29: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! 30: */
! 31:
! 32: #include <sys/types.h>
! 33: #include <stdio.h>
! 34: #include <stdlib.h>
! 35: #define _CTYPE_PRIVATE
! 36: #include <ctype.h>
! 37: #include "ctypeio.h"
! 38:
! 39: int
! 40: __loadctype(name)
! 41: const char *name;
! 42: {
! 43: FILE *fp;
! 44: char id[sizeof(_CTYPE_ID) - 1];
! 45: u_int32_t i, len;
! 46: unsigned char *new_ctype = NULL;
! 47: short *new_toupper = NULL, *new_tolower = NULL;
! 48:
! 49: if ((fp = fopen(name, "r")) == NULL)
! 50: return 0;
! 51:
! 52: if (fread(id, sizeof(id), 1, fp) != 1)
! 53: goto bad;
! 54:
! 55: if (memcmp(id, _CTYPE_ID, sizeof(id)) != 0)
! 56: goto bad;
! 57:
! 58: if (fread(&i, sizeof(u_int32_t), 1, fp) != 1)
! 59: goto bad;
! 60:
! 61: if ((i = ntohl(i)) != _CTYPE_REV)
! 62: goto bad;
! 63:
! 64: if (fread(&len, sizeof(u_int32_t), 1, fp) != 1)
! 65: goto bad;
! 66:
! 67: if ((len = ntohl(len)) != _CTYPE_NUM_CHARS)
! 68: goto bad;
! 69:
! 70: if ((new_ctype = malloc(sizeof(u_int8_t) * (1 + len))) == NULL)
! 71: goto bad;
! 72:
! 73: new_ctype[0] = 0;
! 74: if (fread(&new_ctype[1], sizeof(u_int8_t), len, fp) != len)
! 75: goto bad;
! 76:
! 77: if ((new_toupper = malloc(sizeof(int16_t) * (1 + len))) == NULL)
! 78: goto bad;
! 79:
! 80: new_toupper[0] = EOF;
! 81: if (fread(&new_toupper[1], sizeof(int16_t), len, fp) != len)
! 82: goto bad;
! 83:
! 84: if ((new_tolower = malloc(sizeof(int16_t) * (1 + len))) == NULL)
! 85: goto bad;
! 86:
! 87: new_tolower[0] = EOF;
! 88: if (fread(&new_tolower[1], sizeof(int16_t), len, fp) != len)
! 89: goto bad;
! 90:
! 91: #if BYTE_ORDER == LITTLE_ENDIAN
! 92: for (i = 1; i <= len; i++) {
! 93: new_toupper[i] = ntohs(new_toupper[i]);
! 94: new_tolower[i] = ntohs(new_tolower[i]);
! 95: }
! 96: #endif
! 97:
! 98: (void) fclose(fp);
! 99: if (_ctype_ != _C_ctype_)
! 100: free((void *) _ctype_);
! 101: _ctype_ = new_ctype;
! 102: if (_toupper_tab_ != _C_toupper_)
! 103: free((void *) _toupper_tab_);
! 104: _toupper_tab_ = new_toupper;
! 105: if (_tolower_tab_ != _C_tolower_)
! 106: free((void *) _tolower_tab_);
! 107: _tolower_tab_ = new_tolower;
! 108:
! 109: return 1;
! 110: bad:
! 111: free(new_tolower);
! 112: free(new_toupper);
! 113: free(new_ctype);
! 114: (void) fclose(fp);
! 115: return 0;
! 116: }
! 117:
! 118: int
! 119: __savectype(name, new_ctype, new_toupper, new_tolower)
! 120: const char *name;
! 121: unsigned char *new_ctype;
! 122: short *new_toupper, *new_tolower;
! 123: {
! 124: FILE *fp;
! 125: u_int32_t i, len = _CTYPE_NUM_CHARS;
! 126:
! 127: if ((fp = fopen(name, "w")) == NULL)
! 128: return 0;
! 129:
! 130: if (fwrite(_CTYPE_ID, sizeof(_CTYPE_ID) - 1, 1, fp) != 1)
! 131: goto bad;
! 132:
! 133: i = htonl(_CTYPE_REV);
! 134: if (fwrite(&i, sizeof(u_int32_t), 1, fp) != 1)
! 135: goto bad;
! 136:
! 137: i = htonl(len);
! 138: if (fwrite(&i, sizeof(u_int32_t), 1, fp) != 1)
! 139: goto bad;
! 140:
! 141: if (fwrite(&new_ctype[1], sizeof(u_int8_t), len, fp) != len)
! 142: goto bad;
! 143:
! 144: #if BYTE_ORDER == LITTLE_ENDIAN
! 145: for (i = 1; i <= len; i++) {
! 146: new_toupper[i] = htons(new_toupper[i]);
! 147: new_tolower[i] = htons(new_tolower[i]);
! 148: }
! 149: #endif
! 150: if (fwrite(&new_toupper[1], sizeof(int16_t), len, fp) != len)
! 151: goto bad;
! 152:
! 153: if (fwrite(&new_tolower[1], sizeof(int16_t), len, fp) != len)
! 154: goto bad;
! 155:
! 156:
! 157: (void) fclose(fp);
! 158: return 1;
! 159: bad:
! 160: (void) fclose(fp);
! 161: return 0;
! 162: }
CVSweb <webmaster@jp.NetBSD.org>