/* $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: loadtab.c 1.2 90/02/27 $@(#)libw:port/loadtab.c 1.0 AT&T-UP"

/*	This module was created for NLS on Dec 18 '87		*/ /* NLS */

/*
* loadtab - set character table for multi-byte characters
*/
#include <locale.h>
#include <fcntl.h>
#include <widec.h>
#include <wctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#define	SZ	524

/* pointers to multi-byte character table */
struct	_wctype	*_wcptr[3] = {0, 0, 0}; 

/* _flag=1 if multi-byte character table is updated */
int	_lflag = 0;;

int
_loadtab()
{
	register int fd, ret;
	struct stat f_stat;
	static char *ptr=0;
	char *malloc();
	int i;
	unsigned lsize;

	_lflag = 1;
	if ((fd = open(_fullocale(_cur_locale[LC_CTYPE],"LC_CTYPE"), O_RDONLY)) == -1)
		ret = -1;
	else if (fstat(fd, &f_stat) == 0)
	{
		if (f_stat.st_size > SZ && lseek(fd, SZ, 0) != -1)
		{
			lsize = f_stat.st_size - SZ;
			if (ptr != 0)
			{
				free(ptr);
				ptr = 0;
			}
			if ((ptr = malloc(lsize)) != 0)
			{
				if (read(fd, ptr, lsize) == lsize)
				{
					for (i=0; i<3; i++)
					{
						_wcptr[i] = (struct _wctype *)((int)ptr + (int)((sizeof(struct _wctype)) * i));
						if (_wcptr[i]->index != 0)
							_wcptr[i]->index = (unsigned char *)((int)ptr + (int)_wcptr[i]->index);
						if (_wcptr[i]->type != 0)
							_wcptr[i]->type = (unsigned *)((int)ptr + (int)_wcptr[i]->type);
						if (_wcptr[i]->code != 0)
							_wcptr[i]->code = (wchar_t *)((int)ptr + (int)_wcptr[i]->code);
					}
					ret = 0;
				}
				else{
					free(ptr);
					ptr = 0;
				}
			}
		}
	}
	(void)close(fd);
	return ret;
}
