Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / libc / port / print / wprintf.c
blobfdd1311c9929a1a8e3cfe26da3a12153338e5fe5
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
28 * Use is subject to license terms.
32 #include "lint.h"
33 #include "file64.h"
34 #include <mtlib.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <errno.h>
38 #include <thread.h>
39 #include <synch.h>
40 #include <values.h>
41 #include <wchar.h>
42 #include "print.h"
43 #include "stdiom.h"
44 #include <sys/types.h>
45 #include "libc.h"
46 #include "mse.h"
48 int
49 wprintf(const wchar_t *format, ...)
51 ssize_t count;
52 rmutex_t *lk;
53 va_list ap;
55 va_start(ap, format);
56 FLOCKFILE(lk, stdout);
58 if (GET_NO_MODE(stdout))
59 _setorientation(stdout, _WC_MODE);
61 if (!(stdout->_flag & _IOWRT)) {
62 /* if no write flag */
63 if (stdout->_flag & _IORW) {
64 /* if ok, cause read-write */
65 stdout->_flag |= _IOWRT;
66 } else {
67 /* else error */
68 errno = EBADF;
69 FUNLOCKFILE(lk);
70 return (EOF);
74 count = _wndoprnt(format, ap, stdout, 0);
75 va_end(ap);
76 if (FERROR(stdout) || count == EOF) {
77 FUNLOCKFILE(lk);
78 return (EOF);
80 FUNLOCKFILE(lk);
81 /* check for overflow */
82 if ((size_t)count > MAXINT) {
83 errno = EOVERFLOW;
84 return (EOF);
85 } else {
86 return ((int)count);
90 int
91 fwprintf(FILE *iop, const wchar_t *format, ...)
93 ssize_t count;
94 rmutex_t *lk;
95 va_list ap;
97 va_start(ap, format);
99 FLOCKFILE(lk, iop);
101 if (GET_NO_MODE(iop))
102 _setorientation(iop, _WC_MODE);
104 if (!(iop->_flag & _IOWRT)) {
105 /* if no write flag */
106 if (iop->_flag & _IORW) {
107 /* if ok, cause read-write */
108 iop->_flag |= _IOWRT;
109 } else {
110 /* else error */
111 errno = EBADF;
112 FUNLOCKFILE(lk);
113 return (EOF);
117 count = _wndoprnt(format, ap, iop, 0);
118 va_end(ap);
119 if (FERROR(iop) || count == EOF) {
120 FUNLOCKFILE(lk);
121 return (EOF);
123 FUNLOCKFILE(lk);
124 /* check for overflow */
125 if ((size_t)count > MAXINT) {
126 errno = EOVERFLOW;
127 return (EOF);
128 } else {
129 return ((int)count);
134 swprintf(wchar_t *string, size_t n, const wchar_t *format, ...)
136 ssize_t count;
137 FILE siop;
138 wchar_t *wp;
139 va_list ap;
141 if (n == 0)
142 return (EOF);
143 siop._cnt = (ssize_t)n - 1;
144 siop._base = siop._ptr = (unsigned char *)string;
145 siop._flag = _IOREAD;
147 va_start(ap, format);
148 count = _wndoprnt(format, ap, &siop, 0);
149 va_end(ap);
150 wp = (wchar_t *)(uintptr_t)siop._ptr;
151 *wp = L'\0';
152 if (count == EOF) {
153 return (EOF);
155 /* check for overflow */
156 if ((size_t)count > MAXINT) {
157 errno = EOVERFLOW;
158 return (EOF);
159 } else {
160 return ((int)count);