xref: /titanic_50/usr/src/lib/libc/port/print/vprintf.c (revision 2722387f30847b828b57ba9ca59d2b47ee9244ec)
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 <mtlib.h>
35 #include <stdarg.h>
36 #include <errno.h>
37 #include <thread.h>
38 #include <values.h>
39 #include <synch.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 vprintf() 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: the shadow function is only available in the 32-bit library.
53  */
54 
55 /*VARARGS1*/
56 
57 int
58 #ifdef _C89_INTMAX32
59 _vprintf_c89(const char *format, va_list ap)
60 #else
61 vprintf(const char *format, va_list ap)
62 #endif
63 {
64 	ssize_t count;
65 	rmutex_t *lk;
66 
67 	/* Use F*LOCKFILE() macros because vprintf() is not async-safe. */
68 	FLOCKFILE(lk, stdout);
69 
70 	_SET_ORIENTATION_BYTE(stdout);
71 
72 	if (!(stdout->_flag & _IOWRT)) {
73 		/* if no write flag */
74 		if (stdout->_flag & _IORW) {
75 			/* if ok, cause read-write */
76 			stdout->_flag |= _IOWRT;
77 		} else {
78 			/* else error */
79 			FUNLOCKFILE(lk);
80 			errno = EBADF;
81 			return (EOF);
82 		}
83 	}
84 #ifdef _C89_INTMAX32
85 	count = _ndoprnt(format, ap, stdout, _F_INTMAX32);
86 #else
87 	count = _ndoprnt(format, ap, stdout, 0);
88 #endif
89 	/* check for error or EOF */
90 	if (FERROR(stdout) || 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