TCSETS(2const)

πŸ’– TCSETS β€” get and set terminal attributes πŸ’–

okay bestie, welcome to the TCSETS(2const) page, this is the one where we mess with terminal (serial port) attributes and honestly? it's giving main character energy. πŸ’…

Quick heads-up on the names: this whole family is TCGETS, TCSETS, TCSETSW, TCSETSF, TCGETS2, TCSETS2, TCSETSW2, TCSETSF2, TCGETA, TCSETA, TCSETAW, and TCSETAF β€” get and set terminal attributes, collectively, as a little squad. 🫢


πŸ“š LIBRARY

Standard C library (libc, -lc). yes girl, it's in libc, nothing exotic. ✨


✨ SYNOPSIS ✨

#include <asm/termbits.h>  /* Definition of TC* constants */
#include <sys/ioctl.h>

int ioctl(int fd, TCGETS, struct termios *argp);
int ioctl(int fd, TCSETS, const struct termios *argp);
int ioctl(int fd, TCSETSW, const struct termios *argp);
int ioctl(int fd, TCSETSF, const struct termios *argp);

int ioctl(int fd, TCGETS2, struct termios2 *argp);
int ioctl(int fd, TCSETS2, const struct termios2 *argp);
int ioctl(int fd, TCSETSW2, const struct termios2 *argp);
int ioctl(int fd, TCSETSF2, const struct termios2 *argp);

[[deprecated]] int ioctl(int fd, TCGETA, struct termio *argp);
[[deprecated]] int ioctl(int fd, TCSETA, const struct termio *argp);
[[deprecated]] int ioctl(int fd, TCSETAW, const struct termio *argp);
[[deprecated]] int ioctl(int fd, TCSETAF, const struct termio *argp);

#include <asm/termbits.h>

struct termios;
struct termios2;
struct [[deprecated]] termio;
🚨 see those [[deprecated]] tags? that's not decor, bestie. those ones are on their way out. we'll get to them.

🌸 DESCRIPTION 🌸

okay so here's the tea on each one, one by one, no skipping. πŸ’–

  • TCGETS β€” Equivalent to tcgetattr(fd, argp).

Get the current serial port settings. πŸ“– it's a read, it just looks at the vibe of your port.

  • TCSETS β€” Equivalent to tcsetattr(fd, TCSANOW, argp).

Set the current serial port settings. boom, applied immediately. ⚑

  • TCSETSW β€” Equivalent to tcsetattr(fd, TCSADRAIN, argp).

Allow the output buffer to drain, and set the current serial port settings. 🚰 let everything finish flushing out first, then change.

  • TCSETSF β€” Equivalent to tcsetattr(fd, TCSAFLUSH, argp).

Allow the output buffer to drain, discard pending input, and set the current serial port settings. πŸ’€ output drains, input gets thrown in the bin, and then the new settings land. ruthless but iconic.


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 *. 🫢 (yes, termio, no s. they're cousins, not twins.)

  • TCGETA
  • TCSETA
  • TCSETAW
  • TCSETAF

πŸŽ€ RETURN VALUE πŸŽ€

On success, 0 is returned. ✨ On error, -1 is returned and errno is set to indicate the error. it said what it said. πŸ’…


πŸ’€ ERRORS πŸ’€

  • EPERM β€” Insufficient permission. not yours, bestie, go get some permissions. πŸ™ˆ

πŸ¦‹ HISTORY πŸ¦‹

The girls TCGETS2, TCSETS2, TCSETSW2, TCSETSF2 all showed up in 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>.

no cap, if you grab the wrong termios you WILL be confused and it will NOT be the compiler's fault. check your #include, queen. πŸ‘‘

🌈 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
    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
}

ate, honestly. πŸ“ that whole #if defined ladder is giving "works everywhere, panics nowhere".


πŸ”₯ SEE ALSO πŸ”₯

go read those too, they're the supporting cast and they eat. πŸ‘‘


mwah πŸ’œ this page was brought to you by Linux man-pages 6.20, dated 2026-07-02, and honestly it slayed. go set some terminal attributes, bestie, you've got this. πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms