xref: /illumos-gate/usr/src/lib/libc/port/print/vfprintf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #include "synonyms.h"
34 #include <thread.h>
35 #include <mtlib.h>
36 #include <synch.h>
37 #include <stdarg.h>
38 #include <values.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include "print.h"
42 #include "libc.h"
43 #include "mse.h"
44 
45 /*
46  * 32-bit shadow function of vfprintf() is included here.
47  * When using the c89 compiler to build 32-bit applications, the size
48  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
49  * The shadow function uses 32-bit size of intmax_t for %j conversion.
50  * The #pragma redefine_extname in <stdio.h> selects the proper routine
51  * at compile time for the user application.
52  * NOTE: this function is only available in the 32-bit library.
53  */
54 
55 /*VARARGS2*/
56 int
57 #ifdef _C89_INTMAX32	/* _C89_INTMAX32 version in 32-bit libc only */
58 _vfprintf_c89(FILE *iop, const char *format, va_list ap)
59 #else
60 vfprintf(FILE *iop, const char *format, va_list ap)
61 #endif
62 {
63 	ssize_t count;
64 	rmutex_t *lk;
65 
66 	/* Use F*LOCKFILE() macros because vfprintf() is not async-safe. */
67 	FLOCKFILE(lk, iop);
68 
69 	_SET_ORIENTATION_BYTE(iop);
70 
71 	if (!(iop->_flag & _IOWRT)) {
72 		/* if no write flag */
73 		if (iop->_flag & _IORW) {
74 			/* if ok, cause read-write */
75 			iop->_flag |= _IOWRT;
76 		} else {
77 			/* else error */
78 			FUNLOCKFILE(lk);
79 			errno = EBADF;
80 			return (EOF);
81 		}
82 	}
83 #ifdef _C89_INTMAX32
84 	count = _ndoprnt(format, ap, iop, _F_INTMAX32);
85 #else
86 	count = _ndoprnt(format, ap, iop, 0);
87 #endif
88 
89 	/* check for error or EOF */
90 	if (FERROR(iop) || count == EOF) {
91 		FUNLOCKFILE(lk);
92 		return (EOF);
93 	}
94 
95 	FUNLOCKFILE(lk);
96 
97 	/* check for overflow */
98 	if ((size_t)count > MAXINT) {
99 		errno = EOVERFLOW;
100 		return (EOF);
101 	} else {
102 		return ((int)count);
103 	}
104 }
105