TERMIOS(3V)

NAME
termios, tcgetattr, tcsetattr, tcsendbreak, tcdrain, tcflush, tcflow, cfgetospeed, cfgetispeed, cfsetispeed, cfsetospeed - get and set terminal attributes, line control, get and set baud rate, get and set terminal foreground process group ID

SYNOPSIS
#include <termios.h>
#include <unistd.h>

int tcgetattr(fd, termios_p)
int fd;
struct termios *termios_p;

int tcsetattr(fd, optional_actions, termios_p)
int fd;
int optional_actions;
struct termios *termios_p;

int tcsendbreak(fd, duration)
int fd;
int duration;

int tcdrain(fd)
int fd;

int tcflush(fd, queue_selector)
int fd;
int queue_selector;

int tcflow(fd, action)
int fd;
int action;

speed_t cfgetospeed(termios_p)
struct termios *termios_p;

int cfsetospeed(termios_p, speed)
struct termios *termios_p;
speed_t speed;

speed_t cfgetispeed(termios_p)
struct termios *termios_p;

int cfsetispeed(termios_p, speed)
struct termios *termios_p;
speed_t speed;

#include <sys/types.h>
#include <termios.h>

DESCRIPTION

The termios functions describe a general terminal interface that is provided to control asynchronous communications ports. A more detailed overview of the terminal interface can be found in termio(4). That section also describes an ioctl() interface that can be used to access the same functionality. However, the function interface described here is the preferred user interface.

Many of the functions described here have a termios_p argument that is a pointer to a termios structure. This structure contains the following members:

		tcflag_t	c_iflag;		/* input modes */
		tcflag_t	c_oflag;		/* output modes */
		tcflag_t	c_cflag;		/* control modes */
 		tcflag_t	c_lflag;		/* local modes */
		cc_t	c_cc[NCCS];	/* control chars */

These structure members are described in detail in termio(4).

tcgetattr() gets the parameters associated with the object referred by fd and stores them in the termios structure referenced by termios_p. This function may be invoked from a background process; however, the terminal attributes may be subsequently changed by a foreground process.

tcsetattr() sets the parameters associated with the terminal (unless support is required from the underlying hardware that is not available) from the termios structure referred to by termios_p as follows:

    •   If optional_actions is TCSANOW, the change occurs immediately.

    •   If optional_actions is TCSADRAIN, the change occurs after all output written to fd has been transmitted. This function should be used when changing parameters that affect output.

    •   If optional_actions is TCSAFLUSH, the change occurs after all output written to the object referred by fd has been transmitted, and all input that has been received but not read will be discarded before the change is made.

The symbolic constants for the values of optional_actions are defined in <sys/termios.h>.

If the terminal is using asynchronous serial data transmission, tcsendbreak() transmits a continuous stream of zero-valued bits for a specific duration. If duration is zero, it transmits zero-valued bits for at least 0.25 seconds, and not more that 0.5 seconds. If duration is not zero, it sends zero-valued bits for duration*N seconds, where N is at least 0.25, and not more than 0.5.

If the terminal is not using asynchronous serial data transmission, tcsendbreak() returns without taking any action.

tcdrain() waits until all output written to the object referred to by fd has been transmitted.

tcflush() discards data written to the object referred to by fd but not transmitted, or data received but not read, depending on the value of queue_selector:

    •   If queue_selector is TCIFLUSH, it flushes data received but not read.

    •   If queue_selector is TCOFLUSH, it flushes data written but not transmitted.

    •   If queue_selector is TCIOFLUSH, it flushes both data received but not read, and data written but not transmitted.

The symbolic constants for the values of queue_selector and action are defined in termios.h.

The default on open of a terminal file is that neither its input nor its output is suspended.

tcflow() suspends transmission or reception of data on the object referred to by fd, depending on the value of actions:

    •   If action is TCOOFF, it suspends output.

    •   If action is TCOON, it restarts suspended output.

    •   If action is TCIOFF, the system transmits a STOP character, which stops the terminal device from transmitting data to the system. (See termio(4).)

    •   If action is TCION, the system transmits a START character, which starts the terminal device transmitting data to the system. (See termio(4).)

The baud rate functions are provided for getting and setting the values of the input and output baud rates in the termios structure. The effects on the terminal device described below do not become effective until tcsetattr() is successfully called.

The input and output baud rates are stored in the termios structure. The values shown in the table are supported. The names in this table are defined in termios.h
Name | Description |  | Name | Description
B0 | Hang up |  | B600 | 600 baud
B50 | 50 baud |  | B1200 | 1200 baud
B75 | 75 baud |  | B1800 | 1800 baud
B110 | 110 baud |  | B2400 | 2400 baud
B134 | 134.5 baud |  | B4800 | 4800 baud
B150 | 150 baud |  | B9600 | 9600 baud
B200 | 200 baud |  | B19200 | 19200 baud
B300 | 300 baud |  | B38400 | 38400 baud

cfgetospeed() returns the output baud rate stored in the termios structure pointed to by termios_p.

cfsetospeed() sets the output baud rate stored in the termios structure pointed to by termios_p to speed. The zero baud rate, B0, is used to terminate the connection. If B0 is specified, the modem control lines shall no longer be asserted. Normally, this will disconnect the line.

If the input baud rate is set to zero, the input baud rate will be specified by the value of the output baud rate.

cfgetispeed() returns the input baud rate stored in the termios structure.

cfsetispeed() sets the input baud rate stored in the termios structure to speed.

RETURN VALUES

cfgetispeed() returns the input baud rate stored in the termios structure.

cfgetospeed() returns the output baud rate stored in the termios structure.

cfsetispeed() and cfsetospeed() return:

0   on success.

-1  on failure and sets errno to indicate the error.

All other functions return:

0   on success.

-1  on failure and set errno to indicate the error.

ERRORS

EBADF
    The fd argument is not a valid file descriptor.

ENOTTY
    The file associated with fd is not a terminal.

tcsetattr() may set errno to:

EINVAL
    The optional_actions argument is not a proper value.

    An attempt was made to change an attribute represented in the termios structure to an unsupported value.

tcsendbreak() may set errno to:

EINVAL
    The device does not support tcsendbreak().

tcdrain() may set errno to:

EINTR
    A signal interrupted tcdrain().

EINVAL
    The device does not support tcdrain().

tcflush() may set errno to:

EINVAL
    The device does not support tcflush().

    The queue_selector argument is not a proper value.

tcflow() may set errno to:

EINVAL
    The device does not support tcflow().

    The action argument is not a proper value.

tcsetattr() may set errno to:

EAGAIN
    There is insufficient memory available to copy in the arguments.

EBADF
    fd is not a valid descriptor.

EFAULT
    Some part of the structure pointed to by termios_p is outside the process's allocated address space.

EINVAL
    optional_actions is not valid.

EIO The calling process is a background process.

ENOTTY
    fd does not refer to a terminal device.

ENXIO
    The terminal referred to by fd is hung up.

cfsetispeed() and cfsetospeed() may set errno to:

EINVAL
    speed is greater than B38400 or less than 0.

SEE ALSO
setpgid(2V), setsid(2V), termio(4)