2.2.41 FILE* fdevopen
FILE* fdevopen(int(*put)(char, FILE *), int(*get)(FILE *))
This function is a replacement for fopen()
.
It opens a stream for a device where the actual device implementation needs to be provided by the application. If successful, a pointer to the structure for the opened stream is returned. Reasons for a possible failure currently include that neither the put
nor the get
argument have been provided, thus attempting to open a stream with no IO intent at all, or that insufficient dynamic memory is available to establish a new stream.
If the put
function pointer is provided, the stream is opened with write intent. The function passed as put
shall take two arguments, the first a character to write to the device, and the second a pointer to FILE, and shall return 0 if the output was successful, and a nonzero value if the character could not be sent to the device.
If the get
function pointer is provided, the stream is opened with read intent. The function passed as get
shall take a pointer to FILE as its single argument, and return one character from the device, passed as an int
type. If an error occurs when trying to read from the device, it shall return _FDEV_ERR
. If an end-of-file condition was reached while reading from the device, _FDEV_EOF
shall be returned.
If both functions are provided, the stream is opened with read and write intent.
The first stream opened with read intent is assigned to stdin
, and the first one opened with write intent is assigned to both, stdout
and stderr
.
fdevopen() uses calloc() (und thus malloc()) in order to allocate the storage for the new stream.
If the macro __STDIO_FDEVOPEN_COMPAT_12 is declared before including <stdio.h>, a function prototype for fdevopen() will be chosen that is backwards compatible with avr-libc version 1.2 and before. This is solely intented for providing a simple migration path without the need to immediately change all source code. Do not use for new code.