1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright (c) 1996, by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29/*
30 * wio_put.c
31 *
32 * Wide I/O Library
33 *
34 * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35 *
36 */
37
38#if M_RCSID
39#ifndef lint
40static char rcsID[] = "$Header: /rd/src/libc/wide/rcs/wio_put.c 1.1 1995/07/26 17:51:21 ant Exp $";
41#endif
42#endif
43
44#include <mks.h>
45#include <errno.h>
46#include <m_wio.h>
47
48/*
49 * Return the number of bytes written.  Errno will be set for errors.
50 *
51 * The function referenced by "put" is passed a byte value and the
52 * pointer "object", and returns the byte value or EOF if no further
53 * data can be written.
54 */
55int
56m_wio_put(wc, wio)
57wint_t wc;
58t_wide_io *wio;
59{
60        int count, mb_len;
61        unsigned char *ptr;
62
63	if (wio == (t_wide_io *) 0 || wio->put == (int (*)(int, void *)) 0) {
64		errno = EINVAL;
65		return -1;
66	}
67
68	/* Force shift-in state at end-of-file. */
69	if (wc == WEOF)
70		wc = '\0';
71
72	if ((mb_len = wcrtomb((char *) wio->_mb, wc, &wio->_state)) < 0)
73		/* Note errno will have been set by wcrtomb(). */
74		return -1;
75
76	/* When shift-in state has been forced don't write '\0' byte.
77	 * The "stream" object is considered to be in "text" mode, in
78	 * which case file I/O produces undefined results for systems
79         * using locking-shift character sets.
80         */
81	if (wc == '\0')
82		--mb_len;
83
84	/* Write multibyte character sequence. */
85	for (ptr = wio->_mb, count = 0; count < mb_len; ++ptr, ++count)
86		if ((*wio->put)(*ptr, wio->object) == EOF)
87			break;
88
89	/* Remember just in case some one needs it. */
90	wio->_size = mb_len;
91	wio->_next = count;
92
93        return count;
94}
95
96