setrunelocale.c revision 117274
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/* setrunelocale() is obsolete in FreeBSD 6 -- use ANSI functions instead. */
38#define	OBSOLETE_IN_6
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/lib/libc/locale/setrunelocale.c 117274 2003-07-06 04:01:09Z ache $");
42
43#include <rune.h>
44#include <errno.h>
45#include <limits.h>
46#include <string.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <unistd.h>
50#include "ldpart.h"
51#include "setlocale.h"
52
53extern int		_none_init(_RuneLocale *);
54extern int		_UTF2_init(_RuneLocale *);
55extern int		_UTF8_init(_RuneLocale *);
56extern int		_EUC_init(_RuneLocale *);
57extern int		_GBK_init(_RuneLocale *);
58extern int		_BIG5_init(_RuneLocale *);
59extern int		_MSKanji_init(_RuneLocale *);
60extern _RuneLocale	*_Read_RuneMagi(FILE *);
61
62static int		__setrunelocale(const char *);
63
64__warn_references(setrunelocale, "warning: setrunelocale() is deprecated. See setrunelocale(3).");
65int
66setrunelocale(char *encoding)
67{
68	int ret;
69
70	if (!encoding || !*encoding || strlen(encoding) > ENCODING_LEN ||
71	    (encoding[0] == '.' &&
72	     (encoding[1] == '\0' ||
73	      (encoding[1] == '.' && encoding[2] == '\0'))) ||
74	    strchr(encoding, '/') != NULL)
75		return (EINVAL);
76
77	ret = __detect_path_locale();
78	if (ret != 0)
79		return (ret);
80
81	return (__setrunelocale((const char *)encoding));
82}
83
84static int
85__setrunelocale(const char *encoding)
86{
87	FILE *fp;
88	char name[PATH_MAX];
89	_RuneLocale *rl;
90	int saverr, ret;
91	static char ctype_encoding[ENCODING_LEN + 1];
92	static _RuneLocale *CachedRuneLocale;
93	static int Cached__mb_cur_max;
94
95	/*
96	 * The "C" and "POSIX" locale are always here.
97	 */
98	if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
99		_CurrentRuneLocale = &_DefaultRuneLocale;
100		__mb_cur_max = 1;
101		return (0);
102	}
103
104	/*
105	 * If the locale name is the same as our cache, use the cache.
106	 */
107	if (CachedRuneLocale != NULL &&
108	    strcmp(encoding, ctype_encoding) == 0) {
109		_CurrentRuneLocale = CachedRuneLocale;
110		__mb_cur_max = Cached__mb_cur_max;
111		return (0);
112	}
113
114	/*
115	 * Slurp the locale file into the cache.
116	 */
117
118	/* Range checking not needed, encoding length already checked before */
119	(void) strcpy(name, _PathLocale);
120	(void) strcat(name, "/");
121	(void) strcat(name, encoding);
122	(void) strcat(name, "/LC_CTYPE");
123
124	if ((fp = fopen(name, "r")) == NULL)
125		return (errno == 0 ? ENOENT : errno);
126
127	if ((rl = _Read_RuneMagi(fp)) == NULL) {
128		saverr = (errno == 0 ? EFTYPE : errno);
129		(void)fclose(fp);
130		return (saverr);
131	}
132	(void)fclose(fp);
133
134	if (strcmp(rl->encoding, "NONE") == 0)
135		ret = _none_init(rl);
136	else if (strcmp(rl->encoding, "UTF2") == 0)
137		ret = _UTF2_init(rl);
138	else if (strcmp(rl->encoding, "UTF-8") == 0)
139		ret = _UTF8_init(rl);
140	else if (strcmp(rl->encoding, "EUC") == 0)
141		ret = _EUC_init(rl);
142	else if (strcmp(rl->encoding, "GBK") == 0)
143		ret = _GBK_init(rl);
144	else if (strcmp(rl->encoding, "BIG5") == 0)
145		ret = _BIG5_init(rl);
146	else if (strcmp(rl->encoding, "MSKanji") == 0)
147		ret = _MSKanji_init(rl);
148	else
149		ret = EFTYPE;
150	if (ret == 0) {
151		if (CachedRuneLocale != NULL) {
152			/* See euc.c */
153			if (strcmp(CachedRuneLocale->encoding, "EUC") == 0)
154				free(CachedRuneLocale->variable);
155			free(CachedRuneLocale);
156		}
157		CachedRuneLocale = _CurrentRuneLocale;
158		Cached__mb_cur_max = __mb_cur_max;
159		(void)strcpy(ctype_encoding, encoding);
160	} else
161		free(rl);
162
163	return (ret);
164}
165
166int
167__wrap_setrunelocale(const char *locale)
168{
169	int ret = __setrunelocale(locale);
170
171	if (ret != 0) {
172		errno = ret;
173		return (_LDP_ERROR);
174	}
175	return (_LDP_LOADED);
176}
177
178