xref: /titanic_44/usr/src/lib/libc/port/print/vwprintf.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
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 _vwprintf_c89(), _vfwprintf_c89(),
47  * _vswprintf_c89() 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 <wchar.h> selects the proper routine
52  * at compile time for the user application.
53  * NOTE: shadow functions only exist in the 32-bit library.
54  */
55 
56 int
57 #ifdef _C89_INTMAX32		/* _C89_INTMAX32 version in 32-bit libc only */
58 _vwprintf_c89(const wchar_t *format, va_list ap)
59 #else
60 vwprintf(const wchar_t *format, va_list ap)
61 #endif
62 {
63 	ssize_t	count;
64 	rmutex_t	*lk;
65 
66 	FLOCKFILE(lk, stdout);
67 
68 	if (_set_orientation_wide(stdout, NULL, NULL, 0) == -1) {
69 		errno = EBADF;
70 		FUNLOCKFILE(lk);
71 		return (EOF);
72 	}
73 	if (!(stdout->_flag & _IOWRT)) {
74 		/* if no write flag */
75 		if (stdout->_flag & _IORW) {
76 			/* if ok, cause read-write */
77 			stdout->_flag |= _IOWRT;
78 		} else {
79 			/* else error */
80 			errno = EBADF;
81 			FUNLOCKFILE(lk);
82 			return (EOF);
83 		}
84 	}
85 #ifdef _C89_INTMAX32
86 	count = _wndoprnt(format, ap, stdout, _F_INTMAX32);
87 #else
88 	count = _wndoprnt(format, ap, stdout, 0);
89 #endif  /* _C89_INTMAX32 */
90 
91 	if (FERROR(stdout) || count == EOF) {
92 		FUNLOCKFILE(lk);
93 		return (EOF);
94 	}
95 	FUNLOCKFILE(lk);
96 	/* check for overflow */
97 	if ((size_t)count > MAXINT) {
98 		errno = EOVERFLOW;
99 		return (EOF);
100 	} else {
101 		return ((int)count);
102 	}
103 }
104 
105 int
106 #ifdef _C89_INTMAX32		/* _C89_INTMAX32 version in 32-bit libc only */
107 _vfwprintf_c89(FILE *iop, const wchar_t *format, va_list ap)
108 #else
109 vfwprintf(FILE *iop, const wchar_t *format, va_list ap)
110 #endif
111 {
112 	ssize_t	count;
113 	rmutex_t	*lk;
114 
115 	FLOCKFILE(lk, iop);
116 
117 	if (_set_orientation_wide(iop, NULL, NULL, 0) == -1) {
118 		errno = EBADF;
119 		FUNLOCKFILE(lk);
120 		return (EOF);
121 	}
122 
123 	if (!(iop->_flag & _IOWRT)) {
124 		/* if no write flag */
125 		if (iop->_flag & _IORW) {
126 			/* if ok, cause read-write */
127 			iop->_flag |= _IOWRT;
128 		} else {
129 			/* else error */
130 			errno = EBADF;
131 			FUNLOCKFILE(lk);
132 			return (EOF);
133 		}
134 	}
135 #ifdef _C89_INTMAX32
136 	count = _wndoprnt(format, ap, iop, _F_INTMAX32);
137 #else
138 	count = _wndoprnt(format, ap, iop, 0);
139 #endif
140 	if (FERROR(iop) || count == EOF) {
141 		FUNLOCKFILE(lk);
142 		return (EOF);
143 	}
144 	FUNLOCKFILE(lk);
145 	/* check for overflow */
146 	if ((size_t)count > MAXINT) {
147 		errno = EOVERFLOW;
148 		return (EOF);
149 	} else {
150 		return ((int)count);
151 	}
152 }
153 
154 int
155 #ifdef _C89_INTMAX32		/* _C89_INTMAX32 version in 32-bit libc only */
156 _vswprintf_c89(wchar_t *string, size_t n, const wchar_t *format, va_list ap)
157 #else
158 vswprintf(wchar_t *string, size_t n, const wchar_t *format, va_list ap)
159 #endif
160 {
161 	ssize_t	count;
162 	FILE	siop;
163 	wchar_t	*wp;
164 
165 	if (n == 0)
166 		return (EOF);
167 
168 	siop._cnt = (ssize_t)n - 1;
169 	siop._base = siop._ptr = (unsigned char *)string;
170 	siop._flag = _IOREAD;
171 
172 #ifdef _C89_INTMAX32
173 	count = _wndoprnt(format, ap, &siop, _F_INTMAX32);
174 #else
175 	count = _wndoprnt(format, ap, &siop, 0);
176 #endif
177 	wp = (wchar_t *)(uintptr_t)siop._ptr;
178 	*wp = L'\0';
179 	if (count == EOF) {
180 		return (EOF);
181 	}
182 	/* check for overflow */
183 	if ((size_t)count > MAXINT) {
184 		errno = EOVERFLOW;
185 		return (EOF);
186 	} else {
187 		return ((int)count);
188 	}
189 }
190