ptg10805159
692 Te rminal I/O Chapter 18
Option names preceded by a hyphen aredisabled. The last four lines display the
current settings for each of the terminal special characters (Section 18.3). The first line
displays the number of rows and columns for the current terminal window; we discuss
the terminal window size in Section 18.12.
Thesttycommand uses its standardinput to get and set the terminal option flags. Although
some older implementations used standardoutput, POSIX.1 requires that the standardinput
be used. All four implementations discussed in this text provide versions ofsttythat operate
on the standardinput. This means that we can type
stty -a </dev/tty1a
if we areinterested in discovering the settings on the terminal namedtty1a.
18.7 Baud Rate Functions
The termbaud rateis a historical term that should be referred to today as ‘‘bits per
second.’’Although most terminal devices use the same baud rate for both input and
output, the capability exists to set the two rates to different values, if the hardware
allows this.
#include <termios.h>
speed_t cfgetispeed(const struct termios *termptr);
speed_t cfgetospeed(const struct termios *termptr);
Both return: baud rate value
int cfsetispeed(struct termios *termptr,speed_tspeed);
int cfsetospeed(struct termios *termptr,speed_tspeed);
Both return: 0 if OK,−1 on error
The return value from the twocfgetfunctions and thespeedargument to the two
cfsetfunctions areone of the following constants:B50,B75,B110,B134,B150,B200,
B300,B600,B1200,B1800,B2400,B4800,B9600,B19200,orB38400.The constant
B0means ‘‘hang up.’’IfB0is specified as the output baud rate whentcsetattris
called, the modem control lines are no longer asserted.
Most systems define additional baud rate values, such asB57600andB115200.
To use these functions, we must realize that the input and output baud rates are
stored in the device’stermiosstructure, as shown in Figure18.8. Beforecalling either
of thecfgetfunctions, we first have to obtain the device’stermiosstructureusing
tcgetattr.Similarly,after calling either of the twocfsetfunctions, all we’ve done is
set the baud rate in atermiosstructure. For this change to affect the device, we have
to calltcsetattr.Ifthere is an error in either of the baud rates that we set, we may
not find out about the error until we calltcsetattr.