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-1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /* LINTLIBRARY */
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
40 static char rcsID[] = "$Header: /rd/src/libc/wide/rcs/wio_put.c 1.1 "
41 "1995/07/26 17:51:21 ant Exp $";
42 #endif
43 #endif
44 
45 #include <mks.h>
46 #include <errno.h>
47 #include <m_wio.h>
48 
49 /*
50  * Return the number of bytes written.  Errno will be set for errors.
51  *
52  * The function referenced by "put" is passed a byte value and the
53  * pointer "object", and returns the byte value or EOF if no further
54  * data can be written.
55  */
56 int
m_wio_put(wint_t wc,t_wide_io * wio)57 m_wio_put(wint_t wc, t_wide_io *wio)
58 {
59 	int	count, mb_len;
60 	unsigned char	*ptr;
61 
62 	if (wio == NULL || wio->put == (int (*)(int, void *)) NULL) {
63 		errno = EINVAL;
64 		return (-1);
65 	}
66 
67 	/* Force shift-in state at end-of-file. */
68 	if (wc == WEOF)
69 		wc = L'\0';
70 
71 	if ((mb_len = wctomb((char *) wio->_mb, wc)) < 0)
72 		/* Note errno will have been set by wcrtomb(). */
73 		return (-1);
74 
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