FILENO(3POSIX)
π FILENO(3POSIX) π
okay bestie, so this one comes straight out of the POSIX Programmer's Manual (yes, the fancy IEEE/The Open Group one, 2017 era, we'll get to that at the end π). it's the fileno page and it's honestly short and sweet. let's get into it π
β¨ PROLOG β¨
real talk first, this is a lil disclaimer and you need to hear it:
- This manual page is part of the POSIX Programmer's Manual.
- The Linux implementation of this interface may differ β so go peek at the corresponding Linux manual page for the actual Linux tea β
- ...or the interface may not be implemented on Linux at all π
so like, vibe check before you ship it, yk?
πΈ NAME πΈ
fileno β map a stream pointer to a file descriptor
literally what it says on the tin, babe. stream pointer in, file descriptor out. slay π
π¦ SYNOPSIS π¦
#include <stdio.h>
int fileno(FILE *stream);
that's the whole thing. one header, one function, one FILE * argument. iconic minimalism β¨
π DESCRIPTION π
The fileno() function shall return the integer file descriptor associated with the stream pointed to by stream.
that's it. that's the function. you hand it a stream, it hands you back the int fd chillin' underneath it π«Ά
β RETURN VALUE β
- Upon successful completion,
fileno()shall return the integer value of the file descriptor associated with stream. - Otherwise, the value
-1shall be returned anderrnoset to indicate the error π
so like, check for -1 or you're gonna have a bad time, no cap.
π₯ ERRORS π₯
the "shall fail" section (aka it's over, no recovering)
EBADFβ The stream is not associated with a file. π
the "may fail" section (aka maybe, depends, mysterious energy)
EBADFβ The file descriptor underlying stream is not a valid file descriptor.
bestie notice how EBADF shows up in both lists, once as a "shall fail" and once as a "may fail." that's not me being messy, that's the spec being messy. copy it faithfully π
and then: The following sections are informative. β which is spec-speak for "the rest of this is lore, not law" π
π EXAMPLES π
None.
(not me giving you nothing. the standard said so. go off, standard π)
π§ APPLICATION USAGE π§
None.
(again. empty. the void. we respect it π)
π RATIONALE π
okay so why does this function even exist, right? here's the reasoning, and it's actually kind of a serve:
Without some specification of which file descriptors are associated with these streams, it is impossible for an application to set up the streams for another application it starts with fork() and exec().
and like, in particular? it would not be possible to write a portable version of the sh command interpreter β although, fair warning, there may be other constraints that would prevent that portability anyway. so like, don't come for me, the spec said it not me π
πΈ FUTURE DIRECTIONS πΈ
None.
(no glow-up planned. she's perfect as she is. periodt β¨)
π SEE ALSO π
go read these next, bestie, they're all connected:
- Section 2.5.1, Interaction of File Descriptors and Standard I/O Streams
dirfd()fdopen()fopen()stdin
and also, from the Base Definitions volume of POSIX.1β2017: <stdio.h> π
π COPYRIGHT π
okay legal moment, but make it cute:
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html.
fr fr, the original standard is the referee. if this page and the standard disagree, the standard wins. no arguing with the ref π«Ά
and that's the whole page babes π
this one was written by IEEE/The Open Group back in 2017, and it's filed under FILENO(3POSIX) β go forth and map your streams to your file descriptors, you gorgeous little sysadmin π¦πβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms