xref: /onnv-gate/usr/src/lib/libc/port/stdio/_flsbuf.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51846Scraigm  * Common Development and Distribution License (the "License").
61846Scraigm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211846Scraigm 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
301846Scraigm #pragma ident	"%Z%%M%	%I%	%E% SMI"
311846Scraigm 
32*6812Sraf #pragma weak __flsbuf = _flsbuf
33*6812Sraf 
34*6812Sraf #include "lint.h"
350Sstevel@tonic-gate #include "file64.h"
360Sstevel@tonic-gate #include <mtlib.h>
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <thread.h>
390Sstevel@tonic-gate #include <synch.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include "stdiom.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate int
_flsbuf(int ch,FILE * iop)450Sstevel@tonic-gate _flsbuf(int ch, FILE *iop)	/* flush (write) buffer, save ch, */
460Sstevel@tonic-gate 				/* return EOF on failure */
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	Uchar uch;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	do	/* only loop if need to use _wrtchk() on non-_IOFBF */
510Sstevel@tonic-gate 	{
520Sstevel@tonic-gate 		switch (iop->_flag & (_IOFBF | _IOLBF | _IONBF |
530Sstevel@tonic-gate 				_IOWRT | _IOEOF))
540Sstevel@tonic-gate 		{
550Sstevel@tonic-gate 		case _IOFBF | _IOWRT:	/* okay to do full-buffered case */
560Sstevel@tonic-gate 			if (iop->_base != 0 && iop->_ptr > iop->_base)
570Sstevel@tonic-gate 				goto flush_putc;	/* skip _wrtchk() */
580Sstevel@tonic-gate 			break;
590Sstevel@tonic-gate 		case _IOLBF | _IOWRT:	/* okay to do line-buffered case */
600Sstevel@tonic-gate 			if (iop->_ptr >= _bufend(iop))
610Sstevel@tonic-gate 				/*
620Sstevel@tonic-gate 				 * which will recursively call
630Sstevel@tonic-gate 				 * __flsbuf via putc because of no room
640Sstevel@tonic-gate 				 * in the buffer for the character
650Sstevel@tonic-gate 				 */
660Sstevel@tonic-gate 				goto flush_putc;
670Sstevel@tonic-gate 			if ((*iop->_ptr++ = (unsigned char)ch) == '\n')
680Sstevel@tonic-gate 				(void) _xflsbuf(iop);
690Sstevel@tonic-gate 			iop->_cnt = 0;
700Sstevel@tonic-gate 			goto out;
710Sstevel@tonic-gate 		case _IONBF | _IOWRT:	/* okay to do no-buffered case */
720Sstevel@tonic-gate 			iop->_cnt = 0;
730Sstevel@tonic-gate 			uch = (unsigned char)ch;
745891Sraf 			if (write(GET_FD(iop), (char *)&uch, 1) != 1) {
755891Sraf 				if (!cancel_active())
765891Sraf 					iop->_flag |= _IOERR;
775891Sraf 				return (EOF);
785891Sraf 			}
790Sstevel@tonic-gate 			goto out;
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 		if (_wrtchk(iop) != 0)	/* check, correct permissions */
820Sstevel@tonic-gate 			return (EOF);
830Sstevel@tonic-gate 	} while (iop->_flag & (_IOLBF | _IONBF));
845891Sraf flush_putc:
850Sstevel@tonic-gate 	(void) _xflsbuf(iop);
860Sstevel@tonic-gate 	(void) PUTC(ch, iop); /*  recursive call */
875891Sraf out:
885891Sraf 	/* necessary for putc() */
890Sstevel@tonic-gate 	return ((iop->_flag & _IOERR) ? EOF : (unsigned char)ch);
900Sstevel@tonic-gate }
91