1 /* $NetBSD: fmemopen.c,v 1.4 2010/09/27 16:50:13 tnozaki Exp $ */
4 * Copyright (c)2007, 2010 Takehiko NOZAKI,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
39 #include "priv_stdio.h"
41 struct fmemopen_cookie {
42 char *head, *tail, *cur, *eob;
45 static int min(int x, int y) {
46 return (x > y) ? (y) : (x);
50 fmemopen_read(void *cookie, char *buf, int nbytes)
52 struct fmemopen_cookie *p;
56 assert(cookie != NULL);
57 assert(buf != NULL && nbytes > 0);
59 p = (struct fmemopen_cookie *)cookie;
61 len = min(p->tail - p->cur, nbytes);
62 bcopy(p->cur, buf, len);
65 return (int)(p->cur - s);
69 fmemopen_write(void *cookie, const char *buf, int nbytes)
71 struct fmemopen_cookie *p;
74 assert(cookie != NULL);
75 assert(buf != NULL && nbytes > 0);
77 p = (struct fmemopen_cookie *)cookie;
78 if (p->cur >= p->tail)
82 if (p->cur == p->tail - 1) {
90 } while (--nbytes > 0);
96 return (int)(p->cur - s);
100 fmemopen_seek(void *cookie, fpos_t offset, int whence)
102 struct fmemopen_cookie *p;
104 assert(cookie != NULL);
106 p = (struct fmemopen_cookie *)cookie;
111 offset += p->cur - p->head;
114 offset += p->eob - p->head;
120 if (offset >= (fpos_t)0 && offset <= p->tail - p->head) {
121 p->cur = p->head + (ptrdiff_t)offset;
122 return (fpos_t)(p->cur - p->head);
129 fmemopen_close0(void *cookie)
131 assert(cookie != NULL);
139 fmemopen_close1(void *cookie)
141 struct fmemopen_cookie *p;
143 assert(cookie != NULL);
145 p = (struct fmemopen_cookie *)cookie;
154 fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
158 struct fmemopen_cookie *cookie;
160 if (size < (size_t)1)
163 flags = __sflags(mode, &oflags);
167 if ((oflags & O_RDWR) == 0 && buf == NULL)
174 cookie = malloc(sizeof(*cookie));
179 cookie->head = malloc(size);
180 if (cookie->head == NULL) {
184 *cookie->head = '\0';
185 fp->_close = &fmemopen_close1;
187 cookie->head = (char *)buf;
188 if (oflags & O_TRUNC)
189 *cookie->head = '\0';
190 fp->_close = &fmemopen_close0;
193 cookie->tail = cookie->head + size;
194 cookie->eob = cookie->head;
196 if (*cookie->eob == '\0')
199 } while (--size > 0);
201 cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
203 fp->pub._flags = flags;
204 fp->_write = (flags & __SRD) ? NULL : &fmemopen_write;
205 fp->_read = (flags & __SWR) ? NULL : &fmemopen_read;
206 fp->_seek = &fmemopen_seek;
207 fp->_cookie = (void *)cookie;