/*
 * |-----------------------------------------------------------|
 * | Copyright (c) 1991, 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 252.227-7013.  |
 * |         MIPS Computer Systems, Inc.                       |
 * |         950 DeGuigne Avenue                               |
 * |         Sunnyvale, California 94088-3650, USA             |
 * |-----------------------------------------------------------|
 */
#ident	"$Header: ctype.c,v 1.10.2.1 92/06/24 13:59:51 marker Exp $"

/*	Copyright (c) 1984 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.	*/

#include <fcntl.h>
#include <ctype.h>
#include "../locale/_locale.h" 

#define CCLASS_ROOT	"/lib/chrclass/"/* root for character classes */
#define LEN_CC_ROOT	14		/* length of CCLASS_ROOT */
#define DEFL_CCLASS	"ascii"		/* default character class */

extern unsigned char _ctype[SZ_TOTAL];
static char first_call = 1;

extern char *getenv(), *strcpy(), *memcpy();

int
setchrclass(ccname)
char *ccname;
{
	char pathname[128];
	char my_ctype[SZ_TOTAL];		/* local copy */
	register int fd, ret = -1;

       /*
	* If no character class name supplied, use the CHRCLASS env. variable.
	*/
	if (ccname == 0 && ((ccname = getenv("CHRCLASS")) == 0 || ccname[0] == '\0'))
	{
		/*
		* If this is the first call to setchrclass() then there is
		* nothing else to do here.  If this is a subsequent call
		* to setchrclass() and if _ctype[] was set to anything but
		* the default, the pathname[] array would have been changed.
		* Thus, in this case, actually do the reset to "ascii".
		*/
		if (first_call)
			return (0);
		ccname = DEFL_CCLASS;
	}
	first_call = 0;
	(void)strcpy(pathname, CCLASS_ROOT);
	(void)strcpy(&pathname[LEN_CC_ROOT], ccname);
	if ((fd = open(pathname, O_RDONLY)) >= 0)
	{
		if (read(fd, my_ctype, SZ_TOTAL) == SZ_TOTAL)
		{
			/*
			* Update _ctype[] only after everything works.
			*/
			(void)memcpy(_ctype, my_ctype, SZ_TOTAL);
			ret = 0;
		}
		(void)close(fd);
	}
	return (ret);
}
