isatty(3)

๐Ÿ’… isatty(3) โ€” the "is this a terminal or not" check โœจ

okay bestie, gather round, because this little function is lowkey one of the most useful things in <unistd.h> and nobody talks about her. her name is isatty(), she lives in the Standard C library (libc, -lc), and her whole job is answering one very important question: is the thing I'm talking to an actual terminal, or is it just some file pretending? ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <unistd.h>

int isatty(int fd);

that's it. one arg, one int. no drama. ๐Ÿ’–


๐ŸŒธ DESCRIPTION ๐ŸŒธ

so the way that isatty() works is: you hand her an open file descriptor fd, and she tests whether fd is an open file descriptor that refers to a terminal. that's the whole thing. that's the assignment. she ate and left no crumbs. ๐Ÿ‘‘

like โ€” is this stdin attached to your actual keyboard-and-screen situation, or did someone pipe a file into you and now you're just โœจa fileโœจ? isatty() knows. she always knows. ๐Ÿ’…


๐ŸŽ€ RETURN VALUE ๐ŸŽ€

  • she returns 1 if fd is an open file descriptor referring to a terminal ๐Ÿฆ‹
  • otherwise she returns 0, and errno gets set to tell you what went wrong ๐Ÿ˜ญ

so no, she's not doing the -1 thing. she's a 0/1 girlie. binary. iconic. periodt.


๐Ÿ’€ ERRORS ๐Ÿ’€

  • EBADF โ€” fd is not a valid file descriptor. that's a you problem, bestie, she can't work with that ๐Ÿ™ˆ
  • ENOTTY โ€” fd refers to a file other than a terminal. (ngl, "not a tty" is literally the error name, she's not subtle โœจ)
โš ๏ธ history lesson, no cap: on some older kernels, some types of files gave you EINVAL in this case instead. that's a violation of POSIX, which specifies the error should be ENOTTY. so if you're on ancient hardware and something's giving EINVAL where you expected ENOTTYโ€ฆ yeah. that's why. not your fault. ๐Ÿ’€

๐ŸŒˆ ATTRIBUTES ๐ŸŒˆ

for the full explanation of what these terms even mean, go read attributes(7) โ€” she explains it better than I can fr fr.

  • isatty() โ€” Thread safety: MT-Safe ๐Ÿ”ฅ

so yes, she's thread-safe. multiple threads can vibe-check their file descriptors at the same time and nobody's getting hurt. we love a safe queen. ๐Ÿ’–


โญ STANDARDS โญ

POSIX.1-2008.

that's the standard she answers to. certified. official. ๐Ÿ’…


๐Ÿงš HISTORY ๐Ÿงš

  • POSIX.1-2001
  • SVr4
  • 4.3BSD

she's been around. she's seen things. she's a legacy icon and she's still on every system you'll ever touch ๐Ÿ‘‘


๐Ÿ“ SEE ALSO ๐Ÿ“

  • fstat(2) โ€” the heavier, more detailed way to inspect a file descriptor, if you want the whole lore
  • ttyname(3) โ€” if you already know it's a terminal and now you wanna know which terminal, she'll tell you the name

so yeah. isatty() โ€” small, fast, returns 1 or 0, sets errno when things go sideways, MT-Safe, POSIX.1-2008, with history going back to POSIX.1-2001, SVr4 and 4.3BSD. go check your file descriptors bestie, she's got you ๐Ÿซถ๐Ÿ’–

this page came from Linux man-pages 6.20, dated 2025-05-17, and yeah that's the isatty(3) page โ€” slay, bye โœจ

๐Ÿ“ฆ 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: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms