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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /* LINTLIBRARY */
30 
31 /*
32  * wio_put.c
33  *
34  * Wide I/O Library
35  *
36  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
37  *
38  */
39 
40 #if M_RCSID
41 #ifndef lint
42 static char rcsID[] = "$Header: /rd/src/libc/wide/rcs/wio_put.c 1.1 "
43 "1995/07/26 17:51:21 ant Exp $";
44 #endif
45 #endif
46 
47 #include <mks.h>
48 #include <errno.h>
49 #include <m_wio.h>
50 
51 /*
52  * Return the number of bytes written.  Errno will be set for errors.
53  *
54  * The function referenced by "put" is passed a byte value and the
55  * pointer "object", and returns the byte value or EOF if no further
56  * data can be written.
57  */
58 int
59 m_wio_put(wint_t wc, t_wide_io *wio)
60 {
61 	int	count, mb_len;
62 	unsigned char	*ptr;
63 
64 	if (wio == NULL || wio->put == (int (*)(int, void *)) NULL) {
65 		errno = EINVAL;
66 		return (-1);
67 	}
68 
69 	/* Force shift-in state at end-of-file. */
70 	if (wc == WEOF)
71 		wc = L'\0';
72 
73 	if ((mb_len = wctomb((char *) wio->_mb, wc)) < 0)
74 		/* Note errno will have been set by wcrtomb(). */
75 		return (-1);
76 
77 	/*
78 	 * When shift-in state has been forced don't write '\0' byte.
79 	 * The "stream" object is considered to be in "text" mode, in
80 	 * which case file I/O produces undefined results for systems
81 	 * using locking-shift character sets.
82 	 */
83 	if (wc == '\0')
84 		--mb_len;
85 
86 	/* Write multibyte character sequence. */
87 	for (ptr = wio->_mb, count = 0; count < mb_len; ++ptr, ++count)
88 		if ((*wio->put)(*ptr, wio->object) == EOF)
89 			break;
90 
91 	/* Remember just in case some one needs it. */
92 	wio->_size = mb_len;
93 	wio->_next = count;
94 
95 	return (count);
96 }
97