NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |HISTORY |CAVEATS |EXAMPLES |SEE ALSO |COLOPHON | |
TCSETS(2const)TCSETS(2const)TCGETS, TCSETS, TCSETSW, TCSETSF, TCGETS2, TCSETS2, TCSETSW2, TCSETSF2, TCGETA, TCSETA, TCSETAW, TCSETAF - get and set terminal attributes
Standard C library (libc,-lc)
#include <asm/termbits.h>/* Definition ofTC*constants */#include <sys/ioctl.h>int ioctl(intfd, TCGETS, struct termios *argp);int ioctl(intfd, TCSETS, const struct termios *argp);int ioctl(intfd, TCSETSW, const struct termios *argp);int ioctl(intfd, TCSETSF, const struct termios *argp);int ioctl(intfd, TCGETS2, struct termios2 *argp);int ioctl(intfd, TCSETS2, const struct termios2 *argp);int ioctl(intfd, TCSETSW2, const struct termios2 *argp);int ioctl(intfd, TCSETSF2, const struct termios2 *argp);int ioctl(intfd, TCGETA, struct termio *argp);int ioctl(intfd, TCSETA, const struct termio *argp);int ioctl(intfd, TCSETAW, const struct termio *argp);int ioctl(intfd, TCSETAF, const struct termio *argp);#include <asm/termbits.h>struct termios;struct termios2;struct termio;
TCGETSEquivalent totcgetattr(fd, argp). Get the current serial port settings.TCSETSEquivalent totcsetattr(fd, TCSANOW, argp). Set the current serial port settings.TCSETSW Equivalent totcsetattr(fd, TCSADRAIN, argp). Allow the output buffer to drain, and set the current serial port settings.TCSETSF Equivalent totcsetattr(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 likeTCGETS,TCSETS,TCSETSW,TCSETSF, except that they take astruct termios2 * instead of astruct termios *. If the structure memberc_cflagcontains the flagBOTHER, then the baud rate is stored in the structure membersc_ispeedandc_ospeedas integer values. These ioctls are not supported on all architectures.TCGETS2TCSETS2TCSETSW2TCSETSF2 The following four ioctls are just likeTCGETS,TCSETS,TCSETSW,TCSETSF, except that they take astruct termio * instead of astruct termios *.TCGETATCSETATCSETAWTCSETAF
On success, 0 is returned. On error, -1 is returned anderrno is set to indicate the error.
EPERMInsufficient permission.
TCGETS2TCSETS2TCSETSW2TCSETSF2 Linux 2.6.20.
struct termiosfrom<asm/termbits.h> is different and incompatible withstruct termiosfrom<termios.h>. These ioctl calls requirestruct termiosfrom<asm/termbits.h>.
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 fprintf(stderr, "BOTHER is unsupported\n"); /* Program may fallback to TCGETS/TCSETS with Bnnn constants */ exit(EXIT_FAILURE); #else /* Declare tio structure, its type depends on supported ioctl */ # if defined TCGETS2 struct termios2 tio; # else struct termios tio; # endif int fd, rc; if (argc != 2 && argc != 3 && argc != 4) { fprintf(stderr, "Usage: %s device [output [input] ]\n", argv[0]); exit(EXIT_FAILURE); } fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY); if (fd < 0) { perror("open"); exit(EXIT_FAILURE); } /* Get the current serial port settings via supported ioctl */ # if defined TCGETS2 rc = ioctl(fd, TCGETS2, &tio); # else rc = ioctl(fd, TCGETS, &tio); # endif if (rc) { perror("TCGETS"); close(fd); exit(EXIT_FAILURE); } /* Change baud rate when more arguments were provided */ if (argc == 3 || argc == 4) { /* Clear the current output baud rate and fill a new value */ tio.c_cflag &= ~CBAUD; tio.c_cflag |= BOTHER; tio.c_ospeed = atoi(argv[2]); /* Clear the current input baud rate and fill a new value */ tio.c_cflag &= ~(CBAUD << IBSHIFT); tio.c_cflag |= BOTHER << IBSHIFT; /* When 4th argument is not provided reuse output baud rate */ tio.c_ispeed = (argc == 4) ? atoi(argv[3]) : atoi(argv[2]); /* Set new serial port settings via supported ioctl */ # if defined TCSETS2 rc = ioctl(fd, TCSETS2, &tio); # else rc = ioctl(fd, TCSETS, &tio); # endif if (rc) { perror("TCSETS"); close(fd); exit(EXIT_FAILURE); } /* And get new values which were really configured */ # if defined TCGETS2 rc = ioctl(fd, TCGETS2, &tio); # else rc = ioctl(fd, TCGETS, &tio); # endif if (rc) { perror("TCGETS"); close(fd); exit(EXIT_FAILURE); } } close(fd); printf("output baud rate: %u\n", tio.c_ospeed); printf("input baud rate: %u\n", tio.c_ispeed); exit(EXIT_SUCCESS); #endif }ioctl(2),ioctl_tty(2),termios(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17TCSETS(2const)Pages that refer to this page:ioctl_tty(2), TIOCSLCKTRMIOS(2const)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |