/*
 * |-----------------------------------------------------------|
 * | 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: wcstombs.c,v 1.2.2.1 92/06/24 14:01:11 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.	*/


/*
 * 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]
#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 */
#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 wc_val;

        if(!s)
                return(0);
        while (n > 0) {
                if (*wchar == 0) {
                        *s = 0;
                        return no_bytes;
                }
                if(!multibyte||*wchar<0200||*wchar<=0377&&iscntrl(*wchar)) {
                        *s++ = *wchar++;
                        no_bytes++;
                        n--;
                } else {
                        switch(*wchar & EUCMASK) {

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

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

                                case 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++;
                        while(index--) {
                                d = wc_val | 0200;
                                wc_val = wc_val >> 7;
                                if(iscntrl(d))
                                        return(-1);
                                s[index] = d;
                        }
                        s += size;
                }
        }
        return no_bytes;
}
