17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5a5f69788Scraigm * Common Development and Distribution License (the "License").
6a5f69788Scraigm * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21a5f69788Scraigm
227c478bd9Sstevel@tonic-gate /*
23a574db85Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307257d1b4Sraf #pragma weak __flsbuf = _flsbuf
317257d1b4Sraf
327257d1b4Sraf #include "lint.h"
337c478bd9Sstevel@tonic-gate #include "file64.h"
347c478bd9Sstevel@tonic-gate #include <mtlib.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <thread.h>
377c478bd9Sstevel@tonic-gate #include <synch.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include "stdiom.h"
417c478bd9Sstevel@tonic-gate
42*cd62a92dSRobert Mustacchi /*
43*cd62a92dSRobert Mustacchi * flush (write) buffer, save ch,
44*cd62a92dSRobert Mustacchi * return EOF on failure
45*cd62a92dSRobert Mustacchi */
467c478bd9Sstevel@tonic-gate int
_flsbuf(int ch,FILE * iop)47*cd62a92dSRobert Mustacchi _flsbuf(int ch, FILE *iop)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate Uchar uch;
507c478bd9Sstevel@tonic-gate
51*cd62a92dSRobert Mustacchi do { /* only loop if need to use _wrtchk() on non-_IOFBF */
527c478bd9Sstevel@tonic-gate switch (iop->_flag & (_IOFBF | _IOLBF | _IONBF |
53*cd62a92dSRobert Mustacchi _IOWRT | _IOEOF)) {
547c478bd9Sstevel@tonic-gate case _IOFBF | _IOWRT: /* okay to do full-buffered case */
557c478bd9Sstevel@tonic-gate if (iop->_base != 0 && iop->_ptr > iop->_base)
567c478bd9Sstevel@tonic-gate goto flush_putc; /* skip _wrtchk() */
577c478bd9Sstevel@tonic-gate break;
587c478bd9Sstevel@tonic-gate case _IOLBF | _IOWRT: /* okay to do line-buffered case */
597c478bd9Sstevel@tonic-gate if (iop->_ptr >= _bufend(iop))
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * which will recursively call
627c478bd9Sstevel@tonic-gate * __flsbuf via putc because of no room
637c478bd9Sstevel@tonic-gate * in the buffer for the character
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate goto flush_putc;
667c478bd9Sstevel@tonic-gate if ((*iop->_ptr++ = (unsigned char)ch) == '\n')
677c478bd9Sstevel@tonic-gate (void) _xflsbuf(iop);
687c478bd9Sstevel@tonic-gate iop->_cnt = 0;
697c478bd9Sstevel@tonic-gate goto out;
707c478bd9Sstevel@tonic-gate case _IONBF | _IOWRT: /* okay to do no-buffered case */
717c478bd9Sstevel@tonic-gate iop->_cnt = 0;
727c478bd9Sstevel@tonic-gate uch = (unsigned char)ch;
73*cd62a92dSRobert Mustacchi if (_xwrite(iop, (char *)&uch, 1) != 1) {
74a574db85Sraf if (!cancel_active())
757c478bd9Sstevel@tonic-gate iop->_flag |= _IOERR;
76a574db85Sraf return (EOF);
77a574db85Sraf }
787c478bd9Sstevel@tonic-gate goto out;
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate if (_wrtchk(iop) != 0) /* check, correct permissions */
817c478bd9Sstevel@tonic-gate return (EOF);
827c478bd9Sstevel@tonic-gate } while (iop->_flag & (_IOLBF | _IONBF));
83a574db85Sraf flush_putc:
847c478bd9Sstevel@tonic-gate (void) _xflsbuf(iop);
857c478bd9Sstevel@tonic-gate (void) PUTC(ch, iop); /* recursive call */
86a574db85Sraf out:
877c478bd9Sstevel@tonic-gate /* necessary for putc() */
887c478bd9Sstevel@tonic-gate return ((iop->_flag & _IOERR) ? EOF : (unsigned char)ch);
897c478bd9Sstevel@tonic-gate }
90