/*
 * |-----------------------------------------------------------|
 * | Copyright (c) 1990 MIPS Computer Systems, Inc.            |
 * | All Rights Reserved                                       |
 * |-----------------------------------------------------------|
 * |          Restricted Rights Legend                         |
 * | Use, duplication, or disclosure by the Government is      |
 * | subject to restrictions as set forth in                   |
 * | subparagraph (c)(1)(ii) of the Rights in Technical        |
 * | Data and Computer Software Clause of DFARS 52.227-7013.   |
 * |         MIPS Computer Systems, Inc.                       |
 * |         928 Arques Avenue                                 |
 * |         Sunnyvale, CA 94086                               |
 * |-----------------------------------------------------------|
 */
#ident	"$Header: setlocale.c,v 1.1.2.1 92/06/24 14:10:50 marker Exp $"

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"@(#)libw:port/setlocale.c 1.0 AT&T-UP"

/*
* setlocale - set and query function for all or parts of a program's locale.
*/
#include <locale.h>
#include "_locale.h"	/* internal to libc locale data structures */
#include <string.h>

char *
setlocale(cat, loc)
int cat;
char *loc;
{
	char part[LC_NAMELEN];

	if (loc == 0)	/* query */
	{
		static char ans[LC_MAX * LC_NAMELEN + 1];

		if (cat != LC_ALL)
			(void)strcpy(ans, _cur_locale[cat]);
		else
		{
			register char *p, *q;
			register int i;
			register int flag = 0;

			/*
			* Generate composite locale description.
			*/
			p = ans;
			for (i = LC_CTYPE; i < LC_MAX; i++)
			{
				*p++ = '/';
				q = _cur_locale[i];
				(void)strcpy(p, q);
				p += strlen(q);
				if (!flag && i > LC_CTYPE)
					flag = strcmp(q ,_cur_locale[i-1]);
			}
			if (!flag)
				strcpy(ans,q);
		}
		return ans;
	}
	/*
	* Handle LC_ALL setting specially.
	*/
	if (cat == LC_ALL)
	{
		register char *p;
		register int i;

		cat = LC_CTYPE;
		if ((p = loc)[0] != '/')	/* simple locale */
		{
			loc = strncpy(part, p, LC_NAMELEN - 1);
			part[LC_NAMELEN - 1] = '\0';
		}
		do	/* for each category other than LC_ALL */
		{
			if (p[0] == '/')	/* piece of composite locale */
			{
				i = strcspn(++p, "/");
				(void)strncpy(part, p, i);
				part[i] = '\0';
				p += i;
			}
			(void)setlocale(cat++, part);
		} while (cat < LC_MAX);
		return (setlocale(LC_ALL, (char *)0));
	}
	/*
	* Set single category's locale.  By default,
	* just note the new name and handle it later.
	* For LC_CTYPE and LC_NUMERIC, fill in their
	* tables now.
	*/
	if (loc[0] == '\0')
		loc = _nativeloc(cat);
	else
	{
		loc = strncpy(part, loc, LC_NAMELEN - 1);
		part[LC_NAMELEN - 1] = '\0';
	}
	if (cat <= LC_NUMERIC)
	{
		if(strcmp(loc, _cur_locale[cat]) && _set_tab(loc, cat) != 0)
			return 0;
	}
	return(strcpy(_cur_locale[cat], loc));
}
