NAME
TCGETS, TCSETS, TCSETSW, TCSETSF, TCGETS2, TCSETS2, TCSETSW2, TCSETSF2, TCGETA, TCSETA, TCSETAW, TCSETAF - get and set terminal attributes
LIBRARY
Standard C library (libc, -lc)
SYNOPSIS
#include <asm/termbits.h>\n /* Definition of \nTC*\n constants */
\n#include <sys/ioctl.h>int ioctl(int \nfd\n, TCGETS, struct termios *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETS, const struct termios *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETSW, const struct termios *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETSF, const struct termios *\nargp\n);int ioctl(int \nfd\n, TCGETS2, struct termios2 *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETS2, const struct termios2 *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETSW2, const struct termios2 *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETSF2, const struct termios2 *\nargp\n);int ioctl(int \nfd\n, TCGETA, struct termio *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETA, const struct termio *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETAW, const struct termio *\nargp\n);\n
\nint ioctl(int \nfd\n, TCSETAF, const struct termio *\nargp\n);#include <asm/termbits.h>struct termios;\n
\nstruct termios2;\n
\nstruct termio;DESCRIPTION
- TCGETS
Equivalent to tcgetattr(fd, argp).
Get the current serial port settings.
- TCSETS
Equivalent to tcsetattr(fd, TCSANOW, argp).
Set the current serial port settings.
- TCSETSW
Equivalent to tcsetattr(fd, TCSADRAIN, argp).
Allow the output buffer to drain, and set the current serial port settings.
- TCSETSF
Equivalent to tcsetattr(fd, TCSAFLUSH, argp).
Allow the output buffer to drain, discard pending input, and set the current serial port settings.
The following four ioctls are just like TCGETS, TCSETS, TCSETSW, TCSETSF, except that they take a struct termios2 * instead of a struct termios *. If the structure member c_cflag contains the flag BOTHER, then the baud rate is stored in the structure members c_ispeed and c_ospeed as integer values. These ioctls are not supported on all architectures.
- TCGETS2
- TCSETS2
- TCSETSW2
- TCSETSF2
The following four ioctls are just like TCGETS, TCSETS, TCSETSW, TCSETSF, except that they take a struct termio * instead of a struct termios *.
- TCGETA
- TCSETA
- TCSETAW
- TCSETAF
RETURN VALUE
On success, 0 is returned. On error, -1 is returned and errno is set to indicate the error.
ERRORS
- EPERM
Insufficient permission.
HISTORY
- TCGETS2
- TCSETS2
- TCSETSW2
- TCSETSF2
Linux 2.6.20.
CAVEATS
struct termios from <asm/termbits.h> is different and incompatible with struct termios from <termios.h>. These ioctl calls require struct termios from <asm/termbits.h>.
EXAMPLES
Get or set arbitrary baudrate on the serial port.
/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <asm/termbits.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
#if !defined BOTHER
\n
fprintf(stderr, "BOTHER is unsupported\n");
\n
/* Program may fallback to TCGETS/TCSETS with Bnnn constants */
\n
exit(EXIT_FAILURE);
#else
\n
/* Declare tio structure, its type depends on supported ioctl */
# if defined TCGETS2
\n
struct termios2 tio;
# else
\n
struct termios tio;
# endif
\n
int fd, rc;
\n
if (argc != 2 && argc != 3 && argc != 4) {
\n
fprintf(stderr, "Usage: %s device [output [input] ]\n", argv[0]);
\n
exit(EXIT_FAILURE);
\n
}
\n
fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
\n
if (fd < 0) {
\n
perror("open");
\n
exit(EXIT_FAILURE);
\n
}
\n
/* Get the current serial port settings via supported ioctl */
# if defined TCGETS2
\n
rc = ioctl(fd, TCGETS2, &tio);
# else
\n
rc = ioctl(fd, TCGETS, &tio);
# endif
\n
if (rc) {
\n
perror("TCGETS");
\n
close(fd);
\n
exit(EXIT_FAILURE);
\n
}
\n
/* Change baud rate when more arguments were provided */
\n
if (argc == 3 || argc == 4) {
\n
/* Clear the current output baud rate and fill a new value */
\n
tio.c_cflag &= ~CBAUD;
\n
tio.c_cflag |= BOTHER;
\n
tio.c_ospeed = atoi(argv[2]);
\n
/* Clear the current input baud rate and fill a new value */
\n
tio.c_cflag &= ~(CBAUD << IBSHIFT);
\n
tio.c_cflag |= BOTHER << IBSHIFT;
\n
/* When 4th argument is not provided reuse output baud rate */
\n
tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]);
\n
/* Set new serial port settings via supported ioctl */
# if defined TCSETS2
\n
rc = ioctl(fd, TCSETS2, &tio);
# else
\n
rc = ioctl(fd, TCSETS, &tio);
# endif
\n
if (rc) {
\n
perror("TCSETS");
\n
close(fd);
\n
exit(EXIT_FAILURE);
\n
}
\n
/* And get new values which were really configured */
# if defined TCGETS2
\n
rc = ioctl(fd, TCGETS2, &tio);
# else
\n
rc = ioctl(fd, TCGETS, &tio);
# endif
\n
if (rc) {
\n
perror("TCGETS");
\n
close(fd);
\n
exit(EXIT_FAILURE);
\n
}
\n
}
\n
close(fd);
\n
printf("output baud rate: %u\n", tio.c_ospeed);
\n
printf("input baud rate: %u\n", tio.c_ispeed);
\n
exit(EXIT_SUCCESS);
#endif
}SEE ALSO
ioctl(2), ioctl_tty(2), termios(3)