FMEMOPEN(3)

NAME
fmemopen - open a stream around a fixed-length string

SYNOPSIS

#include <stdio.h>

FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode);

DESCRIPTION

fmemopen creates a seekable FILE stream that wraps a fixed-length buffer of size bytes starting at buf. The stream is opened with mode treated as in fopen, where append mode starts writing at the first NUL byte. If buf is NULL, then size bytes are automatically provided as if by malloc, with the initial size of 0, and mode must contain + so that data can be read after it is written.

The stream maintains a current position, which moves according to bytes read or written, and which can be one past the end of the array. The stream also maintains a current file size, which is never greater than size. If mode starts with r, the position starts at 0, and file size starts at size if buf was provided. If mode starts with w, the position and file size start at 0, and if buf was provided, the first byte is set to NUL. If mode starts with a, the position and file size start at the location of the first NUL byte, or else size if buf was provided.

When reading, NUL bytes have no significance, and reads cannot exceed the current file size. When writing, the file size can increase up to size as needed, and NUL bytes may be embedded in the stream (see open_memstream for an alternative that automatically enlarges the buffer). When the stream is flushed or closed after a write that changed the file size, a NUL byte is written at the current position if there is still room; if the stream is not also open for reading, a NUL byte is additionally written at the last byte of buf when the stream has exceeded size, so that a write-only buf is always NUL-terminated when the stream is flushed or closed (and the initial size should take this into account). It is not possible to seek outside the bounds of size. A NUL byte written during a flush is restored to its previous value when seeking elsewhere in the string.

RETURNS

The return value is an open FILE pointer on success. On error, NULL is returned, and errno will be set to EINVAL if size is zero or mode is invalid, ENOMEM if buf was NULL and memory could not be allocated, or EMFILE if too many streams are already open.

PORTABILITY

This function is being added to POSIX 200x, but is not in POSIX 2001.

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.