FUNOPEN(3)

NAME
funopen, fropen, fwopen - open a stream with custom callbacks

SYNOPSIS

#include <stdio.h>

FILE *funopen(const void *cookie, int (*readfn) (void *cookie, char *buf, int n), int (*writefn) (void *cookie, const char *buf, int n), fpos_t (*seekfn) (void *cookie, fpos_t off, int whence), int (*closefn) (void *cookie));

FILE *fropen(const void *cookie, int (*readfn) (void *cookie, char *buf, int n));

FILE *fwopen(const void *cookie, int (*writefn) (void *cookie, const char *buf, int n));

DESCRIPTION

funopen creates a FILE stream where I/O is performed using custom callbacks. At least one of readfn and writefn must be provided, which determines whether the stream behaves with mode <"r">, <"w">, or <"r+">.

readfn should return -1 on failure, or else the number of bytes read (0 on EOF). It is similar to read, except that <int> rather than <size_t> bounds a transaction size, and cookie will be passed as the first argument. A NULL readfn makes attempts to read the stream fail.

writefn should return -1 on failure, or else the number of bytes written. It is similar to write, except that <int> rather than <size_t> bounds a transaction size, and cookie will be passed as the first argument. A NULL writefn makes attempts to write the stream fail.

seekfn should return (fpos_t)-1 on failure, or else the current file position. It is similar to lseek, except that cookie will be passed as the first argument. A NULL seekfn makes the stream behave similarly to a pipe in relation to stdio functions that require positioning. This implementation assumes fpos_t and off_t are the same type.

closefn should return -1 on failure, or 0 on success. It is similar to close, except that cookie will be passed as the first argument. A NULL closefn merely flushes all data then lets fclose succeed. A failed close will still invalidate the stream.

Read and write I/O functions are allowed to change the underlying buffer on fully buffered or line buffered streams by calling setvbuf. 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.

The functions fropen and fwopen are convenience macros around funopen that only use the specified callback.

RETURNS

The return value is an open FILE pointer on success. On error, NULL is returned, and errno will be set to EINVAL if a function pointer is missing, ENOMEM if the stream cannot be created, or EMFILE if too many streams are already open.

PORTABILITY

This function is a newlib extension, copying the prototype from BSD. It is not portable. See also the fopencookie interface from Linux.

Supporting OS subroutines required: sbrk.

AUTHORS

Steve Chamberlain
    Author.

Roland Pesch
    Author.

Red Hat Support

Jeff Johnston
    Author.

COPYRIGHT

This file documents the ANSI C library.

Copyright (C) 1992, 1993, 1994-2014 Red Hat, Inc.

libc includes software developed by the University of California, Berkeley and its contributors.

libc includes software developed by Martin Jackson, Graham Haley and Steve Chamberlain of Tadpole Technology and released to Cygnus.

libc uses floating-point conversion software developed at AT&T, which includes this copyright information:

The author of this software is David M. Gay.

Copyright (c) 1991 by AT&T.

Permission to use, copy, modify, and distribute this software for any purpose without fee is hereby granted, provided that this entire notice is included in all copies of any software which is or includes a copy or modification of this software and in all copies of the supporting documentation for such software.

THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, subject to the terms of the GNU General Public License, which includes the provision that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.