/* $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: wcstombs.c 1.4 90/05/03 $@(#)libc-port:locale/wcstombs.c 1.0 AT&T-UP"

/*
 * wcstombs.c
 * 30/6/88
 * UEL
 */

#include <ctype.h>
#include <stdlib.h>
#define multibyte       (_ctype[520]>1)
#define eucw1   _ctype[514]
#define eucw2   _ctype[515]
#define eucw3   _ctype[516]
/*
 *	The followings are for 4 bytes of multi-byte code definition.
 */
#define EUCMASK 0x30000000
#define P00     0x00000000      /* Code Set 0 */
#define P11     0x30000000      /* Code Set 1 */
#define P01     0x10000000      /* Code Set 2 */
#define P10     0x20000000      /* Code Set 3 */
/*
 *	The followings are for 2 bytes of multi-byte code definition.
 */
#define H_EUCMASK 0x8080	/* mask bit for 16 bits */
#define H_P11     0x8080	/* Code Set 1 for 2 bytes code */
#define H_P01     0x0080	/* Code Set 2 for 2 bytes code */
#define H_P10     0x8000	/* Code Set 3 for 2 bytes code */

#define SS2     0x8e
#define SS3     0x8f

int wcstombs(s, wchar, n)
char        *s;
wchar_t        *wchar;
int                    n;
{
        register int 		no_bytes = 0;
        register int 		size = 0;
        register int 		index;
        unsigned char 		d;
        register int 		shift_char = 0;
        register int 		pcw;
        register wchar_t 	wc_val, wchar_mask;

        if(!s)
                return(0);

	if ( eucw1 > sizeof(unsigned short) ||
	     eucw2 > sizeof(unsigned short) ||
	     eucw3 > sizeof(unsigned short)) {
		pcw = sizeof(unsigned long);	/* 32 bits process code */
		wchar_mask = EUCMASK;
	}
	else {
		pcw = sizeof(unsigned short);	/* 16 bits process code */
		wchar_mask = H_EUCMASK;
	}

        while (n > 0) {
                if (*wchar == 0) {
                        *s = 0;
                        return no_bytes;
                }			/* MNLS defines space as cntrl */
                if ((!multibyte) || (*wchar < 0200) || (*wchar <= 0377) 
				 && ((iscntrl(*wchar)) || (*wchar == 040))){
                        *s++ = *wchar++;
                        no_bytes++;
                        n--;
                } else {
                        switch(*wchar & wchar_mask) {

                                case P11:
                                case H_P11:
                                        size = eucw1;
                                        shift_char = 0;
                                        break;

                                case P01:
                                case H_P01:
                                        size = eucw2 + 1;
                                        shift_char = SS2;
                                        break;

                                case P10:
                                case H_P10:
                                        size = eucw3 + 1;
                                        shift_char = SS3;
                                        break;

                                default:
                                        return(-1);
                        }
                        n -= size;
                        if (n < 0) {
                                *s = 0;
                                return no_bytes;
                        }
                        no_bytes += size;
                        if (shift_char) {
                                *s++ = shift_char;
                                size--;
                        }
                        if((index = size) <= 0)
                                return(-1);
                        wc_val = *wchar++;
			if (pcw > sizeof(unsigned short)) {
                        	while(index--) {
                                	d = wc_val & ~0200;
                                	wc_val = wc_val >> 7;
                                	if((iscntrl(d)) || (d == 040))
                                        	return(-1);
                                	s[index] = d | 0200;
                        	}
			}
			else {
                        	while(index--) {
                                	d = wc_val & ~0200;
                                	wc_val = wc_val >> 8;
                                	if((iscntrl(d)) || (d == 040))
                                        	return(-1);
                                	s[index] = d | 0200;
                        	}
			}
                        s += size;
                }
        }
        return no_bytes;
}

