1 /* $NetBSD: fmemopen.c,v 1.4 2010/09/27 16:50:13 tnozaki Exp $ */
4 * Copyright (c)2007, 2010 Takehiko NOZAKI,
5 * Copyright (c) 2012, Venkatesh Srinivas <vsrinivas@dragonflybsd.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/param.h>
39 #include "priv_stdio.h"
41 struct fmemopen_cookie {
42 char *head, *tail, *cur, *eob;
46 fmemopen_read(void *cookie, char *buf, int nbytes)
48 struct fmemopen_cookie *p;
52 assert(cookie != NULL);
53 assert(buf != NULL && nbytes > 0);
57 len = MIN(p->tail - p->cur, nbytes);
58 bcopy(p->cur, buf, len);
61 return (int)(p->cur - s);
65 fmemopen_write(void *cookie, const char *buf, int nbytes)
67 struct fmemopen_cookie *p;
71 assert(cookie != NULL);
72 assert(buf != NULL && nbytes > 0);
75 if (p->cur >= p->tail)
79 len = MIN(p->tail - p->cur, nbytes);
81 bcopy(buf, p->cur, len);
84 if (p->cur == p->tail - 1) {
86 if (buf[len - 1] == '\0')
95 return (int)(p->cur - s);
99 fmemopen_seek(void *cookie, fpos_t offset, int whence)
101 struct fmemopen_cookie *p;
103 assert(cookie != NULL);
105 p = (struct fmemopen_cookie *)cookie;
110 offset += p->cur - p->head;
113 offset += p->eob - p->head;
119 if (offset >= (fpos_t)0 && offset <= p->tail - p->head) {
120 p->cur = p->head + (ptrdiff_t)offset;
121 return (fpos_t)(p->cur - p->head);
128 fmemopen_close0(void *cookie)
130 assert(cookie != NULL);
138 fmemopen_close1(void *cookie)
140 struct fmemopen_cookie *p;
142 assert(cookie != NULL);
153 fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
157 struct fmemopen_cookie *cookie;
159 if (size < (size_t)1)
162 flags = __sflags(mode, &oflags);
166 if ((oflags & O_RDWR) == 0 && buf == NULL)
173 cookie = malloc(sizeof(*cookie));
178 cookie->head = malloc(size);
179 if (cookie->head == NULL) {
183 *cookie->head = '\0';
184 fp->_close = &fmemopen_close1;
186 cookie->head = (char *)buf;
187 if (oflags & O_TRUNC)
188 *cookie->head = '\0';
189 fp->_close = &fmemopen_close0;
192 cookie->tail = cookie->head + size;
193 cookie->eob = cookie->head;
195 if (*cookie->eob == '\0')
198 } while (--size > 0);
200 cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
202 fp->pub._flags = flags;
203 fp->_write = (flags & __SRD) ? NULL : &fmemopen_write;
204 fp->_read = (flags & __SWR) ? NULL : &fmemopen_read;
205 fp->_seek = &fmemopen_seek;
206 fp->_cookie = (void *)cookie;