xref: /titanic_50/usr/src/lib/libc/port/print/fprintf.c (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
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 /* This function should not be defined weak, but there might be */
34 /* some program or libraries that may be interposing on this */
35 #pragma weak fprintf = _fprintf
36 
37 #include "synonyms.h"
38 #include <mtlib.h>
39 #include <thread.h>
40 #include <synch.h>
41 #include <stdarg.h>
42 #include <values.h>
43 #include <errno.h>
44 #include "print.h"
45 #include <sys/types.h>
46 #include "libc.h"
47 #include "mse.h"
48 
49 /*VARARGS2*/
50 int
51 fprintf(FILE *iop, const char *format, ...)
52 {
53 	ssize_t count;
54 	rmutex_t *lk;
55 	va_list ap;
56 
57 	va_start(ap, format);
58 
59 	/* Use F*LOCKFILE() macros because fprintf() is not async-safe. */
60 	FLOCKFILE(lk, iop);
61 
62 	_SET_ORIENTATION_BYTE(iop);
63 
64 	if (!(iop->_flag & _IOWRT)) {
65 		/* if no write flag */
66 		if (iop->_flag & _IORW) {
67 			/* if ok, cause read-write */
68 			iop->_flag |= _IOWRT;
69 		} else {
70 			/* else error */
71 			FUNLOCKFILE(lk);
72 			errno = EBADF;
73 			return (EOF);
74 		}
75 	}
76 	count = _ndoprnt(format, ap, iop, 0);
77 
78 	/* return EOF on error or EOF */
79 	if (FERROR(iop) || count == EOF) {
80 		FUNLOCKFILE(lk);
81 		return (EOF);
82 	}
83 
84 	FUNLOCKFILE(lk);
85 
86 	/* error on overflow */
87 	if ((size_t)count > MAXINT) {
88 		errno = EOVERFLOW;
89 		return (EOF);
90 	}
91 	return ((int)count);
92 }
93 
94 #ifndef _LP64
95 
96 /*
97  * 32-bit shadow function of fprintf().
98  * When using the c89 compiler to build 32-bit applications, the size
99  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
100  * The shadow function uses 32-bit size of intmax_t for %j conversion.
101  * The #pragma redefine_extname in <stdio.h> selects the proper routine
102  * at compile time for the user application.
103  * NOTE: this function only exists in the 32-bit library.
104  */
105 
106 int
107 _fprintf_c89(FILE *iop, const char *format, ...)
108 {
109 	ssize_t count;
110 	va_list ap;
111 
112 	va_start(ap, format);
113 	count = _vfprintf_c89(iop, format, ap);
114 	va_end(ap);
115 	return ((int)count);
116 }
117 
118 #endif  /* _LP64 */
119