1 /* $NetBSD: open_memstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $ */
4 * Copyright (c) 2013 Advanced Computing Technologies LLC
5 * Written by: John H. Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: head/lib/libc/stdio/open_memstream.c 247411 2013-02-27 19:50:46Z jhb $");
34 __RCSID("$NetBSD: open_memstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $");
36 #include "namespace.h"
45 #define OFF_MAX LLONG_MAX
55 memstream_grow(struct memstream
*ms
, off_t newoff
)
60 if (newoff
< 0 || newoff
>= SSIZE_MAX
)
61 newsize
= SSIZE_MAX
- 1;
64 if (newsize
> ms
->len
) {
65 buf
= realloc(*ms
->bufp
, newsize
+ 1);
68 fprintf(stderr
, "MS: %p growing from %zd to %zd\n",
69 ms
, ms
->len
, newsize
);
71 memset(buf
+ ms
->len
+ 1, 0, newsize
- ms
->len
);
82 memstream_update(struct memstream
*ms
)
85 *ms
->sizep
= ms
->len
< ms
->offset
? ms
->len
: ms
->offset
;
89 memstream_write(void *cookie
, const void *buf
, size_t len
)
98 if (!memstream_grow(ms
, more
))
100 tocopy
= ms
->len
- ms
->offset
;
103 memcpy(*ms
->bufp
+ ms
->offset
, buf
, tocopy
);
104 ms
->offset
+= tocopy
;
105 memstream_update(ms
);
107 fprintf(stderr
, "MS: write(%p, %zu) = %zu\n", ms
, len
, tocopy
);
109 return (ssize_t
)tocopy
;
113 memstream_seek(void *cookie
, off_t pos
, int whence
)
115 struct memstream
*ms
;
126 /* _fseeko() checks for negative offsets. */
131 /* This is only called by _ftello(). */
136 if (pos
+ (ssize_t
)ms
->len
< 0) {
139 "MS: bad SEEK_END: pos %jd, len %zu\n",
140 (intmax_t)pos
, ms
->len
);
146 if (OFF_MAX
- (ssize_t
)ms
->len
< pos
) {
149 "MS: bad SEEK_END: pos %jd, len %zu\n",
150 (intmax_t)pos
, ms
->len
);
156 ms
->offset
= ms
->len
+ pos
;
159 memstream_update(ms
);
161 fprintf(stderr
, "MS: seek(%p, %jd, %d) %zu -> %zu\n", ms
,
162 (intmax_t)pos
, whence
, old
, ms
->offset
);
168 memstream_close(void *cookie
)
176 open_memstream(char **bufp
, size_t *sizep
)
178 struct memstream
*ms
;
182 if (bufp
== NULL
|| sizep
== NULL
) {
186 *bufp
= calloc(1, 1);
189 ms
= malloc(sizeof(*ms
));
201 memstream_update(ms
);
202 fp
= funopen2(ms
, NULL
, memstream_write
, memstream_seek
,
203 NULL
, memstream_close
);