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
1iffdis an open file descriptor referring to a terminal ๐ฆ - otherwise she returns
0, anderrnogets 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โfdis not a valid file descriptor. that's a you problem, bestie, she can't work with that ๐ENOTTYโfdrefers 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 youEINVALin this case instead. that's a violation of POSIX, which specifies the error should beENOTTY. so if you're on ancient hardware and something's givingEINVALwhere you expectedENOTTYโฆ 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 lorettyname(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