2 * This file is part of ltrace.
3 * Copyright (C) 2012 Petr Machata, Red Hat Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 /* _GNU_SOURCE may be necessary for open_memstream visibility (see
22 * configure.ac), and there's no harm defining it just in case. */
29 #include "memstream.h"
32 memstream_init(struct memstream *memstream)
34 #if HAVE_OPEN_MEMSTREAM
35 memstream->stream = open_memstream(&memstream->buf,
38 memstream->stream = tmpfile();
39 memstream->buf = NULL;
41 return memstream->stream != NULL ? 0 : -1;
45 memstream_close(struct memstream *memstream)
47 #if !defined(HAVE_OPEN_MEMSTREAM) || !HAVE_OPEN_MEMSTREAM
48 if (fseek(memstream->stream, 0, SEEK_END) < 0) {
50 fclose(memstream->stream);
53 memstream->size = ftell(memstream->stream);
54 if (memstream->size == (size_t)-1)
56 memstream->buf = malloc(memstream->size);
57 if (memstream->buf == NULL)
60 rewind(memstream->stream);
61 if (fread(memstream->buf, 1, memstream->size, memstream->stream)
66 return fclose(memstream->stream) == 0 ? 0 : -1;
70 memstream_destroy(struct memstream *memstream)