xref: /onnv-gate/usr/src/lib/libc/port/print/wprintf.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 _wprintf_c89(), _fwprintf_c89(), _swprintf_c89()
470Sstevel@tonic-gate  * 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 <stdio.h> selects the proper routine
520Sstevel@tonic-gate  * at compile time for the user application.
530Sstevel@tonic-gate  * NOTE: the shadow functions only exist in the 32-bit library.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate int
wprintf(const wchar_t * format,...)570Sstevel@tonic-gate wprintf(const wchar_t *format, ...)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	ssize_t	count;
600Sstevel@tonic-gate 	rmutex_t	*lk;
610Sstevel@tonic-gate 	va_list	ap;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	va_start(ap, format);
640Sstevel@tonic-gate 	FLOCKFILE(lk, stdout);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if (_set_orientation_wide(stdout, NULL, NULL, 0) == -1) {
670Sstevel@tonic-gate 		errno = EBADF;
680Sstevel@tonic-gate 		FUNLOCKFILE(lk);
690Sstevel@tonic-gate 		return (EOF);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (!(stdout->_flag & _IOWRT)) {
730Sstevel@tonic-gate 		/* if no write flag */
740Sstevel@tonic-gate 		if (stdout->_flag & _IORW) {
750Sstevel@tonic-gate 			/* if ok, cause read-write */
760Sstevel@tonic-gate 			stdout->_flag |= _IOWRT;
770Sstevel@tonic-gate 		} else {
780Sstevel@tonic-gate 			/* else error */
790Sstevel@tonic-gate 			errno = EBADF;
800Sstevel@tonic-gate 			FUNLOCKFILE(lk);
810Sstevel@tonic-gate 			return (EOF);
820Sstevel@tonic-gate 		}
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	count = _wndoprnt(format, ap, stdout, 0);
860Sstevel@tonic-gate 	va_end(ap);
870Sstevel@tonic-gate 	if (FERROR(stdout) || count == EOF) {
880Sstevel@tonic-gate 		FUNLOCKFILE(lk);
890Sstevel@tonic-gate 		return (EOF);
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 	FUNLOCKFILE(lk);
920Sstevel@tonic-gate 	/* check for overflow */
930Sstevel@tonic-gate 	if ((size_t)count > MAXINT) {
940Sstevel@tonic-gate 		errno = EOVERFLOW;
950Sstevel@tonic-gate 		return (EOF);
960Sstevel@tonic-gate 	} else {
970Sstevel@tonic-gate 		return ((int)count);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate int
fwprintf(FILE * iop,const wchar_t * format,...)1020Sstevel@tonic-gate fwprintf(FILE *iop, const wchar_t *format, ...)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	ssize_t	count;
1050Sstevel@tonic-gate 	rmutex_t	*lk;
1060Sstevel@tonic-gate 	va_list	ap;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	va_start(ap, format);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (_set_orientation_wide(iop, NULL, NULL, 0) == -1) {
1130Sstevel@tonic-gate 		errno = EBADF;
1140Sstevel@tonic-gate 		FUNLOCKFILE(lk);
1150Sstevel@tonic-gate 		return (EOF);
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (!(iop->_flag & _IOWRT)) {
1190Sstevel@tonic-gate 		/* if no write flag */
1200Sstevel@tonic-gate 		if (iop->_flag & _IORW) {
1210Sstevel@tonic-gate 			/* if ok, cause read-write */
1220Sstevel@tonic-gate 			iop->_flag |= _IOWRT;
1230Sstevel@tonic-gate 		} else {
1240Sstevel@tonic-gate 			/* else error */
1250Sstevel@tonic-gate 			errno = EBADF;
1260Sstevel@tonic-gate 			FUNLOCKFILE(lk);
1270Sstevel@tonic-gate 			return (EOF);
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	count = _wndoprnt(format, ap, iop, 0);
1320Sstevel@tonic-gate 	va_end(ap);
1330Sstevel@tonic-gate 	if (FERROR(iop) || count == EOF) {
1340Sstevel@tonic-gate 		FUNLOCKFILE(lk);
1350Sstevel@tonic-gate 		return (EOF);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	FUNLOCKFILE(lk);
1380Sstevel@tonic-gate 	/* check for overflow */
1390Sstevel@tonic-gate 	if ((size_t)count > MAXINT) {
1400Sstevel@tonic-gate 		errno = EOVERFLOW;
1410Sstevel@tonic-gate 		return (EOF);
1420Sstevel@tonic-gate 	} else {
1430Sstevel@tonic-gate 		return ((int)count);
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate int
swprintf(wchar_t * string,size_t n,const wchar_t * format,...)1480Sstevel@tonic-gate swprintf(wchar_t *string, size_t n, const wchar_t *format, ...)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	ssize_t	count;
1510Sstevel@tonic-gate 	FILE	siop;
1520Sstevel@tonic-gate 	wchar_t	*wp;
1530Sstevel@tonic-gate 	va_list	ap;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (n == 0)
1560Sstevel@tonic-gate 		return (EOF);
1570Sstevel@tonic-gate 	siop._cnt = (ssize_t)n - 1;
1580Sstevel@tonic-gate 	siop._base = siop._ptr = (unsigned char *)string;
1590Sstevel@tonic-gate 	siop._flag = _IOREAD;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	va_start(ap, format);
1620Sstevel@tonic-gate 	count = _wndoprnt(format, ap, &siop, 0);
1630Sstevel@tonic-gate 	va_end(ap);
1640Sstevel@tonic-gate 	wp = (wchar_t *)(uintptr_t)siop._ptr;
1650Sstevel@tonic-gate 	*wp = L'\0';
1660Sstevel@tonic-gate 	if (count == EOF) {
1670Sstevel@tonic-gate 		return (EOF);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 	/* check for overflow */
1700Sstevel@tonic-gate 	if ((size_t)count > MAXINT) {
1710Sstevel@tonic-gate 		errno = EOVERFLOW;
1720Sstevel@tonic-gate 		return (EOF);
1730Sstevel@tonic-gate 	} else {
1740Sstevel@tonic-gate 		return ((int)count);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #ifndef _LP64
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate int
_wprintf_c89(const wchar_t * format,...)1810Sstevel@tonic-gate _wprintf_c89(const wchar_t *format, ...)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	ssize_t	count;
1840Sstevel@tonic-gate 	va_list	ap;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	va_start(ap, format);
1870Sstevel@tonic-gate 	count = _vwprintf_c89(format, ap);
1880Sstevel@tonic-gate 	va_end(ap);
1890Sstevel@tonic-gate 	return ((int)count);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate int
_fwprintf_c89(FILE * iop,const wchar_t * format,...)1930Sstevel@tonic-gate _fwprintf_c89(FILE *iop, const wchar_t *format, ...)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	ssize_t	count;
1960Sstevel@tonic-gate 	va_list	ap;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	va_start(ap, format);
1990Sstevel@tonic-gate 	count = _vfwprintf_c89(iop, format, ap);
2000Sstevel@tonic-gate 	va_end(ap);
2010Sstevel@tonic-gate 	return ((int)count);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate int
_swprintf_c89(wchar_t * string,size_t n,const wchar_t * format,...)2050Sstevel@tonic-gate _swprintf_c89(wchar_t *string, size_t n, const wchar_t *format, ...)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	ssize_t	count;
2080Sstevel@tonic-gate 	va_list	ap;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	va_start(ap, format);
2110Sstevel@tonic-gate 	count = _vswprintf_c89(string, n, format, ap);
2120Sstevel@tonic-gate 	va_end(ap);
2130Sstevel@tonic-gate 	return ((int)count);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate #endif	/* _LP64 */
217