carta
fwopen(3)

fwopen3

Open a stream

Part of fropen(3).

Usage

FILE *

FILE *

FILE *

Library

Standard C Library (libc, -lc)

Overview

The funopen() function associates a stream with up to four “I/O functions”. 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.

In general, omitting a function means that any attempt to perform the associated operation on the resulting stream will fail. If the close function is omitted, closing the stream will flush any buffered output and then succeed.

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.

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.

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.

An error on closefn() does not keep the stream open.

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.

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.

Errors

[EINVAL]

The funopen() function was called without either a read or write function. The funopen() function may also fail and set errno for any of the errors specified for the routine malloc(3).

History

  • FreeBSD 16.0-CURRENT (2026-10-27) to FreeBSD 15.0-RELEASE: the text shown here
  • FreeBSD 14.5-RELEASE to FreeBSD 13.3-RELEASE
  • FreeBSD 13.2-RELEASE to FreeBSD 13.0-RELEASE
  • FreeBSD 12.4-RELEASE
  • FreeBSD 12.3-RELEASE
  • FreeBSD 12.2-RELEASE
  • FreeBSD 12.1-RELEASE
  • FreeBSD 12.0-RELEASE
  • FreeBSD 11.4-RELEASE
  • FreeBSD 11.3-RELEASE
  • FreeBSD 11.2-RELEASE
  • FreeBSD 11.1-RELEASE
  • FreeBSD 11.0-RELEASE
  • FreeBSD 10.4-RELEASE
  • FreeBSD 10.3-RELEASE
  • FreeBSD 10.2-RELEASE
  • FreeBSD 10.1-RELEASE
  • FreeBSD 10.0-RELEASE
  • FreeBSD 9.3-RELEASE
  • FreeBSD 9.2-RELEASE
  • FreeBSD 9.1-RELEASE
  • FreeBSD 9.0-RELEASE
  • FreeBSD 8.4-RELEASE
  • FreeBSD 8.3-RELEASE
  • FreeBSD 8.2-RELEASE
  • FreeBSD 8.1-RELEASE
  • FreeBSD 8.0-RELEASE
  • FreeBSD 7.4-RELEASE
  • FreeBSD 7.3-RELEASE
  • FreeBSD 7.2-RELEASE
  • FreeBSD 7.1-RELEASE
  • FreeBSD 7.0-RELEASE
  • FreeBSD 6.4-RELEASE
  • FreeBSD 6.3-RELEASE to FreeBSD 5.3-RELEASE
  • FreeBSD 5.2.1-RELEASE to FreeBSD 5.2-RELEASE
  • FreeBSD 5.1-RELEASE to FreeBSD 5.0-RELEASE
  • FreeBSD 4.11-RELEASE to FreeBSD 4.5-RELEASE
  • FreeBSD 4.4-RELEASE
  • FreeBSD 4.3-RELEASE to FreeBSD 4.1-RELEASE
  • FreeBSD 4.0-RELEASE
  • FreeBSD 3.5.1-RELEASE to FreeBSD 3.5-RELEASE
  • FreeBSD 3.4-RELEASE to FreeBSD 2.2.6-RELEASE
  • FreeBSD 2.2.5-RELEASE to FreeBSD 2.2.2-RELEASE
  • FreeBSD 2.1.7.1-RELEASE to FreeBSD 2.1.5-RELEASE
  • FreeBSD 2.0.5-RELEASE

History

The funopen() functions first appeared in 4.4BSD.

Bugs

The funopen() function may not be portable to systems other than BSD.

The funopen() interface erroneously assumes that fpos_t is an integral type; see fseek(3) for a discussion of this issue.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.