2006-02-15 23:19:08 +00:00
|
|
|
.\" Copyright 2005 walter harms (walter.harms@informatik.uni-oldenburg.de),
|
2007-09-20 06:52:22 +00:00
|
|
|
.\" and Copyright 2005 Michael Kerrisk <mtk.manpages@gmail.com>
|
2006-01-13 00:22:47 +00:00
|
|
|
.\" Distributed under the GPL.
|
2008-12-05 17:43:35 +00:00
|
|
|
.\" 2008-12-04, Petr Baudis <pasky@suse.cz>: Document open_wmemstream()
|
2005-12-08 16:15:17 +00:00
|
|
|
.\"
|
2010-09-15 06:09:53 +00:00
|
|
|
.TH FMEMOPEN 3 2010-09-15 "GNU" "Linux Programmer's Manual"
|
2005-12-08 16:15:17 +00:00
|
|
|
.SH NAME
|
2008-12-05 17:43:35 +00:00
|
|
|
fmemopen, open_memstream, open_wmemstream \- open memory as stream
|
2005-12-08 16:15:17 +00:00
|
|
|
.SH SYNOPSIS
|
2008-12-05 17:51:35 +00:00
|
|
|
.nf
|
2005-12-08 16:15:17 +00:00
|
|
|
.B #include <stdio.h>
|
2008-12-05 17:51:35 +00:00
|
|
|
|
|
|
|
.BI "FILE *fmemopen(void *"buf ", size_t "size ", const char *" mode ");"
|
|
|
|
|
2007-07-10 04:51:30 +00:00
|
|
|
.BI "FILE *open_memstream(char **" ptr ", size_t *" sizeloc );
|
2008-12-05 17:51:35 +00:00
|
|
|
|
2008-12-05 17:43:35 +00:00
|
|
|
.B #include <wchar.h>
|
2008-12-05 17:51:35 +00:00
|
|
|
|
2008-12-05 17:43:35 +00:00
|
|
|
.BI "FILE *open_wmemstream(wchar_t **" ptr ", size_t *" sizeloc );
|
2010-09-15 06:09:53 +00:00
|
|
|
.fi
|
|
|
|
.sp
|
|
|
|
.in -4n
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
|
|
.BR feature_test_macros (7)):
|
|
|
|
.in
|
|
|
|
.sp
|
|
|
|
.BR fmemopen (),
|
|
|
|
.BR open_memstream (),
|
|
|
|
.BR open_wmemstream ():
|
faccessat.2, dprintf.3, fexecve.3, fmemopen.3, mbsnrtowcs.3, mkfifoat.3, opendir.3, stpcpy.3, stpncpy.3, strnlen.3, strsignal.3, wcpcpy.3, wcpncpy.3, wcscasecmp.3, wcsdup.3, wcsncasecmp.3, wcsnlen.3, wcsnrtombs.3: ffix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-09-19 16:57:10 +00:00
|
|
|
.PD 0
|
|
|
|
.ad l
|
|
|
|
.RS 4
|
|
|
|
.TP 4
|
|
|
|
Since glibc 2.10:
|
|
|
|
_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
|
|
|
|
.TP
|
2010-09-15 06:09:53 +00:00
|
|
|
Before glibc 2.10:
|
|
|
|
_GNU_SOURCE
|
faccessat.2, dprintf.3, fexecve.3, fmemopen.3, mbsnrtowcs.3, mkfifoat.3, opendir.3, stpcpy.3, stpncpy.3, strnlen.3, strsignal.3, wcpcpy.3, wcpncpy.3, wcscasecmp.3, wcsdup.3, wcsncasecmp.3, wcsnlen.3, wcsnrtombs.3: ffix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-09-19 16:57:10 +00:00
|
|
|
.RE
|
|
|
|
.ad
|
|
|
|
.PD
|
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 .
|
2007-04-12 22:42:49 +00:00
|
|
|
The stream allows I/O to be performed on the string or memory buffer
|
2006-01-13 00:22:47 +00:00
|
|
|
pointed to by
|
2007-04-12 22:42:49 +00:00
|
|
|
.IR buf .
|
2005-12-08 16:15:17 +00:00
|
|
|
This buffer must be at least
|
|
|
|
.I size
|
|
|
|
bytes long.
|
|
|
|
.PP
|
|
|
|
The argument
|
|
|
|
.I mode
|
|
|
|
is the same as for
|
2007-12-10 15:03:27 +00:00
|
|
|
.BR fopen (3).
|
2005-12-08 16:15:17 +00:00
|
|
|
If
|
|
|
|
.I mode
|
2007-04-12 22:42:49 +00:00
|
|
|
specifies an append mode, then the initial file position is set to
|
2008-10-12 05:12:37 +00:00
|
|
|
the location of the first null byte (\(aq\\0\(aq) in the buffer;
|
2006-01-13 00:22:47 +00:00
|
|
|
otherwise the initial file position is set to the start of the buffer.
|
2008-10-12 05:12:37 +00:00
|
|
|
Since glibc 2.9,
|
|
|
|
the letter 'b' may be specified as the second character in
|
2008-10-12 05:07:53 +00:00
|
|
|
.IR mode .
|
|
|
|
This provides "binary" mode:
|
2008-10-12 05:13:02 +00:00
|
|
|
writes don't implicitly add a terminating null byte, and
|
2008-10-12 05:07:53 +00:00
|
|
|
.BR fseek (3)
|
|
|
|
.B SEEK_END
|
|
|
|
is relative to the end of the buffer (i.e., the value specified by the
|
|
|
|
.I size
|
|
|
|
argument), rather than the current string length.
|
2005-12-08 16:15:17 +00:00
|
|
|
.PP
|
2007-04-12 22:42:49 +00:00
|
|
|
When a stream that has been opened for writing is flushed
|
2006-01-13 00:22:47 +00:00
|
|
|
.RB ( fflush (3))
|
|
|
|
or closed
|
|
|
|
.RB ( fclose (3)),
|
|
|
|
a null byte is written at the end of the buffer if there is space.
|
2007-04-12 22:42:49 +00:00
|
|
|
The caller should ensure that an extra byte is available in the
|
2006-01-13 00:22:47 +00:00
|
|
|
buffer
|
|
|
|
(and that
|
2007-09-20 16:26:31 +00:00
|
|
|
.I size
|
2006-01-13 00:22:47 +00:00
|
|
|
counts that byte)
|
2007-04-12 22:42:49 +00:00
|
|
|
to allow for this.
|
2006-01-13 00:22:47 +00:00
|
|
|
|
2005-12-08 16:15:17 +00:00
|
|
|
Attempts to write more than
|
|
|
|
.I size
|
|
|
|
bytes to the buffer result in an error.
|
2007-04-12 22:42:49 +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.
|
2007-04-12 22:42:49 +00:00
|
|
|
Disabling buffering with
|
2006-04-19 02:47:51 +00:00
|
|
|
.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.
|
2007-04-12 22:42:49 +00:00
|
|
|
Alternatively, the caller can explicitly set
|
2006-04-19 22:56:37 +00:00
|
|
|
.I buf
|
|
|
|
as the stdio stream buffer, at the same time informing stdio
|
2007-04-12 22:42:49 +00:00
|
|
|
of the buffer's size, using
|
2006-04-19 22:56:37 +00:00
|
|
|
.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
|
2007-04-12 22:42:49 +00:00
|
|
|
In a stream opened for reading,
|
2008-06-09 15:49:35 +00:00
|
|
|
null bytes (\(aq\\0\(aq) in the buffer do not cause read
|
2006-01-13 00:22:47 +00:00
|
|
|
operations to return an end-of-file indication.
|
|
|
|
A read from the buffer will only indicate end-of-file
|
2007-04-12 22:42:49 +00:00
|
|
|
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
|
2007-04-12 22:42:49 +00:00
|
|
|
If
|
|
|
|
.I buf
|
2006-01-13 00:22:47 +00:00
|
|
|
is specified as NULL, then
|
2008-07-18 08:37:37 +00:00
|
|
|
.BR fmemopen ()
|
2006-01-13 00:22:47 +00:00
|
|
|
dynamically allocates a buffer
|
2005-12-08 16:15:17 +00:00
|
|
|
.I size
|
2007-04-12 22:42:49 +00:00
|
|
|
bytes long.
|
2006-01-13 00:22:47 +00:00
|
|
|
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 ()
|
2008-04-23 17:03:47 +00:00
|
|
|
function opens a stream for writing to a buffer.
|
2005-12-08 16:15:17 +00:00
|
|
|
The buffer
|
2007-04-12 22:42:49 +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
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
When the stream is closed
|
2006-01-13 00:22:47 +00:00
|
|
|
.RB ( fclose (3))
|
2007-04-12 22:42:49 +00:00
|
|
|
or flushed
|
2006-01-13 00:22:47 +00:00
|
|
|
.RB ( fflush (3)),
|
|
|
|
the locations pointed to by
|
2007-04-12 22:42:49 +00:00
|
|
|
.I ptr
|
|
|
|
and
|
2005-12-08 16:15:17 +00:00
|
|
|
.I sizeloc
|
2006-01-13 00:22:47 +00:00
|
|
|
are updated to contain, respectively, a pointer to the buffer and the
|
2007-04-12 22:42:49 +00:00
|
|
|
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
|
2006-01-13 00:22:47 +00:00
|
|
|
must again be flushed before trying to access these variables.
|
2005-12-08 16:15:17 +00:00
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
A null byte is maintained at the end of the buffer.
|
|
|
|
This byte is
|
|
|
|
.I not
|
|
|
|
included in the size value stored at
|
2005-12-08 16:15:17 +00:00
|
|
|
.IR sizeloc .
|
2008-08-21 10:06:18 +00:00
|
|
|
|
|
|
|
The stream's file position can be changed with
|
|
|
|
.BR fseek (3)
|
|
|
|
or
|
|
|
|
.BR fseeko (3).
|
|
|
|
Moving the file position past the end
|
|
|
|
of the data already written fills the intervening space with
|
|
|
|
zeros.
|
2008-12-05 17:43:35 +00:00
|
|
|
|
|
|
|
The
|
|
|
|
.BR open_wmemstream ()
|
2008-12-05 17:52:11 +00:00
|
|
|
is similar to
|
|
|
|
.BR open_memstream (),
|
|
|
|
but operates on wide characters instead of bytes.
|
2005-12-08 16:15:17 +00:00
|
|
|
.SH "RETURN VALUE"
|
2007-04-12 22:42:49 +00:00
|
|
|
Upon successful completion
|
2008-12-05 17:43:35 +00:00
|
|
|
.BR fmemopen (),
|
2005-12-08 16:15:17 +00:00
|
|
|
.BR open_memstream ()
|
2008-12-05 17:43:35 +00:00
|
|
|
and
|
|
|
|
.BR open_wmemstream ()
|
2005-12-08 16:15:17 +00:00
|
|
|
return a
|
2007-06-22 20:40:07 +00:00
|
|
|
.I FILE
|
2007-04-12 22:42:49 +00:00
|
|
|
pointer.
|
2009-02-23 02:34:12 +00:00
|
|
|
Otherwise, NULL is returned and
|
2007-04-12 22:42:49 +00:00
|
|
|
.I errno
|
2005-12-08 16:15:17 +00:00
|
|
|
is set to indicate the error.
|
2008-12-05 17:47:17 +00:00
|
|
|
.SH VERSIONS
|
|
|
|
.BR fmemopen ()
|
2008-12-06 17:03:31 +00:00
|
|
|
and
|
2008-12-05 17:47:17 +00:00
|
|
|
.BR open_memstream ()
|
2008-12-06 17:03:31 +00:00
|
|
|
were already available in glibc 1.0.x.
|
2008-12-05 17:47:17 +00:00
|
|
|
.BR open_wmemstream ()
|
|
|
|
is available since glibc 2.4.
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH "CONFORMING TO"
|
2009-02-02 20:20:52 +00:00
|
|
|
POSIX.1-2008.
|
|
|
|
These functions are not specified in POSIX.1-2001,
|
|
|
|
and are not widely available on other systems.
|
2009-04-21 04:12:05 +00:00
|
|
|
.SH NOTES
|
|
|
|
There is no file descriptor associated with the file stream
|
|
|
|
returned by these functions
|
2009-07-25 06:51:28 +00:00
|
|
|
(i.e.,
|
2009-04-21 04:12:05 +00:00
|
|
|
.BR fileno (3)
|
|
|
|
will return an error if called on the returned stream).
|
2009-04-21 04:03:54 +00:00
|
|
|
.SH BUGS
|
|
|
|
In glibc before version 2.7, seeking past the end of a stream created by
|
|
|
|
.BR open_memstream ()
|
|
|
|
does not enlarge the buffer; instead the
|
|
|
|
.BR fseek ()
|
|
|
|
call fails, returning \-1.
|
|
|
|
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
|
2005-12-08 16:15:17 +00:00
|
|
|
.SH "EXAMPLE"
|
2007-04-12 22:42:49 +00:00
|
|
|
The program below uses
|
2005-12-08 16:15:17 +00:00
|
|
|
.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:
|
2008-10-28 03:13:46 +00:00
|
|
|
.in +4n
|
2005-12-08 16:15:17 +00:00
|
|
|
.nf
|
|
|
|
|
eventfd.2, execve.2, getdents.2, ioprio_set.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, frexp.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, malloc.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strftime.3, strtok.3, strtol.3, core.5, proc.5, cpuset.7, mq_overview.7: Global fix: Format user input in shell sessions in boldface
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-10-28 15:37:22 +00:00
|
|
|
.RB "$" " ./a.out \(aq1 23 43\(aq"
|
2005-12-08 16:15:17 +00:00
|
|
|
size=11; ptr=1 529 1849
|
2008-10-28 03:13:46 +00:00
|
|
|
.fi
|
|
|
|
.in
|
eventfd.2, getdents.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strtol.3, core.5: global fix: Add ".SS Program source" to EXAMPLE
Add ".SS Program source" to clearly distinguish shell session and
descriptive text from actual program code.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-10-28 15:51:05 +00:00
|
|
|
.SS Program source
|
eventfd.2, getdents.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, frexp.3, getaddrinfo.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strftime.3, strtok.3, strtol.3, core.5: srcfix
s/\.R " "/\\\&/ as a way of getting a blank line after a .SS heading.
(Suggested by Sam Varshavchik <mrsam@courier-mta.com>)
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-11-05 01:42:45 +00:00
|
|
|
\&
|
2008-10-28 03:13:46 +00:00
|
|
|
.nf
|
2005-12-08 16:15:17 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-11-19 03:25:26 +00:00
|
|
|
#define handle_error(msg) \\
|
|
|
|
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
2007-06-02 08:31:25 +00:00
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
int
|
2007-04-05 12:36:57 +00:00
|
|
|
main(int argc, char *argv[])
|
2005-12-08 16:15:17 +00:00
|
|
|
{
|
|
|
|
FILE *out, *in;
|
|
|
|
int v, s;
|
|
|
|
size_t size;
|
|
|
|
char *ptr;
|
|
|
|
|
execve.2, pipe.2, tee.2, fmemopen.3, mq_notify.3, qsort.3: Replace use of assert() by code that checks argc
See http://bugzilla.kernel.org/show_bug.cgi?id=13569
As noted by Andrey:
The purpose of the assert macro, defined in <assert.h>,
is to provide a tool to check for programming mistakes
or program logic errors. However, the assert macro must
never be used to perform checks for run time errors,
since, with the NDEBUG macro defined, expressions within
the assert macro invocations are not evaluated/checked
for, resulting in behavior that was not originally intended.
...
The pages affected in the core package are
execve(2)
pipe(2)
tee(2)
fmemopen(3)
mq_notify(3)
qsort(3)
Reported-by: Andrey Vihrov <vihrov@gmail.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-09-15 04:46:16 +00:00
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s <file>\\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2005-12-08 16:15:17 +00:00
|
|
|
|
|
|
|
in = fmemopen(argv[1], strlen(argv[1]), "r");
|
2007-06-02 08:31:25 +00:00
|
|
|
if (in == NULL)
|
2007-11-19 03:25:26 +00:00
|
|
|
handle_error("fmemopen");
|
2005-12-08 16:15:17 +00:00
|
|
|
|
|
|
|
out = open_memstream(&ptr, &size);
|
2007-06-02 08:31:25 +00:00
|
|
|
if (out == NULL)
|
2010-06-10 01:15:12 +00:00
|
|
|
handle_error("open_memstream");
|
2005-12-08 16:15:17 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
s = fscanf(in, "%d", &v);
|
|
|
|
if (s <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
s = fprintf(out, "%d ", v * v);
|
2007-06-20 21:39:45 +00:00
|
|
|
if (s == \-1)
|
2007-11-19 03:25:26 +00:00
|
|
|
handle_error("fprintf");
|
2005-12-08 16:15:17 +00:00
|
|
|
}
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
printf("size=%ld; ptr=%s\\n", (long) size, ptr);
|
|
|
|
free(ptr);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
.fi
|
|
|
|
.SH "SEE ALSO"
|
2007-06-01 06:24:41 +00:00
|
|
|
.BR fopen (3),
|
2008-12-05 16:25:19 +00:00
|
|
|
.BR fopencookie (3),
|
2007-01-28 20:00:24 +00:00
|
|
|
.BR feature_test_macros (7)
|