setrunelocale.c revision 115626
1/*-
2 * Copyright (c) 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Paul Borman at Krystal Technologies.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/locale/setrunelocale.c 115626 2003-06-01 15:30:56Z ache $");
39
40#include <rune.h>
41#include <errno.h>
42#include <limits.h>
43#include <string.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <unistd.h>
47#include "setlocale.h"
48
49extern int		_none_init(_RuneLocale *);
50extern int		_UTF2_init(_RuneLocale *);
51extern int		_UTF8_init(_RuneLocale *);
52extern int		_EUC_init(_RuneLocale *);
53extern int		_GBK_init(_RuneLocale *);
54extern int		_BIG5_init(_RuneLocale *);
55extern int		_MSKanji_init(_RuneLocale *);
56extern _RuneLocale	*_Read_RuneMagi(FILE *);
57
58int
59setrunelocale(char *encoding)
60{
61	FILE *fp;
62	char name[PATH_MAX];
63	_RuneLocale *rl;
64	int saverr, ret;
65	static char ctype_encoding[ENCODING_LEN + 1];
66	static _RuneLocale *CachedRuneLocale;
67	static int Cached__mb_cur_max;
68
69	if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN ||
70	    (encoding[0] == '.' &&
71	     (encoding[1] == '\0' ||
72	      (encoding[1] == '.' && encoding[2] == '\0'))) ||
73	    strchr(encoding, '/') != NULL)
74		return (EINVAL);
75
76	/*
77	 * The "C" and "POSIX" locale are always here.
78	 */
79	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
80		_CurrentRuneLocale = &_DefaultRuneLocale;
81		__mb_cur_max = 1;
82		return (0);
83	}
84
85	/*
86	 * If the locale name is the same as our cache, use the cache.
87	 */
88	if (CachedRuneLocale != NULL &&
89	    strcmp(encoding, ctype_encoding) == 0) {
90		_CurrentRuneLocale = CachedRuneLocale;
91		__mb_cur_max = Cached__mb_cur_max;
92		return (0);
93	}
94
95	/*
96	 * Slurp the locale file into the cache.
97	 */
98	if (_PathLocale == NULL) {
99		char *p = getenv("PATH_LOCALE");
100
101		if (p != NULL
102#ifndef __NETBSD_SYSCALLS
103			&& !issetugid()
104#endif
105			) {
106			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
107			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
108				return (ENAMETOOLONG);
109			_PathLocale = strdup(p);
110			if (_PathLocale == NULL)
111				return (errno == 0 ? ENOMEM : errno);
112		} else
113			_PathLocale = _PATH_LOCALE;
114	}
115	/* Range checking not needed, encoding length already checked above */
116	(void) strcpy(name, _PathLocale);
117	(void) strcat(name, "/");
118	(void) strcat(name, encoding);
119	(void) strcat(name, "/LC_CTYPE");
120
121	if ((fp = fopen(name, "r")) == NULL)
122		return (errno == 0 ? ENOENT : errno);
123
124	if ((rl = _Read_RuneMagi(fp)) == NULL) {
125		saverr = (errno == 0 ? EFTYPE : errno);
126		(void)fclose(fp);
127		return (saverr);
128	}
129	(void)fclose(fp);
130
131	if (strcmp(rl->encoding, "NONE") == 0)
132		ret = _none_init(rl);
133	else if (strcmp(rl->encoding, "UTF2") == 0)
134		ret = _UTF2_init(rl);
135	else if (strcmp(rl->encoding, "UTF-8") == 0)
136		ret = _UTF8_init(rl);
137	else if (strcmp(rl->encoding, "EUC") == 0)
138		ret = _EUC_init(rl);
139	else if (strcmp(rl->encoding, "GBK") == 0)
140		ret = _GBK_init(rl);
141	else if (strcmp(rl->encoding, "BIG5") == 0)
142		ret = _BIG5_init(rl);
143	else if (strcmp(rl->encoding, "MSKanji") == 0)
144		ret = _MSKanji_init(rl);
145	else
146		ret = EFTYPE;
147	if (ret == 0) {
148		if (CachedRuneLocale != NULL) {
149			/* See euc.c */
150			if (strcmp(CachedRuneLocale->encoding, "EUC") == 0)
151				free(CachedRuneLocale->variable);
152			free(CachedRuneLocale);
153		}
154		CachedRuneLocale = _CurrentRuneLocale;
155		Cached__mb_cur_max = __mb_cur_max;
156		(void)strcpy(ctype_encoding, encoding);
157	} else
158		free(rl);
159
160	return (ret);
161}
162
163