xref: /titanic_52/usr/src/lib/libc/port/print/wprintf.c (revision 0bb073995ac5a95bd35f2dd790df1ea3d8c2d507)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include "file64.h"
31 #include <mtlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <errno.h>
35 #include <thread.h>
36 #include <synch.h>
37 #include <values.h>
38 #include <wchar.h>
39 #include "print.h"
40 #include "stdiom.h"
41 #include <sys/types.h>
42 #include "libc.h"
43 #include "mse.h"
44 
45 /*
46  * 32-bit shadow functions _wprintf_c89(), _fwprintf_c89(), _swprintf_c89()
47  * are included here.
48  * When using the c89 compiler to build 32-bit applications, the size
49  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
50  * The shadow function uses 32-bit size of intmax_t for j conversion.
51  * The #pragma redefine_extname in <stdio.h> selects the proper routine
52  * at compile time for the user application.
53  * NOTE: the shadow functions only exist in the 32-bit library.
54  */
55 
56 int
57 wprintf(const wchar_t *format, ...)
58 {
59 	ssize_t	count;
60 	rmutex_t	*lk;
61 	va_list	ap;
62 
63 	va_start(ap, format);
64 	FLOCKFILE(lk, stdout);
65 
66 	if (_set_orientation_wide(stdout, NULL, NULL, 0) == -1) {
67 		errno = EBADF;
68 		FUNLOCKFILE(lk);
69 		return (EOF);
70 	}
71 
72 	if (!(stdout->_flag & _IOWRT)) {
73 		/* if no write flag */
74 		if (stdout->_flag & _IORW) {
75 			/* if ok, cause read-write */
76 			stdout->_flag |= _IOWRT;
77 		} else {
78 			/* else error */
79 			errno = EBADF;
80 			FUNLOCKFILE(lk);
81 			return (EOF);
82 		}
83 	}
84 
85 	count = _wndoprnt(format, ap, stdout, 0);
86 	va_end(ap);
87 	if (FERROR(stdout) || count == EOF) {
88 		FUNLOCKFILE(lk);
89 		return (EOF);
90 	}
91 	FUNLOCKFILE(lk);
92 	/* check for overflow */
93 	if ((size_t)count > MAXINT) {
94 		errno = EOVERFLOW;
95 		return (EOF);
96 	} else {
97 		return ((int)count);
98 	}
99 }
100 
101 int
102 fwprintf(FILE *iop, const wchar_t *format, ...)
103 {
104 	ssize_t	count;
105 	rmutex_t	*lk;
106 	va_list	ap;
107 
108 	va_start(ap, format);
109 
110 	FLOCKFILE(lk, iop);
111 
112 	if (_set_orientation_wide(iop, NULL, NULL, 0) == -1) {
113 		errno = EBADF;
114 		FUNLOCKFILE(lk);
115 		return (EOF);
116 	}
117 
118 	if (!(iop->_flag & _IOWRT)) {
119 		/* if no write flag */
120 		if (iop->_flag & _IORW) {
121 			/* if ok, cause read-write */
122 			iop->_flag |= _IOWRT;
123 		} else {
124 			/* else error */
125 			errno = EBADF;
126 			FUNLOCKFILE(lk);
127 			return (EOF);
128 		}
129 	}
130 
131 	count = _wndoprnt(format, ap, iop, 0);
132 	va_end(ap);
133 	if (FERROR(iop) || count == EOF) {
134 		FUNLOCKFILE(lk);
135 		return (EOF);
136 	}
137 	FUNLOCKFILE(lk);
138 	/* check for overflow */
139 	if ((size_t)count > MAXINT) {
140 		errno = EOVERFLOW;
141 		return (EOF);
142 	} else {
143 		return ((int)count);
144 	}
145 }
146 
147 int
148 swprintf(wchar_t *string, size_t n, const wchar_t *format, ...)
149 {
150 	ssize_t	count;
151 	FILE	siop;
152 	wchar_t	*wp;
153 	va_list	ap;
154 
155 	if (n == 0)
156 		return (EOF);
157 	siop._cnt = (ssize_t)n - 1;
158 	siop._base = siop._ptr = (unsigned char *)string;
159 	siop._flag = _IOREAD;
160 
161 	va_start(ap, format);
162 	count = _wndoprnt(format, ap, &siop, 0);
163 	va_end(ap);
164 	wp = (wchar_t *)(uintptr_t)siop._ptr;
165 	*wp = L'\0';
166 	if (count == EOF) {
167 		return (EOF);
168 	}
169 	/* check for overflow */
170 	if ((size_t)count > MAXINT) {
171 		errno = EOVERFLOW;
172 		return (EOF);
173 	} else {
174 		return ((int)count);
175 	}
176 }
177 
178 #ifndef _LP64
179 
180 int
181 _wprintf_c89(const wchar_t *format, ...)
182 {
183 	ssize_t	count;
184 	va_list	ap;
185 
186 	va_start(ap, format);
187 	count = _vwprintf_c89(format, ap);
188 	va_end(ap);
189 	return ((int)count);
190 }
191 
192 int
193 _fwprintf_c89(FILE *iop, const wchar_t *format, ...)
194 {
195 	ssize_t	count;
196 	va_list	ap;
197 
198 	va_start(ap, format);
199 	count = _vfwprintf_c89(iop, format, ap);
200 	va_end(ap);
201 	return ((int)count);
202 }
203 
204 int
205 _swprintf_c89(wchar_t *string, size_t n, const wchar_t *format, ...)
206 {
207 	ssize_t	count;
208 	va_list	ap;
209 
210 	va_start(ap, format);
211 	count = _vswprintf_c89(string, n, format, ap);
212 	va_end(ap);
213 	return ((int)count);
214 }
215 
216 #endif	/* _LP64 */
217