---
name: "fopencookie"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/fopencookie"
---

# fopencookie(3)

open a stream

## NAME

**fopencookie** – open a stream

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#include \<stdio.h\>**

*typedef* *ssize\_t*\

**(\*cookie\_read\_function\_t)**(*void \*cookie*, *char \*buf*, *size\_t size*);

*typedef* *ssize\_t*\

**(\*cookie\_write\_function\_t)**(*void \*cookie*, *const char \*buf*, *size\_t size*);

*typedef* *int*\

**(\*cookie\_seek\_function\_t)**(*void \*cookie*, *off64\_t \*offset*, *int whence*);

*typedef* *int*\

**(\*cookie\_close\_function\_t)**(*void \*cookie*);

```
typedef struct {
	cookie_read_function_t	*read;
	cookie_write_function_t	*write;
	cookie_seek_function_t	*seek;
	cookie_close_function_t	*close;
} cookie_io_functions_t;
```

*FILE* *\**\

**fopencookie**(*void \*cookie*, *const char \*mode*, *cookie\_io\_functions\_t io\_funcs*);

## DESCRIPTION

The **fopencookie** function associates a stream with up to four “I/O functions”. 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 write function is omitted, data written to the stream is discarded. If the close function is omitted, closing the stream will flush any buffered output and then succeed.

The calling conventions of *read*, *write*, and *close* must match those, respectively, of [read(2)](/read/2.md), [write(2)](/write/2.md), and [close(2)](/close/2.md) with the single exception that they are passed the *cookie* argument specified to **fopencookie** in place of the traditional file descriptor argument. The *seek* function updates the current stream offset using *\*offset* and *whence*. If *\*offset* is non-NULL, it updates *\*offset* with the current stream offset.

**fopencookie** is implemented as a thin shim around the [funopen(3)](/funopen/3.md) interface. Limitations, possibilities, and requirements of that interface apply to **fopencookie**.

## RETURN VALUES

Upon successful completion, **fopencookie** returns a FILE pointer. Otherwise, NULL is returned and the global variable *errno* is set to indicate the error.

## ERRORS

- \[EINVAL\]

  A bogus *mode* was provided to **fopencookie**.

- \[ENOMEM\]

  The **fopencookie** function may fail and set *errno* for any of the errors specified for the [malloc(3)](/malloc/3.md) routine.

## SEE ALSO

[fcntl(2)](/fcntl/2.md), [open(2)](/open/2.md), [fclose(3)](/fclose/3.md), [fopen(3)](/fopen/3.md), [fseek(3)](/fseek/3.md), [funopen(3)](/funopen/3.md)

## HISTORY

The **funopen**() functions first appeared in 4.4BSD. The **fopencookie** function first appeared in FreeBSD 11.

## BUGS

The **fopencookie** function is a nonstandard glibc extension and may not be portable to systems other than FreeBSD and Linux.
