tcgetattr() — Get the attributes for a terminal

Standards

Standards / Extensions C or C++ Dependencies

POSIX.1
XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _POSIX_SOURCE
#include <termios.h>

int tcgetattr(int fildes, struct termios *termptr);

General description

Gets a termios structure, which contains control information for a terminal associated with fildes. It stores that information in a memory location that termptr points to. The contents of a termios structure are described in tcsetattr() — Set the attributes for a terminal.

tcgetattr() can run in either a foreground or background process; however, if the process is in the background, a foreground process may subsequently change the attributes.

tcgetattr() only works in an environment where either a controlling terminal exists, or stdin and stderr refer to tty devices. Specifically, it does not work in a TSO environment.

Note: The tcgetattr() function has a dependency on the level of the Enhanced ASCII Extensions. See Enhanced ASCII support for details.

Returned value

If successful, tcgetattr() returns 0.

If unsuccessful, tcgetattr() returns -1 and sets errno to one of the following values:
Error Code
Description
EBADF
fildes is not a valid open file descriptor.
ENOTTY
The file associated with fildes is not a terminal.

Example

CELEBT06
⁄* CELEBT06

   This example provides information about the attributes.

 *⁄
#define _POSIX_SOURCE
#include <termios.h>
#include <stdio.h>
#include <unistd.h>

main() {
  struct termios term;

  if (tcgetattr(STDIN_FILENO, &term) != 0)
    perror("tcgetatt() error");
  else {
    if (term.c_iflag & BRKINT)
      puts("BRKINT is set");
    else
      puts("BRKINT is not set");
    if (term.c_cflag & PARODD)
      puts("Odd parity is used");
    else
      puts("Even parity is used");
    if (term.c_lflag & ECHO)
      puts("ECHO is set");
    else
      puts("ECHO is not set");
    printf("The end-of-file character is x'%02x'\n",
term.c_cc[VEOF]);
  }
}
Output
ECHO is set
The End Of File character is x'37'

Related information