xref: /onnv-gate/usr/src/lib/libbc/libc/stdio/sys5/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 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*LINTLIBRARY*/
300Sstevel@tonic-gate #include <stdio.h>
31*722Smuffin #include <errno.h>
320Sstevel@tonic-gate #include "../common/stdiom.h"
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <unistd.h>
36*722Smuffin #include <malloc.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate extern unsigned char (*_smbuf)[_SBFSIZ];
390Sstevel@tonic-gate 
40*722Smuffin void	_findbuf(FILE *);
41*722Smuffin void	_bufsync(FILE *);
42*722Smuffin 
43*722Smuffin extern int fclose();
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Flush buffers on exit
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate void
_cleanup(void)49*722Smuffin _cleanup(void)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	_fwalk(fclose);
530Sstevel@tonic-gate }
54*722Smuffin 
550Sstevel@tonic-gate /*
56*722Smuffin  *	fclose() will flush (output) buffers for a buffered open
57*722Smuffin  *	FILE and then issue a system close on the _fileno.  The
58*722Smuffin  *	_base field will be reset to NULL for any but stdin and
59*722Smuffin  *	stdout, the _ptr field will be set the same as the _base
60*722Smuffin  *	field. The _flags and the _cnt field will be zeroed.
61*722Smuffin  *	If buffers had been obtained via malloc(), the space will
62*722Smuffin  *	be free()'d.  In case the FILE was not open, or fflush()
63*722Smuffin  *	or close() failed, an EOF will be returned, otherwise the
64*722Smuffin  *	return value is 0.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate int
fclose(FILE * iop)67*722Smuffin fclose(FILE *iop)
680Sstevel@tonic-gate {
69*722Smuffin 	int rtn=EOF;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	if(iop == NULL)
720Sstevel@tonic-gate 		return(rtn);
730Sstevel@tonic-gate 	if(iop->_flag & (_IOREAD | _IOWRT | _IORW)
740Sstevel@tonic-gate 	   && (iop->_flag & _IOSTRG) == 0) {
750Sstevel@tonic-gate 		rtn = (iop->_flag & _IONBF)? 0: fflush(iop);
760Sstevel@tonic-gate 		if(close(fileno(iop)) < 0)
770Sstevel@tonic-gate 			rtn = EOF;
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	if(iop->_flag & _IOMYBUF) {
800Sstevel@tonic-gate 		free((char*)iop->_base);
810Sstevel@tonic-gate 		iop->_base = NULL;
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	iop->_flag = 0;
840Sstevel@tonic-gate 	iop->_cnt = 0;
850Sstevel@tonic-gate 	iop->_ptr = iop->_base;
860Sstevel@tonic-gate 	iop->_bufsiz = 0;
870Sstevel@tonic-gate 	return(rtn);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
91*722Smuffin  *	The fflush() routine must take care because of the
92*722Smuffin  *	possibility for recursion. The calling program might
93*722Smuffin  *	do IO in an interupt catching routine that is likely
94*722Smuffin  *	to interupt the write() call within fflush()
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate int
fflush(FILE * iop)98*722Smuffin fflush(FILE *iop)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	if (!(iop->_flag & _IOWRT)) {
1010Sstevel@tonic-gate 		if ((iop->_base != NULL) && iop->_cnt) {
1020Sstevel@tonic-gate 			lseek(iop->_file, -(iop->_cnt), SEEK_CUR);
1030Sstevel@tonic-gate 			iop->_cnt = 0;
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 		return(0);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	while(!(iop->_flag & _IONBF) && (iop->_flag & _IOWRT) &&
1080Sstevel@tonic-gate 			(iop->_base != NULL) && (iop->_ptr > iop->_base) )
1090Sstevel@tonic-gate 		(void) _xflsbuf(iop);
1100Sstevel@tonic-gate 	return(ferror(iop) ? EOF : 0);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
113*722Smuffin /*
114*722Smuffin  * The routine _flsbuf may or may not actually flush the output buffer.  If
1150Sstevel@tonic-gate  * the file is line-buffered, the fact that iop->_cnt has run below zero
1160Sstevel@tonic-gate  * is meaningless: it is always kept below zero so that invocations of putc
1170Sstevel@tonic-gate  * will consistently give control to _flsbuf, even if the buffer is far from
1180Sstevel@tonic-gate  * full.  _flsbuf, on seeing the "line-buffered" flag, determines whether the
1190Sstevel@tonic-gate  * buffer is actually full by comparing iop->_ptr to the end of the buffer
1200Sstevel@tonic-gate  * iop->_base + iop->_bufsiz.  If it is full, or if an output line is
1210Sstevel@tonic-gate  * completed (with a newline), the buffer is flushed.  (Note: the character
1220Sstevel@tonic-gate  * argument to _flsbuf is not flushed with the current buffer if the buffer
1230Sstevel@tonic-gate  * is actually full -- it goes into the buffer after flushing.)
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate int
_flsbuf(unsigned char c,FILE * iop)127*722Smuffin _flsbuf(unsigned char c, FILE *iop)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate     unsigned char c1;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate     do {
1320Sstevel@tonic-gate 	/* check for linebuffered with write perm, but no EOF */
1330Sstevel@tonic-gate 	if ( (iop->_flag & (_IOLBF | _IOWRT | _IOEOF)) == (_IOLBF | _IOWRT) ) {
1340Sstevel@tonic-gate 		if ( iop->_ptr >= iop->_base + iop->_bufsiz )  /* if buffer full, */
1350Sstevel@tonic-gate 			break;		    /* exit do-while, and flush buf. */
1360Sstevel@tonic-gate 		if ( (*iop->_ptr++ = c) != '\n' )
1370Sstevel@tonic-gate 			return(c);
1380Sstevel@tonic-gate 		return(_xflsbuf(iop) == EOF ? EOF : c);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	/* write out an unbuffered file, if have write perm, but no EOF */
1410Sstevel@tonic-gate 	if ( (iop->_flag & (_IONBF | _IOWRT | _IOEOF)) == (_IONBF | _IOWRT) ) {
1420Sstevel@tonic-gate 		c1 = c;
1430Sstevel@tonic-gate 		iop->_cnt = 0;
1440Sstevel@tonic-gate 		if (write(fileno(iop), (char *) &c1, 1) == 1)
1450Sstevel@tonic-gate 			return(c);
1460Sstevel@tonic-gate 		iop->_flag |= _IOERR;
1470Sstevel@tonic-gate 		return(EOF);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 	/* The _wrtchk call is here rather than at the top of _flsbuf to re- */
1500Sstevel@tonic-gate 	/* duce overhead for line-buffered I/O under normal circumstances.  */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (_WRTCHK(iop))			/* is writing legitimate? */
1530Sstevel@tonic-gate 		return(EOF);
1540Sstevel@tonic-gate     } while ( (iop->_flag & (_IONBF | _IOLBF)) );
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate     (void) _xflsbuf(iop);   /* full buffer:  flush buffer */
1580Sstevel@tonic-gate     (void) putc((char) c, iop);  /* then put "c" in newly emptied buf */
1590Sstevel@tonic-gate 			/* (which, because of signals, may NOT be empty) */
1600Sstevel@tonic-gate     return( ferror(iop) ? EOF : c);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
163*722Smuffin /*
164*722Smuffin  * The function _xflsbuf writes out the current contents of the output
1650Sstevel@tonic-gate  * buffer delimited by iop->_base and iop->_ptr.
1660Sstevel@tonic-gate  * iop->_cnt is reset appropriately, but its value on entry to _xflsbuf
1670Sstevel@tonic-gate  * is ignored.
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  * The following code is not strictly correct.  If a signal is raised,
1700Sstevel@tonic-gate  * invoking a signal-handler which generates output into the same buffer
1710Sstevel@tonic-gate  * being flushed, a peculiar output sequence may result (for example,
1720Sstevel@tonic-gate  * the output generated by the signal-handler may appear twice).  At
1730Sstevel@tonic-gate  * present no means has been found to guarantee correct behavior without
1740Sstevel@tonic-gate  * resorting to the disabling of signals, a means considered too expensive.
1750Sstevel@tonic-gate  * For now the code has been written with the intent of reducing the
1760Sstevel@tonic-gate  * probability of strange effects and, when they do occur, of confining
1770Sstevel@tonic-gate  * the damage.  Except under extremely pathological circumstances, this
1780Sstevel@tonic-gate  * code should be expected to respect buffer boundaries even in the face
1790Sstevel@tonic-gate  * of interrupts and other signals.
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate int
_xflsbuf(FILE * iop)183*722Smuffin _xflsbuf(FILE *iop)
1840Sstevel@tonic-gate {
185*722Smuffin 	unsigned char *base;
186*722Smuffin 	int n;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	n = iop->_ptr - (base = iop->_base);
1890Sstevel@tonic-gate 	iop->_ptr = base;
1900Sstevel@tonic-gate 	iop->_cnt = (iop->_flag &(_IONBF | _IOLBF)) ? 0 : iop->_bufsiz;
1910Sstevel@tonic-gate 	_BUFSYNC(iop);
1920Sstevel@tonic-gate 	if (n > 0 && n != write(fileno(iop),(char*)base,(unsigned)n) )  {
1930Sstevel@tonic-gate 		iop->_flag |= _IOERR;
1940Sstevel@tonic-gate 		return(EOF);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	return(0);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
199*722Smuffin /*
200*722Smuffin  * The function _wrtchk checks to see whether it is legitimate to write
2010Sstevel@tonic-gate  * to the specified device.  If it is, _wrtchk sets flags in iop->_flag for
2020Sstevel@tonic-gate  * writing, assures presence of a buffer, and returns 0.  If writing is not
2030Sstevel@tonic-gate  * legitimate, EOF is returned.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate int
_wrtchk(FILE * iop)207*722Smuffin _wrtchk(FILE *iop)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	if ( (iop->_flag & (_IOWRT | _IOEOF)) != _IOWRT ) {
2100Sstevel@tonic-gate 		if (!(iop->_flag & (_IOWRT | _IORW)))
2110Sstevel@tonic-gate 			return(EOF);  /* bogus call--read-only file */
2120Sstevel@tonic-gate 		iop->_flag = iop->_flag & ~_IOEOF | _IOWRT; /* fix flags */
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	if (iop->_flag & _IOSTRG)
2150Sstevel@tonic-gate 		return(0);	/* not our business to monkey with buffers or counts */
2160Sstevel@tonic-gate 	if (iop->_base == NULL)    /* this is first I/O to file--get buffer */
2170Sstevel@tonic-gate 		_findbuf(iop);
2180Sstevel@tonic-gate 	if (iop->_ptr == iop->_base && !(iop->_flag & (_IONBF | _IOLBF)) )  {
2190Sstevel@tonic-gate 		iop->_cnt = iop->_bufsiz; /* first write since seek--set cnt */
2200Sstevel@tonic-gate 		_BUFSYNC(iop);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 	return(0);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * _findbuf, called only when iop->_base == NULL, locates a predefined buffer
2270Sstevel@tonic-gate  * or allocates a buffer using malloc.  If a buffer is obtained from malloc,
2280Sstevel@tonic-gate  * the _IOMYBUF flag is set in iop->_flag.
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate 
231*722Smuffin void
_findbuf(FILE * iop)232*722Smuffin _findbuf(FILE *iop)
2330Sstevel@tonic-gate {
234*722Smuffin 	int fno = fileno(iop); /* file number */
2350Sstevel@tonic-gate 	struct stat statb;
236*722Smuffin 	int size;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/* allocate a small block for unbuffered, large for buffered */
2390Sstevel@tonic-gate 	if (iop->_flag & _IONBF)  {
2400Sstevel@tonic-gate 		iop->_base = _smbuf[fno];
2410Sstevel@tonic-gate 		iop->_bufsiz = _SBFSIZ;
2420Sstevel@tonic-gate 	}  else  {
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		if ( isatty(fno) ) {
2450Sstevel@tonic-gate 			iop->_flag |= _IOLBF;
2460Sstevel@tonic-gate 			size = 128;
2470Sstevel@tonic-gate 		} else {
2480Sstevel@tonic-gate 			if (fstat(fno, &statb) < 0)
2490Sstevel@tonic-gate 				size = BUFSIZ;
2500Sstevel@tonic-gate 			else {
2510Sstevel@tonic-gate 				if ((size = statb.st_blksize) <= 0)
2520Sstevel@tonic-gate 					size = BUFSIZ;
2530Sstevel@tonic-gate 			}
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 		if ((iop->_base = (unsigned char *) malloc(size+8)) != NULL) {
2560Sstevel@tonic-gate 			/* if  we got a buffer */
2570Sstevel@tonic-gate 			iop->_flag |= _IOMYBUF;
2580Sstevel@tonic-gate 			iop->_bufsiz = size;
2590Sstevel@tonic-gate 		} else {
2600Sstevel@tonic-gate 			/* if no room for buffer, use small buffer */
2610Sstevel@tonic-gate 			iop->_base = _smbuf[fno];
2620Sstevel@tonic-gate 			iop->_bufsiz = _SBFSIZ;
2630Sstevel@tonic-gate 			iop->_flag &= ~_IOLBF;
2640Sstevel@tonic-gate 			iop->_flag |= _IONBF;
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	iop->_ptr = iop->_base;
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
270*722Smuffin /*
271*722Smuffin  * The function _bufsync is called because interrupts and other signals
2720Sstevel@tonic-gate  * which occur in between the decrementing of iop->_cnt and the incrementing
2730Sstevel@tonic-gate  * of iop->_ptr, or in other contexts as well, may upset the synchronization
2740Sstevel@tonic-gate  * of iop->_cnt and iop->ptr.  If this happens, calling _bufsync should
2750Sstevel@tonic-gate  * resynchronize the two quantities (this is not always possible).  Resyn-
2760Sstevel@tonic-gate  * chronization guarantees that putc invocations will not write beyond
2770Sstevel@tonic-gate  * the end of the buffer.  Note that signals during _bufsync can cause
2780Sstevel@tonic-gate  * _bufsync to do the wrong thing, but usually with benign effects.
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate 
281*722Smuffin void
_bufsync(FILE * iop)282*722Smuffin _bufsync(FILE *iop)
2830Sstevel@tonic-gate {
284*722Smuffin 	int spaceleft;
285*722Smuffin 	unsigned char *bufend = iop->_base + iop->_bufsiz;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if ((spaceleft = bufend - iop->_ptr) < 0)
2880Sstevel@tonic-gate 		iop->_ptr = bufend;
2890Sstevel@tonic-gate 	else if (spaceleft < iop->_cnt)
2900Sstevel@tonic-gate 		iop->_cnt = spaceleft;
2910Sstevel@tonic-gate }
292