1funopen(3bsd) LOCAL funopen(3bsd)
2
4 funopen, fropen, fwopen — open a stream
5
7 Utility functions from BSD systems (libbsd, -lbsd)
8
10 #include <stdio.h>
11 (See libbsd(7) for include usage.)
12
13 FILE *
14 funopen(const void *cookie, int (*readfn)(void *, char *, int),
15 int (*writefn)(void *, const char *, int),
16 off_t (*seekfn)(void *, off_t, int), int (*closefn)(void *));
17
18 FILE *
19 fropen(void *cookie, int (*readfn)(void *, char *, int));
20
21 FILE *
22 fwopen(void *cookie, int (*writefn)(void *, const char *, int));
23
25 The funopen() function associates a stream with up to four “I/O
26 functions”. Either readfn or writefn must be specified; the others can
27 be given as an appropriately-typed NULL pointer. These I/O functions
28 will be used to read, write, seek and close the new stream.
29
30 In general, omitting a function means that any attempt to perform the as‐
31 sociated operation on the resulting stream will fail. If the close func‐
32 tion is omitted, closing the stream will flush any buffered output and
33 then succeed.
34
35 The calling conventions of readfn, writefn, seekfn and closefn must match
36 those, respectively, of read(2), write(2), lseek(2), and close(2) with
37 the single exception that they are passed the cookie argument specified
38 to funopen() in place of the traditional file descriptor argument.
39
40 Read and write I/O functions are allowed to change the underlying buffer
41 on fully buffered or line buffered streams by calling setvbuf(3). They
42 are also not required to completely fill or empty the buffer. They are
43 not, however, allowed to change streams from unbuffered to buffered or to
44 change the state of the line buffering flag. They must also be prepared
45 to have read or write calls occur on buffers other than the one most re‐
46 cently specified.
47
48 All user I/O functions can report an error by returning -1. Addition‐
49 ally, all of the functions should set the external variable errno appro‐
50 priately if an error occurs.
51
52 An error on closefn() does not keep the stream open.
53
54 As a convenience, the include file <stdio.h> defines the macros fropen()
55 and fwopen() as calls to funopen() with only a read or write function
56 specified.
57
59 Upon successful completion, funopen() returns a FILE pointer. Otherwise,
60 NULL is returned and the global variable errno is set to indicate the er‐
61 ror.
62
64 [EINVAL] The funopen() function was called without either a
65 read or write function. The funopen() function may
66 also fail and set errno for any of the errors speci‐
67 fied for the routine malloc(3).
68
70 fcntl(2), open(2), fclose(3), fopen(3), fseek(3), setbuf(3)
71
73 The funopen() functions first appeared in 4.4BSD.
74
76 The funopen() function may not be portable to systems other than BSD.
77
78 On FreeBSD, OpenBSD and DragonFly the funopen() interface erroneously as‐
79 sumes that fpos_t is an integral type, and uses it in the seekfn hook;
80 but because code using a seekfn hook will fail to build on systems where
81 fpos_t is a struct, and it will need to be slightly fixed anyway, the im‐
82 plementation provided by libbsd (in the same way as NetBSD) uses the cor‐
83 rect off_t types.
84
85BSD March 19, 2004 BSD