Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

fmemopen(3) [netbsd man page]

FMEMOPEN(3)						   BSD Library Functions Manual 					       FMEMOPEN(3)

NAME
fmemopen -- open a stream that points to the given buffer LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdio.h> FILE * fmemopen(void *restrict buffer, size_t size, const char *restrict mode); DESCRIPTION
The fmemopen() function associates a stream with the given buffer and size. The buffer can be either NULL, or must be of the given size. If the buffer is NULL, a buffer of the given size will be dynamically allocated using malloc(3) and freed when fclose(3) is called. The mode argument has the same meaning as in fopen(3). The stream treats the buffer as it would treat a file tracking the current position to perform I/O operations. For example, in the beginning the stream points to the beginning of the buffer, unless a was specified in the mode argument, and then it points to the first NUL byte. If a NULL buffer was specified, then the stream will always point at the first byte of the buffer. The stream also keeps track of the size of the buffer. The size is initialized depending on the mode: r/r+ Set to the size argument. w/w+ Set to 0. a/a+ Set to the first NUL byte, or the size argument if one is not found. Read or write operations advance the buffer, but not to exceed the given size of the buffer. Trying to read beyond the size of the buffer results in EOF returned. NUL bytes are read normally. Trying to write beyond the size of the buffer has no effect. When a stream open for writing is either flushed or closed, a NUL byte is written at the current position or at the end of the current size as kept internally, if there is room. RETURN VALUES
Upon successful completion, fmemopen() returns a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error. ERRORS
[EINVAL] The size was 0; or the mode argument is invalid; or the buffer argument is NULL and the mode argument does not specify a +. The fmemopen() function may also fail and set errno for any of the errors specified for the routine malloc(3). SEE ALSO
fclose(3), fflush(3), fopen(3), malloc(3) STANDARDS
The fmemopen() function conforms to IEEE Std 1003.1-2008 (``POSIX.1''). HISTORY
The fmemopen() functions first appeared in NetBSD 6.0. BSD
October 15, 2011 BSD

Check Out this Related Man Page

FMEMOPEN(3P)						     POSIX Programmer's Manual						      FMEMOPEN(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
fmemopen -- open a memory buffer stream SYNOPSIS
#include <stdio.h> FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode); DESCRIPTION
The fmemopen() function shall associate the buffer given by the buf and size arguments with a stream. The buf argument shall be either a null pointer or point to a buffer that is at least size bytes long. The mode argument points to a string. If the string is one of the following, the stream shall be opened in the indicated mode. Otherwise, the behavior is undefined. r Open the stream for reading. w Open the stream for writing. a Append; open the stream for writing at the first null byte. r+ Open the stream for update (reading and writing). w+ Open the stream for update (reading and writing). Truncate the buffer contents. a+ Append; open the stream for update (reading and writing); the initial position is at the first null byte. Implementations shall accept all mode strings allowed by fopen(), but the use of the character 'b' shall produce implementation-defined results, where the resulting FILE * need not behave the same as if 'b' were omitted. If a null pointer is specified as the buf argument, fmemopen() shall allocate size bytes of memory as if by a call to malloc(). This buf- fer shall be automatically freed when the stream is closed. Because this feature is only useful when the stream is opened for updating (because there is no way to get a pointer to the buffer) the fmemopen() call may fail if the mode argument does not include a '+'. The stream shall maintain a current position in the buffer. This position shall be initially set to either the beginning of the buffer (for r and w modes) or to the first null byte in the buffer (for a modes). If no null byte is found in append mode, the initial position shall be set to one byte after the end of the buffer. If buf is a null pointer, the initial position shall always be set to the beginning of the buffer. The stream shall also maintain the size of the current buffer contents; use of fseek() or fseeko() on the stream with SEEK_END shall seek relative to this size. For modes r and r+ the size shall be set to the value given by the size argument. For modes w and w+ the initial size shall be zero and for modes a and a+ the initial size shall be either the position of the first null byte in the buffer or the value of the size argument if no null byte is found. A read operation on the stream shall not advance the current buffer position beyond the current buffer size. Reaching the buffer size in a read operation shall count as ``end-of-file''. Null bytes in the buffer shall have no special meaning for reads. The read operation shall start at the current buffer position of the stream. A write operation shall start either at the current position of the stream (if mode has not specified 'a' as the first character) or at the current size of the stream (if mode had 'a' as the first character). If the current position at the end of the write is larger than the current buffer size, the current buffer size shall be set to the current position. A write operation on the stream shall not advance the current buffer size beyond the size given in the size argument. When a stream open for writing is flushed or closed, a null byte shall be written at the current position or at the end of the buffer, depending on the size of the contents. If a stream open for update is flushed or closed and the last write has advanced the current buffer size, a null byte shall be written at the end of the buffer if it fits. An attempt to seek a memory buffer stream to a negative position or to a position larger than the buffer size given in the size argument shall fail. RETURN VALUE
Upon successful completion, fmemopen() shall return a pointer to the object controlling the stream. Otherwise, a null pointer shall be returned, and errno shall be set to indicate the error. ERRORS
The fmemopen() function shall fail if: EINVAL The size argument specifies a buffer size of zero. The fmemopen() function may fail if: EINVAL The value of the mode argument is not valid. EINVAL The buf argument is a null pointer and the mode argument does not include a '+' character. ENOMEM The buf argument is a null pointer and the allocation of a buffer of length size has failed. EMFILE {FOPEN_MAX} streams are currently open in the calling process. The following sections are informative. EXAMPLES
#include <stdio.h> #include <string.h> static char buffer[] = "foobar"; int main (void) { int ch; FILE *stream; stream = fmemopen(buffer, strlen (buffer), "r"); if (stream == NULL) /* handle error */; while ((ch = fgetc(stream)) != EOF) printf("Got %c ", ch); fclose(stream); return(0); } This program produces the following output: Got f Got o Got o Got b Got a Got r APPLICATION USAGE
None. RATIONALE
This interface has been introduced to eliminate many of the errors encountered in the construction of strings, notably overflowing of strings. This interface prevents overflow. FUTURE DIRECTIONS
A future revision of this standard may mandate specific behavior when the mode argument includes 'b'. SEE ALSO
fdopen(), fopen(), freopen(), fseek(), malloc(), open_memstream() The Base Definitions volume of POSIX.1-2008, <stdio.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Stan- dard is the referee document. The original Standard can be obtained online at http://www.unix.org/online.html . Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . IEEE
/The Open Group 2013 FMEMOPEN(3P)
Man Page