17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1braf * Common Development and Distribution License (the "License").
67257d1braf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217257d1braf
227c478bdstevel@tonic-gate/*
237257d1braf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307257d1braf#pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bdstevel@tonic-gate
327257d1braf/*
337257d1braf * This symbol should not be defined, but there are some
347257d1braf * miserable old compiler libraries that depend on it.
357257d1braf */
367257d1braf#pragma weak _fprintf = fprintf
377c478bdstevel@tonic-gate
387257d1braf#include "lint.h"
397c478bdstevel@tonic-gate#include <mtlib.h>
407c478bdstevel@tonic-gate#include <thread.h>
417c478bdstevel@tonic-gate#include <synch.h>
427c478bdstevel@tonic-gate#include <stdarg.h>
437c478bdstevel@tonic-gate#include <values.h>
447c478bdstevel@tonic-gate#include <errno.h>
457c478bdstevel@tonic-gate#include "print.h"
467c478bdstevel@tonic-gate#include <sys/types.h>
477c478bdstevel@tonic-gate#include "libc.h"
487c478bdstevel@tonic-gate#include "mse.h"
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate/*VARARGS2*/
517c478bdstevel@tonic-gateint
527c478bdstevel@tonic-gatefprintf(FILE *iop, const char *format, ...)
537c478bdstevel@tonic-gate{
547c478bdstevel@tonic-gate	ssize_t count;
557c478bdstevel@tonic-gate	rmutex_t *lk;
567c478bdstevel@tonic-gate	va_list ap;
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate	va_start(ap, format);
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate	/* Use F*LOCKFILE() macros because fprintf() is not async-safe. */
617c478bdstevel@tonic-gate	FLOCKFILE(lk, iop);
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate	_SET_ORIENTATION_BYTE(iop);
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate	if (!(iop->_flag & _IOWRT)) {
667c478bdstevel@tonic-gate		/* if no write flag */
677c478bdstevel@tonic-gate		if (iop->_flag & _IORW) {
687c478bdstevel@tonic-gate			/* if ok, cause read-write */
697c478bdstevel@tonic-gate			iop->_flag |= _IOWRT;
707c478bdstevel@tonic-gate		} else {
717c478bdstevel@tonic-gate			/* else error */
727c478bdstevel@tonic-gate			FUNLOCKFILE(lk);
737c478bdstevel@tonic-gate			errno = EBADF;
747c478bdstevel@tonic-gate			return (EOF);
757c478bdstevel@tonic-gate		}
767c478bdstevel@tonic-gate	}
777c478bdstevel@tonic-gate	count = _ndoprnt(format, ap, iop, 0);
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate	/* return EOF on error or EOF */
807c478bdstevel@tonic-gate	if (FERROR(iop) || count == EOF) {
817c478bdstevel@tonic-gate		FUNLOCKFILE(lk);
827c478bdstevel@tonic-gate		return (EOF);
837c478bdstevel@tonic-gate	}
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate	FUNLOCKFILE(lk);
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate	/* error on overflow */
887c478bdstevel@tonic-gate	if ((size_t)count > MAXINT) {
897c478bdstevel@tonic-gate		errno = EOVERFLOW;
907c478bdstevel@tonic-gate		return (EOF);
917c478bdstevel@tonic-gate	}
927c478bdstevel@tonic-gate	return ((int)count);
937c478bdstevel@tonic-gate}
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate#ifndef _LP64
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate/*
987c478bdstevel@tonic-gate * 32-bit shadow function of fprintf().
997c478bdstevel@tonic-gate * When using the c89 compiler to build 32-bit applications, the size
1007c478bdstevel@tonic-gate * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
1017c478bdstevel@tonic-gate * The shadow function uses 32-bit size of intmax_t for %j conversion.
1027c478bdstevel@tonic-gate * The #pragma redefine_extname in <stdio.h> selects the proper routine
1037c478bdstevel@tonic-gate * at compile time for the user application.
1047c478bdstevel@tonic-gate * NOTE: this function only exists in the 32-bit library.
1057c478bdstevel@tonic-gate */
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gateint
1087c478bdstevel@tonic-gate_fprintf_c89(FILE *iop, const char *format, ...)
1097c478bdstevel@tonic-gate{
1107c478bdstevel@tonic-gate	ssize_t count;
1117c478bdstevel@tonic-gate	va_list ap;
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gate	va_start(ap, format);
1147c478bdstevel@tonic-gate	count = _vfprintf_c89(iop, format, ap);
1157c478bdstevel@tonic-gate	va_end(ap);
1167c478bdstevel@tonic-gate	return ((int)count);
1177c478bdstevel@tonic-gate}
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate#endif  /* _LP64 */
120