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#include "lint.h"
337c478bdstevel@tonic-gate#include <mtlib.h>
347c478bdstevel@tonic-gate#include <stdarg.h>
357c478bdstevel@tonic-gate#include <errno.h>
367c478bdstevel@tonic-gate#include <thread.h>
377c478bdstevel@tonic-gate#include <synch.h>
387c478bdstevel@tonic-gate#include <values.h>
397c478bdstevel@tonic-gate#include "print.h"
407c478bdstevel@tonic-gate#include <sys/types.h>
417c478bdstevel@tonic-gate#include "libc.h"
427c478bdstevel@tonic-gate#include "mse.h"
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate/*VARARGS1*/
457c478bdstevel@tonic-gateint
467c478bdstevel@tonic-gateprintf(const char *format, ...)
477c478bdstevel@tonic-gate{
487c478bdstevel@tonic-gate	ssize_t count;
497c478bdstevel@tonic-gate	rmutex_t *lk;
507c478bdstevel@tonic-gate	va_list ap;
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate	va_start(ap, format);
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate	/* Use F*LOCKFILE() macros because printf() is not async-safe. */
557c478bdstevel@tonic-gate	FLOCKFILE(lk, stdout);
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate	_SET_ORIENTATION_BYTE(stdout);
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate	if (!(stdout->_flag & _IOWRT)) {
607c478bdstevel@tonic-gate		/* if no write flag */
617c478bdstevel@tonic-gate		if (stdout->_flag & _IORW) {
627c478bdstevel@tonic-gate			/* if ok, cause read-write */
637c478bdstevel@tonic-gate			stdout->_flag |= _IOWRT;
647c478bdstevel@tonic-gate		} else {
657c478bdstevel@tonic-gate			/* else error */
667c478bdstevel@tonic-gate			FUNLOCKFILE(lk);
677c478bdstevel@tonic-gate			errno = EBADF;
687c478bdstevel@tonic-gate			return (EOF);
697c478bdstevel@tonic-gate		}
707c478bdstevel@tonic-gate	}
717c478bdstevel@tonic-gate	count = _ndoprnt(format, ap, stdout, 0);
727c478bdstevel@tonic-gate	va_end(ap);
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate	/* check for errors or EOF */
757c478bdstevel@tonic-gate	if (FERROR(stdout) || count ==  EOF) {
767c478bdstevel@tonic-gate		FUNLOCKFILE(lk);
777c478bdstevel@tonic-gate		return (EOF);
787c478bdstevel@tonic-gate	}
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gate	FUNLOCKFILE(lk);
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate	/* check for overflow */
837c478bdstevel@tonic-gate	if ((size_t)count > MAXINT) {
847c478bdstevel@tonic-gate		errno = EOVERFLOW;
857c478bdstevel@tonic-gate		return (EOF);
867c478bdstevel@tonic-gate	} else {
877c478bdstevel@tonic-gate		return ((int)count);
887c478bdstevel@tonic-gate	}
897c478bdstevel@tonic-gate}
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate#ifndef _LP64
927c478bdstevel@tonic-gate
937c478bdstevel@tonic-gate/*
947c478bdstevel@tonic-gate * 32-bit shadow function of printf().
957c478bdstevel@tonic-gate * When using the c89 compiler to build 32-bit applications, the size
967c478bdstevel@tonic-gate * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
977c478bdstevel@tonic-gate * The shadow function uses 32-bit size of intmax_t for %j conversion.
987c478bdstevel@tonic-gate * The #pragma redefine_extname in <stdio.h> selects the proper routine
997c478bdstevel@tonic-gate * at compile time for the user application.
1007c478bdstevel@tonic-gate * NOTE: this function only exists in the 32-bit library.
1017c478bdstevel@tonic-gate */
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gateint
1047c478bdstevel@tonic-gate_printf_c89(const char *format, ...)
1057c478bdstevel@tonic-gate{
1067c478bdstevel@tonic-gate	ssize_t count;
1077c478bdstevel@tonic-gate	va_list ap;
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gate	va_start(ap, format);
1107c478bdstevel@tonic-gate	count = _vprintf_c89(format, ap);
1117c478bdstevel@tonic-gate	va_end(ap);
1127c478bdstevel@tonic-gate	return ((int)count);
1137c478bdstevel@tonic-gate}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate#endif
116