xref: /onnv-gate/usr/src/lib/libbc/libc/stdio/4.2/flsbuf.c (revision 722:636b850d4ee9)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate /*      Copyright (c) 1984 AT&T */
270Sstevel@tonic-gate /*        All Rights Reserved   */
280Sstevel@tonic-gate 
29*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*LINTLIBRARY*/
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include "../common/stdiom.h"
34*722Smuffin #include <errno.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <sys/stat.h>
37*722Smuffin #include <malloc.h>
38*722Smuffin #include <unistd.h>
390Sstevel@tonic-gate 
40*722Smuffin extern int	fclose();
410Sstevel@tonic-gate extern unsigned char (*_smbuf)[_SBFSIZ];
420Sstevel@tonic-gate 
43*722Smuffin void	_findbuf(FILE *);
44*722Smuffin void	_bufsync(FILE *);
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * Flush buffers on exit
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate void
_cleanup(void)51*722Smuffin _cleanup(void)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	_fwalk(fclose);
550Sstevel@tonic-gate }
560Sstevel@tonic-gate /*
57*722Smuffin  *	fclose() will flush (output) buffers for a buffered open
58*722Smuffin  *	FILE and then issue a system close on the _fileno.  The
59*722Smuffin  *	_base field will be reset to NULL for any but stdin and
60*722Smuffin  *	stdout, the _ptr field will be set the same as the _base
61*722Smuffin  *	field. The _flags and the _cnt field will be zeroed.
62*722Smuffin  *	If buffers had been obtained via malloc(), the space will
63*722Smuffin  *	be free()'d.  In case the FILE was not open, or fflush()
64*722Smuffin  *	or close() failed, an EOF will be returned, otherwise the
65*722Smuffin  *	return value is 0.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate int
fclose(FILE * iop)69*722Smuffin fclose(FILE *iop)
700Sstevel@tonic-gate {
71*722Smuffin 	int rtn=EOF;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if(iop == NULL)
740Sstevel@tonic-gate 		return(rtn);
750Sstevel@tonic-gate 	if(iop->_flag & (_IOREAD | _IOWRT | _IORW)
760Sstevel@tonic-gate 	   && (iop->_flag & _IOSTRG) == 0) {
770Sstevel@tonic-gate 		rtn = (iop->_flag & _IONBF)? 0: fflush(iop);
780Sstevel@tonic-gate 		if(close(fileno(iop)) < 0)
790Sstevel@tonic-gate 			rtn = EOF;
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	if(iop->_flag & _IOMYBUF) {
820Sstevel@tonic-gate 		free((char*)iop->_base);
830Sstevel@tonic-gate 		iop->_base = NULL;
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	iop->_flag = 0;
860Sstevel@tonic-gate 	iop->_cnt = 0;
870Sstevel@tonic-gate 	iop->_ptr = iop->_base;
880Sstevel@tonic-gate 	iop->_bufsiz = 0;
890Sstevel@tonic-gate 	return(rtn);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
93*722Smuffin  *	The fflush() routine must take care because of the
94*722Smuffin  *	possibility for recursion. The calling program might
95*722Smuffin  *	do IO in an interupt catching routine that is likely
96*722Smuffin  *	to interupt the write() call within fflush()
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate int
fflush(FILE * iop)100*722Smuffin fflush(FILE *iop)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	if (!(iop->_flag & _IOWRT)) {
1030Sstevel@tonic-gate 		return(0);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	while(!(iop->_flag & _IONBF) && (iop->_flag & _IOWRT) &&
1060Sstevel@tonic-gate 			(iop->_base != NULL) && (iop->_ptr > iop->_base) )
1070Sstevel@tonic-gate 		(void) _xflsbuf(iop);
1080Sstevel@tonic-gate 	return(ferror(iop) ? EOF : 0);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /* The routine _flsbuf may or may not actually flush the output buffer.  If
1120Sstevel@tonic-gate  * the file is line-buffered, the fact that iop->_cnt has run below zero
1130Sstevel@tonic-gate  * is meaningless: it is always kept below zero so that invocations of putc
1140Sstevel@tonic-gate  * will consistently give control to _flsbuf, even if the buffer is far from
1150Sstevel@tonic-gate  * full.  _flsbuf, on seeing the "line-buffered" flag, determines whether the
1160Sstevel@tonic-gate  * buffer is actually full by comparing iop->_ptr to the end of the buffer
1170Sstevel@tonic-gate  * iop->_base + iop->_bufsiz.  If it is full, or if an output line is
1180Sstevel@tonic-gate  * completed (with a newline), the buffer is flushed.  (Note: the character
1190Sstevel@tonic-gate  * argument to _flsbuf is not flushed with the current buffer if the buffer
1200Sstevel@tonic-gate  * is actually full -- it goes into the buffer after flushing.)
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate int
_flsbuf(unsigned char c,FILE * iop)124*722Smuffin _flsbuf(unsigned char c, FILE *iop)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate     unsigned char c1;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate     do {
1290Sstevel@tonic-gate 	/* check for linebuffered with write perm, but no EOF */
1300Sstevel@tonic-gate 	if ( (iop->_flag & (_IOLBF | _IOWRT | _IOEOF)) == (_IOLBF | _IOWRT) ) {
1310Sstevel@tonic-gate 		if ( iop->_ptr >= iop->_base + iop->_bufsiz )  /* if buffer full, */
1320Sstevel@tonic-gate 			break;		    /* exit do-while, and flush buf. */
1330Sstevel@tonic-gate 		if ( (*iop->_ptr++ = c) != '\n' )
1340Sstevel@tonic-gate 			return(c);
1350Sstevel@tonic-gate 		return(_xflsbuf(iop) == EOF ? EOF : c);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	/* write out an unbuffered file, if have write perm, but no EOF */
1380Sstevel@tonic-gate 	if ( (iop->_flag & (_IONBF | _IOWRT | _IOEOF)) == (_IONBF | _IOWRT) ) {
1390Sstevel@tonic-gate 		c1 = c;
1400Sstevel@tonic-gate 		iop->_cnt = 0;
1410Sstevel@tonic-gate 		if (write(fileno(iop), (char *) &c1, 1) == 1)
1420Sstevel@tonic-gate 			return(c);
1430Sstevel@tonic-gate 		iop->_flag |= _IOERR;
1440Sstevel@tonic-gate 		return(EOF);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	/* The _wrtchk call is here rather than at the top of _flsbuf to re- */
1470Sstevel@tonic-gate 	/* duce overhead for line-buffered I/O under normal circumstances.  */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if (_WRTCHK(iop))			/* is writing legitimate? */
1500Sstevel@tonic-gate 		return(EOF);
1510Sstevel@tonic-gate     } while ( (iop->_flag & (_IONBF | _IOLBF)) );
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate     (void) _xflsbuf(iop);   /* full buffer:  flush buffer */
1550Sstevel@tonic-gate     (void) putc((char) c, iop);  /* then put "c" in newly emptied buf */
1560Sstevel@tonic-gate 			/* (which, because of signals, may NOT be empty) */
1570Sstevel@tonic-gate     return( ferror(iop) ? EOF : c);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /* The function _xflsbuf writes out the current contents of the output
1610Sstevel@tonic-gate  * buffer delimited by iop->_base and iop->_ptr.
1620Sstevel@tonic-gate  * iop->_cnt is reset appropriately, but its value on entry to _xflsbuf
1630Sstevel@tonic-gate  * is ignored.
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  * The following code is not strictly correct.  If a signal is raised,
1660Sstevel@tonic-gate  * invoking a signal-handler which generates output into the same buffer
1670Sstevel@tonic-gate  * being flushed, a peculiar output sequence may result (for example,
1680Sstevel@tonic-gate  * the output generated by the signal-handler may appear twice).  At
1690Sstevel@tonic-gate  * present no means has been found to guarantee correct behavior without
1700Sstevel@tonic-gate  * resorting to the disabling of signals, a means considered too expensive.
1710Sstevel@tonic-gate  * For now the code has been written with the intent of reducing the
1720Sstevel@tonic-gate  * probability of strange effects and, when they do occur, of confining
1730Sstevel@tonic-gate  * the damage.  Except under extremely pathological circumstances, this
1740Sstevel@tonic-gate  * code should be expected to respect buffer boundaries even in the face
1750Sstevel@tonic-gate  * of interrupts and other signals.
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate int
_xflsbuf(FILE * iop)179*722Smuffin _xflsbuf(FILE *iop)
1800Sstevel@tonic-gate {
181*722Smuffin 	unsigned char *base;
182*722Smuffin 	int n;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	n = iop->_ptr - (base = iop->_base);
1850Sstevel@tonic-gate 	iop->_ptr = base;
1860Sstevel@tonic-gate 	iop->_cnt = (iop->_flag &(_IONBF | _IOLBF)) ? 0 : iop->_bufsiz;
1870Sstevel@tonic-gate 	_BUFSYNC(iop);
1880Sstevel@tonic-gate 	if (n > 0 && n != write(fileno(iop),(char*)base,(unsigned)n) )  {
1890Sstevel@tonic-gate 		iop->_flag |= _IOERR;
1900Sstevel@tonic-gate 		return(EOF);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 	return(0);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /* The function _wrtchk checks to see whether it is legitimate to write
1960Sstevel@tonic-gate  * to the specified device.  If it is, _wrtchk sets flags in iop->_flag for
1970Sstevel@tonic-gate  * writing, assures presence of a buffer, and returns 0.  If writing is not
1980Sstevel@tonic-gate  * legitimate, EOF is returned.
1990Sstevel@tonic-gate  */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate int
_wrtchk(FILE * iop)202*722Smuffin _wrtchk(FILE *iop)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	if ( (iop->_flag & (_IOWRT | _IOEOF)) != _IOWRT ) {
2050Sstevel@tonic-gate 		if (!(iop->_flag & (_IOWRT | _IORW)))
2060Sstevel@tonic-gate 			return(EOF);  /* bogus call--read-only file */
2070Sstevel@tonic-gate 		iop->_flag = iop->_flag & ~_IOEOF | _IOWRT; /* fix flags */
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 	if (iop->_flag & _IOSTRG)
2100Sstevel@tonic-gate 		return(0);	/* not our business to monkey with buffers or counts */
2110Sstevel@tonic-gate 	if (iop->_base == NULL)    /* this is first I/O to file--get buffer */
2120Sstevel@tonic-gate 		_findbuf(iop);
2130Sstevel@tonic-gate 	if (iop->_ptr == iop->_base && !(iop->_flag & (_IONBF | _IOLBF)) )  {
2140Sstevel@tonic-gate 		iop->_cnt = iop->_bufsiz; /* first write since seek--set cnt */
2150Sstevel@tonic-gate 		_BUFSYNC(iop);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	return(0);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * _findbuf, called only when iop->_base == NULL, locates a predefined buffer
2220Sstevel@tonic-gate  * or allocates a buffer using malloc.  If a buffer is obtained from malloc,
2230Sstevel@tonic-gate  * the _IOMYBUF flag is set in iop->_flag.
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate 
226*722Smuffin void
_findbuf(FILE * iop)227*722Smuffin _findbuf(FILE *iop)
2280Sstevel@tonic-gate {
229*722Smuffin 	int fno = fileno(iop); /* file number */
2300Sstevel@tonic-gate 	struct stat statb;
231*722Smuffin 	int size;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/* allocate a small block for unbuffered, large for buffered */
2340Sstevel@tonic-gate 	if (iop->_flag & _IONBF)  {
2350Sstevel@tonic-gate 		iop->_base = _smbuf[fno];
2360Sstevel@tonic-gate 		iop->_bufsiz = _SBFSIZ;
2370Sstevel@tonic-gate 	}  else  {
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		if ( isatty(fno) ) {
2400Sstevel@tonic-gate 			iop->_flag |= _IOLBF;
2410Sstevel@tonic-gate 			size = 128;
2420Sstevel@tonic-gate 		} else {
2430Sstevel@tonic-gate 			if (fstat(fno, &statb) < 0)
2440Sstevel@tonic-gate 				size = BUFSIZ;
2450Sstevel@tonic-gate 			else {
2460Sstevel@tonic-gate 				if ((size = statb.st_blksize) <= 0)
2470Sstevel@tonic-gate 					size = BUFSIZ;
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		if ((iop->_base = (unsigned char *) malloc(size+8)) != NULL) {
2510Sstevel@tonic-gate 			/* if  we got a buffer */
2520Sstevel@tonic-gate 			iop->_flag |= _IOMYBUF;
2530Sstevel@tonic-gate 			iop->_bufsiz = size;
2540Sstevel@tonic-gate 		} else {
2550Sstevel@tonic-gate 			/* if no room for buffer, use small buffer */
2560Sstevel@tonic-gate 			iop->_base = _smbuf[fno];
2570Sstevel@tonic-gate 			iop->_bufsiz = _SBFSIZ;
2580Sstevel@tonic-gate 			iop->_flag &= ~_IOLBF;
2590Sstevel@tonic-gate 			iop->_flag |= _IONBF;
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	iop->_ptr = iop->_base;
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
265*722Smuffin /*
266*722Smuffin  * The function _bufsync is called because interrupts and other signals
2670Sstevel@tonic-gate  * which occur in between the decrementing of iop->_cnt and the incrementing
2680Sstevel@tonic-gate  * of iop->_ptr, or in other contexts as well, may upset the synchronization
2690Sstevel@tonic-gate  * of iop->_cnt and iop->ptr.  If this happens, calling _bufsync should
2700Sstevel@tonic-gate  * resynchronize the two quantities (this is not always possible).  Resyn-
2710Sstevel@tonic-gate  * chronization guarantees that putc invocations will not write beyond
2720Sstevel@tonic-gate  * the end of the buffer.  Note that signals during _bufsync can cause
2730Sstevel@tonic-gate  * _bufsync to do the wrong thing, but usually with benign effects.
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate 
276*722Smuffin void
_bufsync(FILE * iop)277*722Smuffin _bufsync(FILE *iop)
2780Sstevel@tonic-gate {
279*722Smuffin 	int spaceleft;
280*722Smuffin 	unsigned char *bufend = iop->_base + iop->_bufsiz;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if ((spaceleft = bufend - iop->_ptr) < 0)
2830Sstevel@tonic-gate 		iop->_ptr = bufend;
2840Sstevel@tonic-gate 	else if (spaceleft < iop->_cnt)
2850Sstevel@tonic-gate 		iop->_cnt = spaceleft;
2860Sstevel@tonic-gate }
287