/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
*/

/*	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	"$Header: setlocale.c 1.2 89/10/25 $@(#)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 (cat < LC_CTYPE || cat > LC_MAX)
		return (char *)0;

	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));
}
