xref: /onnv-gate/usr/src/lib/libc/port/print/vwprintf.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #include "lint.h"
300Sstevel@tonic-gate #include "file64.h"
310Sstevel@tonic-gate #include <mtlib.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <stdarg.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <thread.h>
360Sstevel@tonic-gate #include <synch.h>
370Sstevel@tonic-gate #include <values.h>
380Sstevel@tonic-gate #include <wchar.h>
390Sstevel@tonic-gate #include "print.h"
400Sstevel@tonic-gate #include "stdiom.h"
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include "libc.h"
430Sstevel@tonic-gate #include "mse.h"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * 32-bit shadow functions _vwprintf_c89(), _vfwprintf_c89(),
470Sstevel@tonic-gate  * _vswprintf_c89() are included here.
480Sstevel@tonic-gate  * When using the c89 compiler to build 32-bit applications, the size
490Sstevel@tonic-gate  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
500Sstevel@tonic-gate  * The shadow function uses 32-bit size of intmax_t for j conversion.
510Sstevel@tonic-gate  * The #pragma redefine_extname in <wchar.h> selects the proper routine
520Sstevel@tonic-gate  * at compile time for the user application.
530Sstevel@tonic-gate  * NOTE: shadow functions only exist in the 32-bit library.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate int
570Sstevel@tonic-gate #ifdef _C89_INTMAX32		/* _C89_INTMAX32 version in 32-bit libc only */
_vwprintf_c89(const wchar_t * format,va_list ap)580Sstevel@tonic-gate _vwprintf_c89(const wchar_t *format, va_list ap)
590Sstevel@tonic-gate #else
600Sstevel@tonic-gate vwprintf(const wchar_t *format, va_list ap)
610Sstevel@tonic-gate #endif
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	ssize_t	count;
640Sstevel@tonic-gate 	rmutex_t	*lk;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	FLOCKFILE(lk, stdout);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (_set_orientation_wide(stdout, NULL, NULL, 0) == -1) {
690Sstevel@tonic-gate 		errno = EBADF;
700Sstevel@tonic-gate 		FUNLOCKFILE(lk);
710Sstevel@tonic-gate 		return (EOF);
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 	if (!(stdout->_flag & _IOWRT)) {
740Sstevel@tonic-gate 		/* if no write flag */
750Sstevel@tonic-gate 		if (stdout->_flag & _IORW) {
760Sstevel@tonic-gate 			/* if ok, cause read-write */
770Sstevel@tonic-gate 			stdout->_flag |= _IOWRT;
780Sstevel@tonic-gate 		} else {
790Sstevel@tonic-gate 			/* else error */
800Sstevel@tonic-gate 			errno = EBADF;
810Sstevel@tonic-gate 			FUNLOCKFILE(lk);
820Sstevel@tonic-gate 			return (EOF);
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate #ifdef _C89_INTMAX32
860Sstevel@tonic-gate 	count = _wndoprnt(format, ap, stdout, _F_INTMAX32);
870Sstevel@tonic-gate #else
880Sstevel@tonic-gate 	count = _wndoprnt(format, ap, stdout, 0);
890Sstevel@tonic-gate #endif  /* _C89_INTMAX32 */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (FERROR(stdout) || count == EOF) {
920Sstevel@tonic-gate 		FUNLOCKFILE(lk);
930Sstevel@tonic-gate 		return (EOF);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 	FUNLOCKFILE(lk);
960Sstevel@tonic-gate 	/* check for overflow */
970Sstevel@tonic-gate 	if ((size_t)count > MAXINT) {
980Sstevel@tonic-gate 		errno = EOVERFLOW;
990Sstevel@tonic-gate 		return (EOF);
1000Sstevel@tonic-gate 	} else {
1010Sstevel@tonic-gate 		return ((int)count);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate int
1060Sstevel@tonic-gate #ifdef _C89_INTMAX32		/* _C89_INTMAX32 version in 32-bit libc only */
_vfwprintf_c89(FILE * iop,const wchar_t * format,va_list ap)1070Sstevel@tonic-gate _vfwprintf_c89(FILE *iop, const wchar_t *format, va_list ap)
1080Sstevel@tonic-gate #else
1090Sstevel@tonic-gate vfwprintf(FILE *iop, const wchar_t *format, va_list ap)
1100Sstevel@tonic-gate #endif
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	ssize_t	count;
1130Sstevel@tonic-gate 	rmutex_t	*lk;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (_set_orientation_wide(iop, NULL, NULL, 0) == -1) {
1180Sstevel@tonic-gate 		errno = EBADF;
1190Sstevel@tonic-gate 		FUNLOCKFILE(lk);
1200Sstevel@tonic-gate 		return (EOF);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (!(iop->_flag & _IOWRT)) {
1240Sstevel@tonic-gate 		/* if no write flag */
1250Sstevel@tonic-gate 		if (iop->_flag & _IORW) {
1260Sstevel@tonic-gate 			/* if ok, cause read-write */
1270Sstevel@tonic-gate 			iop->_flag |= _IOWRT;
1280Sstevel@tonic-gate 		} else {
1290Sstevel@tonic-gate 			/* else error */
1300Sstevel@tonic-gate 			errno = EBADF;
1310Sstevel@tonic-gate 			FUNLOCKFILE(lk);
1320Sstevel@tonic-gate 			return (EOF);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate #ifdef _C89_INTMAX32
1360Sstevel@tonic-gate 	count = _wndoprnt(format, ap, iop, _F_INTMAX32);
1370Sstevel@tonic-gate #else
1380Sstevel@tonic-gate 	count = _wndoprnt(format, ap, iop, 0);
1390Sstevel@tonic-gate #endif
1400Sstevel@tonic-gate 	if (FERROR(iop) || count == EOF) {
1410Sstevel@tonic-gate 		FUNLOCKFILE(lk);
1420Sstevel@tonic-gate 		return (EOF);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	FUNLOCKFILE(lk);
1450Sstevel@tonic-gate 	/* check for overflow */
1460Sstevel@tonic-gate 	if ((size_t)count > MAXINT) {
1470Sstevel@tonic-gate 		errno = EOVERFLOW;
1480Sstevel@tonic-gate 		return (EOF);
1490Sstevel@tonic-gate 	} else {
1500Sstevel@tonic-gate 		return ((int)count);
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate int
1550Sstevel@tonic-gate #ifdef _C89_INTMAX32		/* _C89_INTMAX32 version in 32-bit libc only */
_vswprintf_c89(wchar_t * string,size_t n,const wchar_t * format,va_list ap)1560Sstevel@tonic-gate _vswprintf_c89(wchar_t *string, size_t n, const wchar_t *format, va_list ap)
1570Sstevel@tonic-gate #else
1580Sstevel@tonic-gate vswprintf(wchar_t *string, size_t n, const wchar_t *format, va_list ap)
1590Sstevel@tonic-gate #endif
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	ssize_t	count;
1620Sstevel@tonic-gate 	FILE	siop;
1630Sstevel@tonic-gate 	wchar_t	*wp;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (n == 0)
1660Sstevel@tonic-gate 		return (EOF);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	siop._cnt = (ssize_t)n - 1;
1690Sstevel@tonic-gate 	siop._base = siop._ptr = (unsigned char *)string;
1700Sstevel@tonic-gate 	siop._flag = _IOREAD;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate #ifdef _C89_INTMAX32
1730Sstevel@tonic-gate 	count = _wndoprnt(format, ap, &siop, _F_INTMAX32);
1740Sstevel@tonic-gate #else
1750Sstevel@tonic-gate 	count = _wndoprnt(format, ap, &siop, 0);
1760Sstevel@tonic-gate #endif
1770Sstevel@tonic-gate 	wp = (wchar_t *)(uintptr_t)siop._ptr;
1780Sstevel@tonic-gate 	*wp = L'\0';
1790Sstevel@tonic-gate 	if (count == EOF) {
1800Sstevel@tonic-gate 		return (EOF);
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 	/* check for overflow */
1830Sstevel@tonic-gate 	if ((size_t)count > MAXINT) {
1840Sstevel@tonic-gate 		errno = EOVERFLOW;
1850Sstevel@tonic-gate 		return (EOF);
1860Sstevel@tonic-gate 	} else {
1870Sstevel@tonic-gate 		return ((int)count);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate }
190