/*
 * |-----------------------------------------------------------|
 * | 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: _wctomb.c,v 1.1.2.1 92/06/24 14:10:43 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/_wctomb.c 1.0 AT&T-UP"

#include <ctype.h>
#include <stdlib.h>
#define multibyte	(_ctype[520]>1)
#define eucw1	_ctype[514]
#define eucw2   _ctype[515]
#define eucw3   _ctype[516]
#define	EUCMASK	0x8080
#define P00	0x0000		/* Code Set 0 */
#define P11	0x8080		/* Code Set 1 */
#define P01	0x0080		/* Code Set 2 */
#define P10	0x8000		/* Code Set 3 */
#define	SS2	0x8e
#define	SS3	0x8f

int _wctomb(s, wchar)
char *s;
wchar_t wchar;
{
	char *olds = s;
	register int size, index;
	unsigned char d;
	if(!s)
		return(0);
	if(!multibyte || wchar <= 0177 || wchar <= 0377 && iscntrl(wchar)) {
		*s++ = wchar;
		return(wchar ? 1 : 0);
	}
	switch(wchar & EUCMASK) {
		
		case P11:
			size = eucw1;
			break;
		
		case P01:
			*s++ = SS2;
			size = eucw2;
			break;
		
		case P10:
			*s++ = SS3;
			size = eucw3;
			break;
		
		default:
			return(-1);
	}
	index = size;	
	while(index--) {
		d = wchar | 0200;
		wchar >>= 8;
		if(iscntrl(d))
			return(-1);
		s[index] = d;
	}
	return(s + size - olds);
}
