Update fmemopen from NetBSD.
[dragonfly.git] / lib / libc / stdio / fmemopen.c
CommitLineData
395d17df
VS
1/* $NetBSD: fmemopen.c,v 1.4 2010/09/27 16:50:13 tnozaki Exp $ */
2
3/*-
4 * Copyright (c)2007, 2010 Takehiko NOZAKI,
5 * All rights reserved.
e5afb31f
VS
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
e5afb31f
VS
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
395d17df
VS
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
e5afb31f 15 *
395d17df
VS
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
e5afb31f 26 * SUCH DAMAGE.
e5afb31f 27 *
e5afb31f
VS
28 */
29
395d17df
VS
30#include <sys/cdefs.h>
31#include <assert.h>
32#include <errno.h>
33#include <fcntl.h>
34#include <stddef.h>
e5afb31f
VS
35#include <stdio.h>
36#include <stdlib.h>
e5afb31f 37
395d17df
VS
38#include "local.h"
39#include "priv_stdio.h"
e5afb31f 40
e5afb31f 41struct fmemopen_cookie {
395d17df 42 char *head, *tail, *cur, *eob;
e5afb31f
VS
43};
44
395d17df
VS
45static int min(int x, int y) {
46 return (x > y) ? (y) : (x);
47}
48
c43830c0 49static int
395d17df 50fmemopen_read(void *cookie, char *buf, int nbytes)
e5afb31f 51{
395d17df
VS
52 struct fmemopen_cookie *p;
53 char *s;
54 int len;
c43830c0 55
395d17df
VS
56 assert(cookie != NULL);
57 assert(buf != NULL && nbytes > 0);
c43830c0 58
395d17df
VS
59 p = (struct fmemopen_cookie *)cookie;
60 s = p->cur;
61 len = min(p->tail - p->cur, nbytes);
62 bcopy(p->cur, buf, len);
63 p->cur += len;
c43830c0 64
395d17df 65 return (int)(p->cur - s);
e5afb31f
VS
66}
67
c43830c0 68static int
395d17df 69fmemopen_write(void *cookie, const char *buf, int nbytes)
e5afb31f 70{
395d17df
VS
71 struct fmemopen_cookie *p;
72 char *s;
73
74 assert(cookie != NULL);
75 assert(buf != NULL && nbytes > 0);
76
77 p = (struct fmemopen_cookie *)cookie;
78 if (p->cur >= p->tail)
79 return 0;
80 s = p->cur;
81 do {
82 if (p->cur == p->tail - 1) {
83 if (*buf == '\0') {
84 *p->cur++ = '\0';
85 goto ok;
86 }
87 break;
88 }
89 *p->cur++ = *buf++;
90 } while (--nbytes > 0);
91 *p->cur = '\0';
92ok:
93 if (p->cur > p->eob)
94 p->eob = p->cur;
95
96 return (int)(p->cur - s);
e5afb31f
VS
97}
98
c43830c0 99static fpos_t
395d17df 100fmemopen_seek(void *cookie, fpos_t offset, int whence)
e5afb31f 101{
395d17df
VS
102 struct fmemopen_cookie *p;
103
104 assert(cookie != NULL);
c43830c0 105
395d17df
VS
106 p = (struct fmemopen_cookie *)cookie;
107 switch (whence) {
108 case SEEK_SET:
e5afb31f 109 break;
395d17df
VS
110 case SEEK_CUR:
111 offset += p->cur - p->head;
e5afb31f 112 break;
395d17df
VS
113 case SEEK_END:
114 offset += p->eob - p->head;
e5afb31f 115 break;
395d17df
VS
116 default:
117 errno = EINVAL;
118 goto error;
119 }
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);
e5afb31f 123 }
395d17df
VS
124error:
125 return (fpos_t)-1;
126}
c43830c0 127
395d17df
VS
128static int
129fmemopen_close0(void *cookie)
130{
131 assert(cookie != NULL);
c43830c0 132
395d17df 133 free(cookie);
c43830c0 134
395d17df 135 return 0;
e5afb31f
VS
136}
137
c43830c0 138static int
395d17df 139fmemopen_close1(void *cookie)
e5afb31f 140{
395d17df 141 struct fmemopen_cookie *p;
4ad99553 142
395d17df 143 assert(cookie != NULL);
c43830c0 144
395d17df
VS
145 p = (struct fmemopen_cookie *)cookie;
146 free(p->head);
147 free(p);
c43830c0 148
395d17df 149 return 0;
e5afb31f
VS
150}
151
395d17df 152
c43830c0 153FILE *
395d17df 154fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
e5afb31f 155{
395d17df
VS
156 int flags, oflags;
157 FILE *fp;
158 struct fmemopen_cookie *cookie;
159
160 if (size < (size_t)1)
161 goto invalid;
c43830c0 162
395d17df
VS
163 flags = __sflags(mode, &oflags);
164 if (flags == 0)
e5afb31f 165 return NULL;
c43830c0 166
395d17df
VS
167 if ((oflags & O_RDWR) == 0 && buf == NULL)
168 goto invalid;
c43830c0 169
395d17df
VS
170 fp = __sfp();
171 if (fp == NULL)
172 return NULL;
173
174 cookie = malloc(sizeof(*cookie));
175 if (cookie == NULL)
176 goto release;
177
178 if (buf == NULL) {
179 cookie->head = malloc(size);
180 if (cookie->head == NULL) {
181 free(cookie);
182 goto release;
e5afb31f 183 }
395d17df
VS
184 *cookie->head = '\0';
185 fp->_close = &fmemopen_close1;
e5afb31f 186 } else {
395d17df
VS
187 cookie->head = (char *)buf;
188 if (oflags & O_TRUNC)
189 *cookie->head = '\0';
190 fp->_close = &fmemopen_close0;
e5afb31f 191 }
395d17df
VS
192
193 cookie->tail = cookie->head + size;
194 cookie->eob = cookie->head;
195 do {
196 if (*cookie->eob == '\0')
197 break;
198 ++cookie->eob;
199 } while (--size > 0);
200
201 cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
202
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;
208
209 return fp;
210
211invalid:
212 errno = EINVAL;
213 return NULL;
214
215release:
216 fp->pub._flags = 0;
217 return NULL;
e5afb31f 218}
395d17df 219