setrunelocale.c revision 92905
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.
3561218Sache *
3661218Sache * $FreeBSD: head/lib/libc/locale/setrunelocale.c 92905 2002-03-21 22:49:10Z obrien $
3711695Sache */
3811695Sache
3911695Sache#include <rune.h>
4011695Sache#include <errno.h>
4111695Sache#include <limits.h>
4211695Sache#include <string.h>
4311695Sache#include <stdio.h>
4411695Sache#include <stdlib.h>
4524694Sache#include <unistd.h>
4622330Sache#include "setlocale.h"
4711695Sache
4892905Sobrienextern int		_none_init(_RuneLocale *);
4992905Sobrienextern int		_UTF2_init(_RuneLocale *);
5092905Sobrienextern int		_EUC_init(_RuneLocale *);
5192905Sobrienextern int		_BIG5_init(_RuneLocale *);
5292905Sobrienextern int		_MSKanji_init(_RuneLocale *);
5392905Sobrienextern _RuneLocale      *_Read_RuneMagi(FILE *);
5411695Sache
5511695Sacheint
5611695Sachesetrunelocale(encoding)
5711695Sache	char *encoding;
5811695Sache{
5911695Sache	FILE *fp;
6011695Sache	char name[PATH_MAX];
6111695Sache	_RuneLocale *rl;
6211695Sache
6322330Sache	if (!encoding || strlen(encoding) > ENCODING_LEN)
6411695Sache	    return(EFAULT);
6511695Sache
6611695Sache	/*
6711695Sache	 * The "C" and "POSIX" locale are always here.
6811695Sache	 */
6919964Sache	if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX")) {
7011695Sache		_CurrentRuneLocale = &_DefaultRuneLocale;
7111695Sache		return(0);
7211695Sache	}
7311695Sache
7424694Sache	if (_PathLocale == NULL) {
7524694Sache		char *p = getenv("PATH_LOCALE");
7624694Sache
7732524Sjb		if (p != NULL
7832524Sjb#ifndef __NETBSD_SYSCALLS
7932524Sjb			&& !issetugid()
8032524Sjb#endif
8132524Sjb			) {
8224694Sache			if (strlen(p) + 1/*"/"*/ + ENCODING_LEN +
8324694Sache			    1/*"/"*/ + CATEGORY_LEN >= PATH_MAX)
8424694Sache				return(EFAULT);
8524694Sache			_PathLocale = strdup(p);
8624694Sache			if (_PathLocale == NULL)
8724694Sache				return (errno);
8824694Sache		} else
8924694Sache			_PathLocale = _PATH_LOCALE;
9024694Sache	}
9122330Sache	/* Range checking not needed, encoding length already checked above */
9220810Sjoerg	(void) strcpy(name, _PathLocale);
9320810Sjoerg	(void) strcat(name, "/");
9420810Sjoerg	(void) strcat(name, encoding);
9520810Sjoerg	(void) strcat(name, "/LC_CTYPE");
9611695Sache
9711695Sache	if ((fp = fopen(name, "r")) == NULL)
9811695Sache		return(ENOENT);
9911695Sache
10011695Sache	if ((rl = _Read_RuneMagi(fp)) == 0) {
10111695Sache		fclose(fp);
10211695Sache		return(EFTYPE);
10311695Sache	}
10411695Sache	fclose(fp);
10511695Sache
10629857Sache	if (!rl->encoding[0])
10711695Sache		return(EINVAL);
10829857Sache	else if (!strcmp(rl->encoding, "NONE"))
10911695Sache		return(_none_init(rl));
11061218Sache	else if (!strcmp(rl->encoding, "UTF2"))
11161218Sache		return(_UTF2_init(rl));
11229857Sache	else if (!strcmp(rl->encoding, "EUC"))
11311695Sache		return(_EUC_init(rl));
11438333Sphk	else if (!strcmp(rl->encoding, "BIG5"))
11538333Sphk		return(_BIG5_init(rl));
11629857Sache	else if (!strcmp(rl->encoding, "MSKanji"))
11729857Sache		return(_MSKanji_init(rl));
11829857Sache	else
11911695Sache		return(EINVAL);
12011695Sache}
12111695Sache
122