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 -1 shall be returned and errno set 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> πŸ“š


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 πŸ¦‹πŸ’–βœ¨