The Linux Programming Interface

(nextflipdebug5) #1

1316 Chapter 62


In the last line of the preceding shell session, we see that the shell printed its
prompt on the same line as the q character that caused the program to terminate.
The following shows an example run using cbreak mode:

$ ./test_tty_functions x
XYZ Type XYZ and Control-Z
[1]+ Stopped ./test_tty_functions x
$ stty Verify that terminal mode was restored
speed 38400 baud; line = 0;
$ fg Resume in foreground
./test_tty_functions x
*** Type 123 and Control-J
$ Type Control-C to terminate program
Press Enter to get next shell prompt
$ stty Verify that terminal mode was restored
speed 38400 baud; line = 0;

62.7 Terminal Line Speed (Bit Rate)


Different terminals (and serial lines) are capable of transmitting and receiving at differ-
ent speeds (bits per second). The cfgetispeed() and cfsetispeed() functions retrieve and
modify the input line speed. The cfgetospeed() and cfsetospeed() functions retrieve
and modify the output line speed.

The term baud is commonly used as a synonym for the terminal line speed (in
bits per second), although this usage is not technically correct. Precisely, baud
is the per-second rate at which signal changes can occur on the line, which is
not necessarily the same as the number of bits transmitted per second, since
the latter depends on how bits are encoded into signals. Nevertheless, the term
baud continues to be used synonymously with bit rate (bits per second). (The
term baud rate is often also used synonymously with baud, but this is redun-
dant; the baud is by definition a rate.) To avoid confusion, we’ll generally use
terms like line speed or bit rate.

Each of these functions works on a termios structure that must be previously initial-
ized by a call to tcgetattr().

#include <termios.h>

speed_t cfgetispeed(const struct termios *termios_p);
speed_t cfgetospeed(const struct termios *termios_p);
Both return a line speed from given termios structure
int cfsetospeed(struct termios *termios_p, speed_t speed);
int cfsetispeed(struct termios *termios_p, speed_t speed);
Both return 0 on success, or –1 on error
Free download pdf