aboutsummaryrefslogtreecommitdiff
path: root/sys/libc/cflsbuf.c
blob: 9547b0c253e479fe9932ecb4f0f7f5629eacb1d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/

#define	import_spp
#define	import_libc
#define	import_xnames
#define	import_stdio
#include <iraf.h>


/* C_FLSBUF -- Flush the FIO file buffer.  Called by the PUTC macro to flush
** the file buffer when it fills.  The function value returned is either the
** first char written to the buffer (passed as an argument) or EOF in the
** event of an error.
*/
int
c_flsbuf (
  unsigned int	ch,		/* char which caused the fault	*/
  FILE	*fp			/* output file			*/
)
{
	register int	buf_not_full;
	XINT	fd = fileno(fp);
	XINT	nreserve = 1;


	/* If we were called due to flush on newline and there is space in
	 * the buffer, put the ch in the buffer before flushing.
	 */
	buf_not_full = (fp->_iop < fp->_otop);
	if (buf_not_full)
	    Memc[fp->_iop++] = (unsigned)ch;

	iferr (FLSBUF (&fd, &nreserve)) {
	    fp->_fflags |= _FERR;
	    return (EOF);
	}

	if (!buf_not_full)
	    Memc[fp->_iop++] = (unsigned)ch;

	return (ch);
}