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, -1 is returned and errno is 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms