ptg10805159704 Te rminal I/O Chapter 18
Case C: MIN == 0, TIME > 0
TIME specifies a read timer that is started whenreadis called. (Comparethis
to case A, in which a nonzeroTIME represented an interbyte timer that was not
started until the first byte was received.) Thereadreturns when a single byte
is received or when the timer expires. If the timer expires,readreturns 0.
Case D: MIN == 0, TIME == 0
If some data is available,readreturns up to the number of bytes requested. If
no data is available,readreturns 0 immediately.Realize in all these cases that MIN is only a minimum. If the program requests more
than MIN bytes of data, it’s possible to receive up to the requested amount. This also
applies to cases C and D, in which MIN is 0.
Figure18.19 summarizes the four cases for noncanonical input. In this figure,nbytes
is the thirdargument toread(the maximum number of bytes to return).MIN > 0 MIN == 0TIME > 0A:readreturns[MIN,nbytes]
beforetimer expires;
readreturns [1, MIN)
if timer expires.
(TIME = interbyte timer.
Caller can block indefinitely.)C:readreturns[1,nbytes]
beforetimer expires;
readreturns 0
if timer expires.
(TIME =readtimer.)TIME == 0B:readreturns[MIN,nbytes]
when available.
(Caller can block indefinitely.)D:readreturns[0,nbytes]
immediately.Figure 18.19 Four cases for noncanonical inputBe awarethat POSIX.1 allows the subscriptsVMINandVTIMEto have the same values asVEOF
andVEOL,respectively.Indeed, Solaris does this for backwardcompatibility with older
versions of System V.This creates a portability problem, however.Ingoing from
noncanonical to canonical mode, we must now restoreVEOFandVEOLas well. IfVMINequals
VEOFand we don’t restoretheir values, when we setVMINto its typical value of 1, the
end-of-file character becomes Control-A. The easiest way around this problem is to save the
entiretermiosstructurewhen going into noncanonical mode and restore it when going back
to canonical mode.Example
The program in Figure18.20 defines thetty_cbreakandtty_rawfunctions that set
the terminal incbreak modeandraw mode.(The termscbreakandrawcome from the
Version 7 terminal driver.) Wecan reset the terminal to its original state (the state before
either of these functions was called) by calling the functiontty_reset.
If we’ve calledtty_cbreak, we need to calltty_resetbeforecallingtty_raw.
The same goes for callingtty_cbreakafter callingtty_raw.This improves the
chances that the terminal will be left in a usable state if we encounter any errors.