- fixed poll emulation management
[vde.git] / vde-2 / compat / open_memstream.c
blob68133f54f1f6d98a644d2da23c8da75c6f3e9906
1 /*
2 * Copyright (C) 2005 Richard Kettlewell
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
20 #include <config.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <assert.h>
28 /* BSD-compatible implementation of open_memstream */
30 #if HAVE_FUNOPEN
31 struct memstream {
32 char *buffer;
33 size_t size, space;
35 char **ptr;
36 size_t *sizeloc;
39 static int memstream_writefn(void *u, const char *buffer, int bytes) {
40 struct memstream *m = u;
41 size_t needed = m->size + bytes + 1;
42 size_t newspace;
43 char *newbuffer;
45 assert(bytes >= 0);
46 if(needed > m->space) {
47 newspace = m->space ? m->space : 32;
48 while(newspace && newspace < needed)
49 newspace *= 2;
50 if(!newspace) {
51 errno = ENOMEM;
52 return -1;
54 if(!(newbuffer = realloc(m->buffer, newspace)))
55 return -1;
56 m->buffer = newbuffer;
57 m->space = newspace;
59 memcpy(m->buffer + m->size, buffer, bytes);
60 m->size += bytes;
61 m->buffer[m->size] = 0;
63 *m->ptr = m->buffer;
64 *m->sizeloc = m->size;
65 return bytes;
68 FILE *open_memstream(char **ptr, size_t *sizeloc) {
69 struct memstream *m;
71 if(!(m = malloc(sizeof *m))) return 0;
72 m->buffer = 0;
73 m->size = 0;
74 m->space = 0;
75 m->ptr = ptr;
76 m->sizeloc = sizeloc;
77 *ptr = 0;
78 *sizeloc = 0;
79 return funopen(m,
80 0, /* read */
81 memstream_writefn,
82 0, /* seek */
83 0); /* close */
85 #endif