/*
 * |-----------------------------------------------------------|
 * | 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: loadtab.c,v 1.1.2.1 92/06/24 14:10:49 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/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 <locale.h>
#include <fcntl.h>
#include <widec.h>
#include <wctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#define	SZ	524

extern	int	_lflag;
extern	struct _wctype *_wcptr[];
int
_loadtab()
{
	register int fd, ret;
	/* start for MNLS by Unix Pacific on Dec. 2 1988	*/
/*	struct stat fstat;	*/
	struct stat f_stat;
	/* end for MNLS	*/
	static char *ptr=0;
	char *malloc();
	int i;
	unsigned lsize;

	_lflag = 1;
	/* start for MNLS by Unix Pacific on Dec. 2 1988	*/
/*	if ((fd = open(_cur_locale[LC_CTYPE], O_RDONLY)) == -1)	*/
	if ((fd = open(_fullocale(_cur_locale[LC_CTYPE],"LC_CTYPE"), O_RDONLY)) == -1)
	/* end for MNLS	*/
		ret = -1;
	/* start for MNLS by Unix Pacific on Dec. 2 1988	*/
/*	else if (stat(_cur_locale[LC_CTYPE], &fstat) == 0)	*/
	else if (fstat(fd, &f_stat) == 0)
	/* end for MNLS	*/
	{
	/* start for MNLS by Unix Pacific on Dec. 2 1988	*/
	/*	if (fstat.st_size > SZ && lseek(fd, SZ, 0) != -1)	*/
		if (f_stat.st_size > SZ && lseek(fd, SZ, 0) != -1)
	/* end for MNLS	*/
		{
	/* start for MNLS by Unix Pacific on Dec. 2 1988	*/
		/*	lsize = fstat.st_size - SZ;	*/
			lsize = f_stat.st_size - SZ;
	/* end for MNLS	*/
			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;
}
