FUNOPEN(3)
β¨ NAME β¨
Okay bestie, welcome to funopen(3) π
β the page for funopen, fropen, and fwopen, aka the little crew that opens a stream. Yes, βopen a streamβ is the whole vibe. π«Ά
π LIBRARY π
Standard C Library (libc, -lc) β iconic, classic, no cap. π
πΈ SYNOPSIS πΈ
#include <stdio.h>
FILE *
funopen(const void *cookie, int (*readfn)(void *, char *, int),
int (*writefn)(void *, const char *, int),
fpos_t (*seekfn)(void *, fpos_t, int), int (*closefn)(void *));
FILE *
fropen(void *cookie, int (*readfn)(void *, char *, int));
FILE *
fwopen(void *cookie, int (*writefn)(void *, const char *, int));
π¦ DESCRIPTION π¦
The way that funopen() works is it associates a stream with up to four βI/O functionsβ β like, a whole little custom squad. β¨ Either readfn or writefn must be specified; the others can be given as an appropriately-typed NULL pointer. These I/O functions will be used to read, write, seek and close the new stream. Obsessed with the flexibility, honestly.
In general, omitting a function means that any attempt to perform the associated operation on the resulting stream will fail. Big yikes if you expected it to work, but okay. If the close function is omitted, closing the stream will flush any buffered output and then succeed. Slay. π
The calling conventions of readfn, writefn, seekfn and closefn must match those, respectively, of read(2), write(2), lseek(2), and close(2) β with the single exception that they are passed the cookie argument specified to funopen() in place of the traditional file descriptor argument. Iykyk. π
Read and write I/O functions are allowed to change the underlying buffer on fully buffered or line buffered streams by calling setvbuf(3). They are also not required to completely fill or empty the buffer. They are not, however, allowed to change streams from unbuffered to buffered or to change the state of the line buffering flag. They must also be prepared to have read or write calls occur on buffers other than the one most recently specified. Thatβs the rule, bestie β respect the buffering vibe check, periodt. π
All user I/O functions can report an error by returning -1. Additionally, all of the functions should set the external variable errno appropriately if an error occurs. Fr fr, set your errno. π
An error on ζ₯ιͺ€fn() does not keep the stream open. Important. π
As a convenience, the include file <stdio.h> defines the macros fropen() and fwopen() as calls to funopen() with only a read or write function specified. Cute shortcut energy. π
π RETURN VALUES π
Upon successful completion, funopen() returns a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error. Thatβs it, thatβs the return vibe check. β¨
π₯ ERRORS π₯
[EINVAL]β Thefunopen()function was called without either a read or write function. Thefunopen()function may also fail and seterrnofor any of the errors specified for the routinemalloc(3).
π SEE ALSO π
fcntl(2), open(2), fclose(3), fopen(3), fopencookie(3), fseek(3), setbuf(3) β the whole friend group. Go read them if youβre curious, bestie. π«Ά
β HISTORY β
The funopen() functions first appeared in 4.4BSD. Vintage but still main character energy. π
π BUGS π
- The
funopen()function may not be portable to systems other than BSD. Not every system is gonna get the vibe, ngl. π¬ - The
funopen()interface erroneously assumes thatfpos_tis an integral type; seefseek(3)for a discussion of this issue. Thatβs a real βhmmβ moment, but itβs in the docs, so weβre saying it. π
xoxo, Debian β May 9, 2016 β FUNOPEN(3) π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms