Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / chrtbl / ctypeio.c
blob67c894d647d07ad09692f4b3899fb353b9a1e521
1 /* $NetBSD: ctypeio.c,v 1.1 2009/01/02 00:20:23 tnozaki Exp $ */
3 /*
4 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 #if defined(LIBC_SCCS) && !defined(lint)
29 __RCSID("$NetBSD: ctypeio.c,v 1.1 2009/01/02 00:20:23 tnozaki Exp $");
30 #endif /* LIBC_SCCS and not lint */
32 #include <sys/types.h>
33 #include <assert.h>
34 #define _CTYPE_PRIVATE
35 #include <ctype.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
42 #include "ctypeio.h"
44 int
45 __savectype(const char *name, unsigned char *new_ctype,
46 short *new_toupper, short *new_tolower)
48 FILE *fp;
49 uint32_t i, len = _CTYPE_NUM_CHARS;
51 _DIAGASSERT(name != NULL);
52 _DIAGASSERT(new_ctype != NULL);
53 _DIAGASSERT(new_toupper != NULL);
54 _DIAGASSERT(new_tolower != NULL);
56 if ((fp = fopen(name, "w")) == NULL)
57 return 0;
59 if (fwrite(_CTYPE_ID, sizeof(_CTYPE_ID) - 1, 1, fp) != 1)
60 goto bad;
62 i = htonl(_CTYPE_REV);
63 if (fwrite(&i, sizeof(uint32_t), 1, fp) != 1)
64 goto bad;
66 i = htonl(len);
67 if (fwrite(&i, sizeof(uint32_t), 1, fp) != 1)
68 goto bad;
70 if (fwrite(&new_ctype[1], sizeof(uint8_t), len, fp) != len)
71 goto bad;
73 #if BYTE_ORDER == LITTLE_ENDIAN
74 for (i = 1; i <= len; i++) {
75 new_toupper[i] = htons(new_toupper[i]);
76 new_tolower[i] = htons(new_tolower[i]);
78 #endif
79 if (fwrite(&new_toupper[1], sizeof(int16_t), len, fp) != len)
80 goto bad;
82 if (fwrite(&new_tolower[1], sizeof(int16_t), len, fp) != len)
83 goto bad;
86 (void)fclose(fp);
87 return 1;
88 bad:
89 (void)fclose(fp);
90 return 0;