fileno(3)
β¨ fileno(3) β¨
okay bestie, welcome to the Library Functions Manual entry for fileno β yes it's a 3 page, yes it's a C thing, yes you're gonna get it, let's go π«Ά
π NAME π
fileno β obtain file descriptor of a stdio stream
like literally that's it, that's the whole job. give it a stream, it hands you the fd. iconic behavior honestly π
π LIBRARY π
Standard C library (libc, -lc)
π SYNOPSIS π
#include <stdio.h>
int fileno(FILE *stream);
π¦ Feature Test Macro Requirements for glibc π¦
(peep feature_test_macros(7) if you need the full lore, bestie)
fileno():_POSIX_C_SOURCE
not me needing a feature test macro just to peek at a file descriptor π glibc really said "define it or you don't get her"
πΈ DESCRIPTION πΈ
So what does fileno() actually do? She examines the argument stream and returns the integer file descriptor used to implement this stream. Simple. Elegant. Main character energy fr fr β¨
β οΈ BUT β and this is the part where you need to lock in β that file descriptor is still owned by stream. It's not yours, it's hers, and it will be closed when fclose(3) is called. So if you hand that fd to some other code that might close it, you have to duplicate the file descriptor with dup(2) first. No cap. Do not skip this step. The way that closing someone else's fd will absolutely ruin your whole afternoon π
For the nonlocking counterpart, see unlocked_stdio(3). iykyk π
π₯ RETURN VALUE π₯
- On success,
fileno()returns the file descriptor associated with stream. slay π - On failure,
-1is returned anderrnois set to indicate the error. tragedy but make it informative π
π ERRORS π
EBADFβ stream is not associated with a file. basically the stream said "i have no file bestie" and walked away πΆβοΈ
π ATTRIBUTES π
For an explanation of the terms used in this section, see attributes(7).
- Interface:
fileno()- Attribute: Thread safety
- Value:
MT-Safe
the table said MT-Safe so you can flex that in threads, periodt. (the original had a lil box graphic, we're doing bullets instead because tables said "not today" π)
π STANDARDS π
POSIX.1-2008.
π§ HISTORY π§
POSIX.1-2001. she's been around, she's established, she's a vet π
π SEE ALSO π
go read those too, bestie, they're all in the group chat π
okay that's the whole page π go forth and never close an fd you don't own. mwah π
this masterpiece brought to you by Linux man-pages 6.20, dated 2026-02-08, filed under fileno(3) β ty for coming to my ted talk, ily, 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: BSD-4-Clause-UC
- Copyright 1990-1991, The Regents of the University of California
- Copyright 2021, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms