2006-02-15 23:19:08 +00:00
|
|
|
.\" Copyright 2005 walter harms (walter.harms@informatik.uni-oldenburg.de),
|
2006-02-15 23:15:29 +00:00
|
|
|
.\" and Copyright 2005 Michael Kerrisk <mtk-manpages@gmx.net>
|
2006-01-13 00:22:47 +00:00
|
|
|
.\" Distributed under the GPL.
|
2005-12-08 16:15:17 +00:00
|
|
|
.\"
|
|
|
|
.TH FMEMOPEN 3 2005-12-08 "GNU" "glibc function"
|
|
|
|
.SH NAME
|
|
|
|
fmemopen, open_memstream \- open memory as stream
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #define _GNU_SOURCE
|
|
|
|
.br
|
|
|
|
.B #include <stdio.h>
|
|
|
|
.sp
|
2006-05-29 05:10:07 +00:00
|
|
|
.BI "FILE *fmemopen(void *"buf ", size_t "size ","
|
2005-12-08 16:15:17 +00:00
|
|
|
.BI "const char *" mode ");"
|
|
|
|
.sp
|
2006-05-29 05:10:26 +00:00
|
|
|
.BI "FILE *open_memstream(char ** "ptr ", size_t *" sizeloc )
|
2005-12-08 16:15:17 +00:00
|
|
|
.SH DESCRIPTION
|
|
|
|
The
|
|
|
|
.BR fmemopen ()
|
|
|
|
function opens a stream that permits the access specified by
|
|
|
|
.IR mode .
|
2006-01-13 00:22:47 +00:00
|
|
|
The stream allows I/O to be performed on the string or memory buffer
|
|
|
|
pointed to by
|
2005-12-08 16:15:17 +00:00
|
|
|
.IR buf .
|
|
|
|
This buffer must be at least
|
|
|
|
.I size
|
|
|
|
bytes long.
|
|
|
|
.PP
|
|
|
|
The argument
|
|
|
|
.I mode
|
|
|
|
is the same as for
|
2006-01-13 00:22:47 +00:00
|
|
|
.BR fopen (3) .
|
2005-12-08 16:15:17 +00:00
|
|
|
If
|
|
|
|
.I mode
|
2006-01-13 00:22:47 +00:00
|
|
|
specifies an append mode, then the initial file position is set to
|
|
|
|
location of the first null byte ('\\0') in the buffer;
|
|
|
|
otherwise the initial file position is set to the start of the buffer.
|
2005-12-08 16:15:17 +00:00
|
|
|
.PP
|
2006-01-13 00:22:47 +00:00
|
|
|
When a stream that has been opened for writing is flushed
|
|
|
|
.RB ( fflush (3))
|
|
|
|
or closed
|
|
|
|
.RB ( fclose (3)),
|
|
|
|
a null byte is written at the end of the buffer if there is space.
|
|
|
|
The caller should ensure that an extra byte is available in the
|
|
|
|
buffer
|
|
|
|
(and that
|
|
|
|
.IR size
|
|
|
|
counts that byte)
|
|
|
|
to allow for this.
|
|
|
|
|
2005-12-08 16:15:17 +00:00
|
|
|
Attempts to write more than
|
|
|
|
.I size
|
|
|
|
bytes to the buffer result in an error.
|
2006-04-19 22:56:37 +00:00
|
|
|
(By default, such errors will only be visible when the
|
2006-04-19 02:47:51 +00:00
|
|
|
.I stdio
|
|
|
|
buffer is flushed.
|
|
|
|
Disabling buffering with
|
|
|
|
.I setbuf(fp,\ NULL)
|
2006-04-19 22:56:37 +00:00
|
|
|
may be useful to detect errors at the time of an output operation.
|
|
|
|
Alternatively, the caller can explicitly set
|
|
|
|
.I buf
|
|
|
|
as the stdio stream buffer, at the same time informing stdio
|
|
|
|
of the buffer's size, using
|
|
|
|
.IR "setbuffer(fp, buf, size)" .)
|
2006-04-23 23:15:26 +00:00
|
|
|
.\" See http://sourceware.org/bugzilla/show_bug.cgi?id=1995
|
|
|
|
.\" and
|
2006-04-21 19:01:55 +00:00
|
|
|
.\" http://sources.redhat.com/ml/libc-alpha/2006-04/msg00064.html
|
2005-12-08 16:15:17 +00:00
|
|
|
.PP
|
2006-01-13 00:22:47 +00:00
|
|
|
In a stream opened for reading,
|
|
|
|
null bytes ('\\0') in the buffer do not cause read
|
|
|
|
operations to return an end-of-file indication.
|
|
|
|
A read from the buffer will only indicate end-of-file
|
|
|
|
when the file pointer advances
|
2005-12-08 16:15:17 +00:00
|
|
|
.I size
|
2006-01-13 00:22:47 +00:00
|
|
|
bytes past the start of the buffer.
|
|
|
|
.PP
|
|
|
|
If
|
|
|
|
.I buf
|
|
|
|
is specified as NULL, then
|
|
|
|
.BR fmemopen ()
|
|
|
|
dynamically allocates a buffer
|
2005-12-08 16:15:17 +00:00
|
|
|
.I size
|
2006-01-13 00:22:47 +00:00
|
|
|
bytes long.
|
|
|
|
This is useful for an application that wants to write data to
|
|
|
|
a temporary buffer and then read it back again.
|
|
|
|
The buffer is automatically freed when the stream is closed.
|
|
|
|
Note that the caller has no way to obtain a pointer to the
|
|
|
|
temporary buffer allocated by this call (but see
|
|
|
|
.BR open_memstream ()
|
|
|
|
below).
|
2005-12-08 16:15:17 +00:00
|
|
|
|
|
|
|
The
|
|
|
|
.BR open_memstream ()
|
|
|
|
opens a stream for writing to a buffer.
|
|
|
|
The buffer
|
2006-01-13 00:22:47 +00:00
|
|
|
is dynamically allocated (as with
|
2005-12-08 16:15:17 +00:00
|
|
|
.BR malloc (3)),
|
2006-01-13 00:22:47 +00:00
|
|
|
and automatically grows as required.
|
2005-12-08 16:15:17 +00:00
|
|
|
After closing the stream, the caller should
|
|
|
|
.BR free (3)
|
2005-12-12 09:27:49 +00:00
|
|
|
this buffer.
|
2005-12-08 16:15:17 +00:00
|
|
|
|
2006-01-13 00:22:47 +00:00
|
|
|
When the stream is closed
|
|
|
|
.RB ( fclose (3))
|
|
|
|
or flushed
|
|
|
|
.RB ( fflush (3)),
|
|
|
|
the locations pointed to by
|
2005-12-08 16:15:17 +00:00
|
|
|
.I ptr
|
|
|
|
and
|
|
|
|
.I sizeloc
|
2006-01-13 00:22:47 +00:00
|
|
|
are updated to contain, respectively, a pointer to the buffer and the
|
|
|
|
current size of the buffer.
|
|
|
|
These values remain valid only as long as the caller
|
|
|
|
performs no further output on the stream.
|
|
|
|
If further output is performed, then the stream
|
|
|
|
must again be flushed before trying to access these variables.
|
2005-12-08 16:15:17 +00:00
|
|
|
|
2006-01-13 00:22:47 +00:00
|
|
|
A null byte is maintained at the end of the buffer.
|
|
|
|
This byte is
|
2005-12-08 16:15:17 +00:00
|
|
|
.I not
|
|
|
|
included in the size value stored at
|
|
|
|
.IR sizeloc .
|
|
|
|
.\"
|
2006-03-20 04:46:28 +00:00
|
|
|
.\" FIXME The glibc info doc has text like the following, but it appears
|
|
|
|
.\" not to be true: http://sourceware.org/bugzilla/show_bug.cgi?id=1996
|
2006-02-08 09:44:13 +00:00
|
|
|
.\" The stream's file position can be changed with
|
2005-12-08 16:15:17 +00:00
|
|
|
.\" .BR fseek (3)
|
|
|
|
.\" or
|
|
|
|
.\" .BR fseeko (3).
|
|
|
|
.\" Moving the file position past the end
|
|
|
|
.\" of the data already written fills the intervening space with
|
|
|
|
.\" zeroes.
|
|
|
|
.SH "RETURN VALUE"
|
|
|
|
Upon successful completion
|
|
|
|
.BR fmemopen ()
|
|
|
|
and
|
|
|
|
.BR open_memstream ()
|
|
|
|
return a
|
|
|
|
.B FILE
|
|
|
|
pointer.
|
|
|
|
Otherwise, NULL is returned and the global variable
|
|
|
|
.I errno
|
|
|
|
is set to indicate the error.
|
|
|
|
|
|
|
|
.SH "EXAMPLE"
|
|
|
|
The program below uses
|
|
|
|
.BR fmemopen ()
|
|
|
|
to open an input buffer, and
|
|
|
|
.BR open_memstream ()
|
|
|
|
to open a dynamically sized output buffer.
|
|
|
|
The program scans its input string (taken from the program's
|
|
|
|
first command-line argument) reading integers,
|
|
|
|
and writes the squares of these integers to the output buffer.
|
2006-01-13 00:22:47 +00:00
|
|
|
An example of the output produced by this program is the following:
|
2005-12-08 16:15:17 +00:00
|
|
|
.nf
|
|
|
|
|
|
|
|
$ ./a.out "1 23 43"
|
|
|
|
size=11; ptr=1 529 1849
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *out, *in;
|
|
|
|
int v, s;
|
|
|
|
size_t size;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
assert(argc == 2);
|
|
|
|
|
|
|
|
in = fmemopen(argv[1], strlen(argv[1]), "r");
|
|
|
|
if (in == NULL) { perror("fmemopen"); exit(EXIT_FAILURE);}
|
|
|
|
|
|
|
|
out = open_memstream(&ptr, &size);
|
|
|
|
if (out == NULL) { perror("fmemopen"); exit(EXIT_FAILURE);}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
s = fscanf(in, "%d", &v);
|
|
|
|
if (s <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
s = fprintf(out, "%d ", v * v);
|
|
|
|
if (s == -1) { perror("fprintf"); exit(EXIT_FAILURE); }
|
|
|
|
}
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
printf("size=%ld; ptr=%s\\n", (long) size, ptr);
|
|
|
|
free(ptr);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
.fi
|
|
|
|
.sp
|
|
|
|
.SH "CONFORMING TO"
|
|
|
|
These functions are GNU extensions.
|
2006-01-13 00:22:47 +00:00
|
|
|
.\" Jan 06: But they appear to be going up for standardization by
|
|
|
|
.\" POSIX/PASC/IEEE.
|
2005-12-08 16:15:17 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR open (3)
|