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