Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

684 Te rminal I/O Chapter 18


All the flags listed specify one or morebits that we turn on or clear,unless we call
the flag amask.Amask defines multiple bits grouped together from which a set of
values is defined.We have a defined name for the mask and a name for each value. For
example, to set the character size, we first zerothe bits using the character-size mask
CSIZE,and then set one of the valuesCS5,CS6,CS7,orCS8.
The six delay values supported by Linux and Solaris arealso masks:BSDLY,CRDLY,
FFDLY,NLDLY,TABDLY,andVTDLY.Refer to thetermio(7I) manual page on Solaris
for the length of each delay value. In all cases, a delay mask of 0 means no delay.Ifa
delay is specified, theOFILLandOFDELflags determine whether the driver does an
actual delay or whether fill characters aretransmitted instead.

Example


Figure18.11demonstrates the use of these masks to extract a value and to set a value.
#include "apue.h"
#include <termios.h>
int
main(void)
{
struct termios term;
if (tcgetattr(STDIN_FILENO, &term) < 0)
err_sys("tcgetattr error");
switch (term.c_cflag & CSIZE) {
case CS5:
printf("5 bits/byte\n");
break;
case CS6:
printf("6 bits/byte\n");
break;
case CS7:
printf("7 bits/byte\n");
break;
case CS8:
printf("8 bits/byte\n");
break;
default:
printf("unknown bits/byte\n");
}
term.c_cflag &= ̃CSIZE; /* zero out the bits */
term.c_cflag |= CS8; /* set 8 bits/byte */
if (tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0)
err_sys("tcsetattr error");
exit(0);
}

Figure 18.11 Example oftcgetattrandtcsetattr
Free download pdf