setrunelocale.c revision 101263
111695Sache/*-
211695Sache * Copyright (c) 1993
311695Sache *	The Regents of the University of California.  All rights reserved.
411695Sache *
511695Sache * This code is derived from software contributed to Berkeley by
611695Sache * Paul Borman at Krystal Technologies.
711695Sache *
811695Sache * Redistribution and use in source and binary forms, with or without
911695Sache * modification, are permitted provided that the following conditions
1011695Sache * are met:
1111695Sache * 1. Redistributions of source code must retain the above copyright
1211695Sache *    notice, this list of conditions and the following disclaimer.
1311695Sache * 2. Redistributions in binary form must reproduce the above copyright
1411695Sache *    notice, this list of conditions and the following disclaimer in the
1511695Sache *    documentation and/or other materials provided with the distribution.
1611695Sache * 3. All advertising materials mentioning features or use of this software
1711695Sache *    must display the following acknowledgement:
1811695Sache *	This product includes software developed by the University of
1911695Sache *	California, Berkeley and its contributors.
2011695Sache * 4. Neither the name of the University nor the names of its contributors
2111695Sache *    may be used to endorse or promote products derived from this software
2211695Sache *    without specific prior written permission.
2311695Sache *
2411695Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2511695Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2611695Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2711695Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2811695Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2911695Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3011695Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3111695Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3211695Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3311695Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3411695Sache * SUCH DAMAGE.
3511695Sache */
3611695Sache
3792986Sobrien#include <sys/cdefs.h>
3892986Sobrien__FBSDID("$FreeBSD: head/lib/libc/locale/setrunelocale.c 101263 2002-08-03 11:55:19Z ache $");
3992986Sobrien
4011695Sache#include <rune.h>
4111695Sache#include <errno.h>
4211695Sache#include <limits.h>
4311695Sache#include <string.h>
4411695Sache#include <stdio.h>
4511695Sache#include <stdlib.h>
4624694Sache#include <unistd.h>
4722330Sache#include "setlocale.h"
4811695Sache
4992905Sobrienextern int		_none_init(_RuneLocale *);
5092905Sobrienextern int		_UTF2_init(_RuneLocale *);
5192905Sobrienextern int		_EUC_init(_RuneLocale *);
5292905Sobrienextern int		_BIG5_init(_RuneLocale *);
5392905Sobrienextern int		_MSKanji_init(_RuneLocale *);
5492905Sobrienextern _RuneLocale      *_Read_RuneMagi(FILE *);
5511695Sache
5611695Sacheint
5711695Sachesetrunelocale(encoding)
5811695Sache	char *encoding;
5911695Sache{
6011695Sache	FILE *fp;
6111695Sache	char name[PATH_MAX];
6211695Sache	_RuneLocale *rl;
6311695Sache
64101262Sache	if (!encoding || strlen(encoding) > ENCODING_LEN ||
65101262Sache	    (encoding[0] == '.' &&
66101262Sache	     (encoding[1] == '\0' ||
67101262Sache	      (encoding[1] == '.' && encoding[2] == '\0'))) ||
68101262Sache	    strchr(encoding, '/') != NULL)
69101261Sache		return (EINVAL);
7011695Sache
7111695Sache	/*
7211695Sache	 * The "C" and "POSIX" locale are always here.
7311695Sache	 */
7419964Sache	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX")) {
7511695Sache		_CurrentRuneLocale = &_DefaultRuneLocale;
76101263Sache		return (0);
7711695Sache	}
7811695Sache
7924694Sache	if (_PathLocale == NULL) {
8024694Sache		char *p = getenv("PATH_LOCALE");
8124694Sache
8232524Sjb		if (p != NULL
8332524Sjb#ifndef __NETBSD_SYSCALLS
8432524Sjb			&& !issetugid()
8532524Sjb#endif
8632524Sjb			) {
8724694Sache			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
8824694Sache			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
89101260Sache				return (ENAMETOOLONG);
9024694Sache			_PathLocale = strdup(p);
9124694Sache			if (_PathLocale == NULL)
9224694Sache				return (errno);
9324694Sache		} else
9424694Sache			_PathLocale = _PATH_LOCALE;
9524694Sache	}
9622330Sache	/* Range checking not needed, encoding length already checked above */
9720810Sjoerg	(void) strcpy(name, _PathLocale);
9820810Sjoerg	(void) strcat(name, "/");
9920810Sjoerg	(void) strcat(name, encoding);
10020810Sjoerg	(void) strcat(name, "/LC_CTYPE");
10111695Sache
10211695Sache	if ((fp = fopen(name, "r")) == NULL)
103101263Sache		return (errno);
10411695Sache
10511695Sache	if ((rl = _Read_RuneMagi(fp)) == 0) {
10611695Sache		fclose(fp);
107101263Sache		return (EFTYPE);
10811695Sache	}
10911695Sache	fclose(fp);
11011695Sache
11129857Sache	if (!rl->encoding[0])
112101263Sache		return (EFTYPE);
11329857Sache	else if (!strcmp(rl->encoding, "NONE"))
114101263Sache		return (_none_init(rl));
11561218Sache	else if (!strcmp(rl->encoding, "UTF2"))
116101263Sache		return (_UTF2_init(rl));
11729857Sache	else if (!strcmp(rl->encoding, "EUC"))
118101263Sache		return (_EUC_init(rl));
11938333Sphk	else if (!strcmp(rl->encoding, "BIG5"))
120101263Sache		return (_BIG5_init(rl));
12129857Sache	else if (!strcmp(rl->encoding, "MSKanji"))
122101263Sache		return (_MSKanji_init(rl));
12329857Sache	else
124101263Sache		return (EFTYPE);
12511695Sache}
12611695Sache
127