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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2025 Hans Rosenfeld 24 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 /* Copyright (c) 1988 AT&T */ 29 /* All Rights Reserved */ 30 31 #include "lint.h" 32 #include <thread.h> 33 #include <mtlib.h> 34 #include <synch.h> 35 #include <stdarg.h> 36 #include <values.h> 37 #include <errno.h> 38 #include <sys/types.h> 39 #include "print.h" 40 #include "libc.h" 41 #include "mse.h" 42 43 #ifdef _C89_INTMAX32 44 /* 45 * 32-bit shadow functions of vfprintf(), fprintf(), vprintf(), and printf() 46 * are built 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: these functions are only available in the 32-bit library. 53 */ 54 #pragma redefine_extname vfprintf _vfprintf_c89 55 #pragma redefine_extname fprintf _fprintf_c89 56 #pragma redefine_extname vprintf _vprintf_c89 57 #pragma redefine_extname printf _printf_c89 58 #else 59 /* 60 * This symbol should not be defined, but there are some 61 * miserable old compiler libraries that depend on it. 62 */ 63 #pragma weak _fprintf = fprintf 64 #endif 65 66 int 67 vfprintf(FILE *iop, const char *format, va_list ap) 68 { 69 ssize_t count; 70 rmutex_t *lk; 71 72 /* Use F*LOCKFILE() macros because vfprintf() is not async-safe. */ 73 FLOCKFILE(lk, iop); 74 75 _SET_ORIENTATION_BYTE(iop); 76 77 if (!(iop->_flag & _IOWRT)) { 78 /* if no write flag */ 79 if (iop->_flag & _IORW) { 80 /* if ok, cause read-write */ 81 iop->_flag |= _IOWRT; 82 } else { 83 /* else error */ 84 FUNLOCKFILE(lk); 85 errno = EBADF; 86 return (EOF); 87 } 88 } 89 #ifdef _C89_INTMAX32 90 count = _ndoprnt(format, ap, iop, _F_INTMAX32); 91 #else 92 count = _ndoprnt(format, ap, iop, 0); 93 #endif 94 95 /* check for error or EOF */ 96 if (FERROR(iop) || count == EOF) { 97 FUNLOCKFILE(lk); 98 return (EOF); 99 } 100 101 FUNLOCKFILE(lk); 102 103 /* check for overflow */ 104 if ((size_t)count > MAXINT) { 105 errno = EOVERFLOW; 106 return (EOF); 107 } 108 109 return ((int)count); 110 } 111 112 int 113 fprintf(FILE *iop, const char *format, ...) 114 { 115 int count; 116 va_list ap; 117 118 va_start(ap, format); 119 count = vfprintf(iop, format, ap); 120 va_end(ap); 121 122 return (count); 123 } 124 125 int 126 vprintf(const char *format, va_list ap) 127 { 128 int count; 129 130 count = vfprintf(stdout, format, ap); 131 132 return (count); 133 } 134 135 int 136 printf(const char *format, ...) 137 { 138 int count; 139 va_list ap; 140 141 va_start(ap, format); 142 count = vfprintf(stdout, format, ap); 143 va_end(ap); 144 145 return (count); 146 } 147