setrunelocale.c revision 101261
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 101261 2002-08-03 09:10:31Z 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
6422330Sache	if (!encoding || strlen(encoding) > ENCODING_LEN)
65101261Sache		return (EINVAL);
6611695Sache
6711695Sache	/*
6811695Sache	 * The "C" and "POSIX" locale are always here.
6911695Sache	 */
7019964Sache	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX")) {
7111695Sache		_CurrentRuneLocale = &_DefaultRuneLocale;
7211695Sache		return(0);
7311695Sache	}
7411695Sache
7524694Sache	if (_PathLocale == NULL) {
7624694Sache		char *p = getenv("PATH_LOCALE");
7724694Sache
7832524Sjb		if (p != NULL
7932524Sjb#ifndef __NETBSD_SYSCALLS
8032524Sjb			&& !issetugid()
8132524Sjb#endif
8232524Sjb			) {
8324694Sache			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
8424694Sache			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
85101260Sache				return (ENAMETOOLONG);
8624694Sache			_PathLocale = strdup(p);
8724694Sache			if (_PathLocale == NULL)
8824694Sache				return (errno);
8924694Sache		} else
9024694Sache			_PathLocale = _PATH_LOCALE;
9124694Sache	}
9222330Sache	/* Range checking not needed, encoding length already checked above */
9320810Sjoerg	(void) strcpy(name, _PathLocale);
9420810Sjoerg	(void) strcat(name, "/");
9520810Sjoerg	(void) strcat(name, encoding);
9620810Sjoerg	(void) strcat(name, "/LC_CTYPE");
9711695Sache
9811695Sache	if ((fp = fopen(name, "r")) == NULL)
9911695Sache		return(ENOENT);
10011695Sache
10111695Sache	if ((rl = _Read_RuneMagi(fp)) == 0) {
10211695Sache		fclose(fp);
10311695Sache		return(EFTYPE);
10411695Sache	}
10511695Sache	fclose(fp);
10611695Sache
10729857Sache	if (!rl->encoding[0])
10811695Sache		return(EINVAL);
10929857Sache	else if (!strcmp(rl->encoding, "NONE"))
11011695Sache		return(_none_init(rl));
11161218Sache	else if (!strcmp(rl->encoding, "UTF2"))
11261218Sache		return(_UTF2_init(rl));
11329857Sache	else if (!strcmp(rl->encoding, "EUC"))
11411695Sache		return(_EUC_init(rl));
11538333Sphk	else if (!strcmp(rl->encoding, "BIG5"))
11638333Sphk		return(_BIG5_init(rl));
11729857Sache	else if (!strcmp(rl->encoding, "MSKanji"))
11829857Sache		return(_MSKanji_init(rl));
11929857Sache	else
12011695Sache		return(EINVAL);
12111695Sache}
12211695Sache
123