aboutsummaryrefslogtreecommitdiff
path: root/sys/fio/flsbuf.x
blob: 6f65157791946876af737ce5cce9cfe9bac784aa (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<config.h>
include	<syserr.h>
include	<fio.h>

# FLSBUF -- Flush the file buffer.  Called by PUTC, PUTLINE, and WRITE
# when the i/o pointer no longer points into the file buffer.  Prior to
# the first write to a buffer, the OTOP pointer will be set to the 
# beginning of the buffer.  The first call to FLSBUF advances OTOP to the
# end of the buffer.  The next call to FLSBUF finds the buffer "dirty",
# and flushes the buffer, leaving the buffer ready to be written into
# (OTOP left pointing at the end of the buffer).  A seek on a binary file
# will usually leave the i/o pointer pointing outside the buffer, which
# requires a call to FFAULT (file fault).

procedure flsbuf (fd, nreserve)

int	fd, nreserve
pointer	bp
bool	iop_in_range
int	nchars_written, ffault(), and()
errchk	fmkbfs, syserr, filerr, ffault
include	<fio.com>

begin
	fp = fiodes[fd]
	bp = bufptr[fd]

	if (fd <= 0 || fp == NULL)			# verification
	    call syserr (SYS_FILENOTOPEN)
	else if (and (FF_WRITE, fflags[fd]) == 0) {
	    if (FTYPE(fp) == SPOOL_FILE) {
		if (otop[fd] < buftop[fd])
		    otop[fd] = buftop[fd]
		else
		    call fexbuf (fd)
		return
	    } else
		call filerr (FNAME(fp), SYS_FNOWRITEPERM)
	}

	iop_in_range = iop[fd] >= bufptr[fd] && iop[fd] < buftop[fd]

	if (bp == NULL) {				# no buffer yet
	    call fmkbfs (fd)
	    bp = bufptr[fd]
	    itop[fd] = bp
	    if (FTYPE(fp) == BINARY_FILE)
		nchars_written = ffault (fd, LNOTE(fd), nreserve, FF_WRITE)
	    else
		nchars_written = 0
					
	} else if (iop_in_range && otop[fd] < buftop[fd]) {
	    nchars_written = 0				# buffer not full yet?

	} else if (FTYPE(fp) == TEXT_FILE) {		# text files
	    call fputtx (fd, Memc[bp], iop[fd] - bp, nchars_written)
	    iop[fd] = bp
	    itop[fd] = bp

	} else						# binary files
	    nchars_written = ffault (fd, LNOTE(fd), nreserve, FF_WRITE)

	otop[fd] = buftop[fd]				# make space available

	if (nchars_written == ERR)
	    call filerr (FNAME(fp), SYS_FWRITE)
end