tcsetattr()

Updated: April 19, 2023

Change the terminal control settings for a device

Synopsis:

#include <termios.h>

int tcsetattr( int filedes,
               int optional_actions,
               const struct termios *termios_p );

Arguments:

filedes
The file descriptor associated with the terminal device.
termios_p
A pointer to a termios structure that describes the attributes that you want to set for the terminal device.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The tcsetattr() function sets the current terminal control settings for the opened device indicated by filedes to the values stored in the structure pointed to by termios_p.

The operation of tcsetattr() depends on the values in optional_actions:

TCSANOW
The change is made immediately.
TCSADRAIN
No change is made until all currently written data has been transmitted.
TCSAFLUSH
No change is made until all currently written data has been transmitted, at which point any received but unread data is also discarded.

The termios control structure is defined in <termios.h>. For more information, see tcgetattr().

Returns:

0
Success.
-1
An error occurred; errno is set.

Errors:

EBADF
The argument filedes is invalid.
EINVAL
The argument action is invalid, or one of the members of termios_p is invalid.
ENOSYS
The resource manager associated with filedes doesn't support this call.
ENOTTY
The argument filedes doesn't refer to a terminal device.

Examples:

#include <stdlib.h>
#include <termios.h>

int raw( int fd )
  {
    struct termios termios_p;

    if( tcgetattr( fd, &termios_p ) )
      return( -1 );

    termios_p.c_cc[VMIN]  =  1;
    termios_p.c_cc[VTIME] =  0;
    termios_p.c_lflag &= ~( ECHO|ICANON|ISIG|
              ECHOE|ECHOK|ECHONL );
    termios_p.c_oflag &= ~( OPOST );
    return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
  }

int unraw( int fd )
  {
    struct termios termios_p;

    if( tcgetattr( fd, &termios_p ) )
      return( -1 );

    termios_p.c_lflag |= ( ECHO|ICANON|ISIG|
             ECHOE|ECHOK|ECHONL );
    termios_p.c_oflag |= ( OPOST );
    return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
  }

int main( void )
  {
    raw( 0 );
    /*
     * Stdin is now "raw"
     */
    unraw ( 0 );
    return EXIT_SUCCESS;
  }

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes