Remove building with NOCRYPTO option
[minix.git] / lib / libc / stdio / fmemopen.c
blob474c798ada789fb7215c896a76cb57f0aa7db523
1 /* $NetBSD: fmemopen.c,v 1.8 2012/03/29 14:27:33 christos Exp $ */
3 /*-
4 * Copyright (c)2007, 2010 Takehiko NOZAKI,
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 #if defined(LIBC_SCCS) && !defined(lint)
32 __RCSID("$NetBSD: fmemopen.c,v 1.8 2012/03/29 14:27:33 christos Exp $");
33 #endif /* LIBC_SCCS and not lint */
35 #include <assert.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
42 #include "reentrant.h"
43 #include "local.h"
45 struct fmemopen_cookie {
46 char *head, *tail, *cur, *eob;
49 static ssize_t
50 fmemopen_read(void *cookie, void *buf, size_t nbytes)
52 struct fmemopen_cookie *p;
53 char *s, *b = buf;
55 _DIAGASSERT(cookie != NULL);
56 _DIAGASSERT(buf != NULL && nbytes > 0);
58 p = (struct fmemopen_cookie *)cookie;
59 s = p->cur;
60 do {
61 if (p->cur == p->tail)
62 break;
63 *b++ = *p->cur++;
64 } while (--nbytes > 0);
66 return (ssize_t)(p->cur - s);
69 static ssize_t
70 fmemopen_write(void *cookie, const void *buf, size_t nbytes)
72 struct fmemopen_cookie *p;
73 char *s;
74 const char *b = buf;
76 _DIAGASSERT(cookie != NULL);
77 _DIAGASSERT(buf != NULL && nbytes > 0);
79 p = (struct fmemopen_cookie *)cookie;
80 if (p->cur >= p->tail)
81 return 0;
82 s = p->cur;
83 do {
84 if (p->cur == p->tail - 1) {
85 if (*b == '\0') {
86 *p->cur++ = '\0';
87 goto ok;
89 break;
91 *p->cur++ = *b++;
92 } while (--nbytes > 0);
93 *p->cur = '\0';
94 ok:
95 if (p->cur > p->eob)
96 p->eob = p->cur;
98 return (ssize_t)(p->cur - s);
101 #ifdef notyet
102 static int
103 fmemopen_flush(void *cookie)
105 struct fmemopen_cookie *p;
107 _DIAGASSERT(cookie != NULL);
109 p = (struct fmemopen_cookie *)cookie;
110 if (p->cur >= p->tail)
111 return -1;
112 *p->cur = '\0';
113 return 0;
115 #endif
117 static off_t
118 fmemopen_seek(void *cookie, off_t offset, int whence)
120 struct fmemopen_cookie *p;
122 _DIAGASSERT(cookie != NULL);
124 p = (struct fmemopen_cookie *)cookie;
125 switch (whence) {
126 case SEEK_SET:
127 break;
128 case SEEK_CUR:
129 offset += p->cur - p->head;
130 break;
131 case SEEK_END:
132 offset += p->eob - p->head;
133 break;
134 default:
135 errno = EINVAL;
136 goto error;
138 if (offset >= (off_t)0 && offset <= p->tail - p->head) {
139 p->cur = p->head + (ptrdiff_t)offset;
140 return (off_t)(p->cur - p->head);
142 error:
143 return (off_t)-1;
146 static int
147 fmemopen_close0(void *cookie)
149 _DIAGASSERT(cookie != NULL);
151 free(cookie);
153 return 0;
156 static int
157 fmemopen_close1(void *cookie)
159 struct fmemopen_cookie *p;
161 _DIAGASSERT(cookie != NULL);
163 p = (struct fmemopen_cookie *)cookie;
164 free(p->head);
165 free(p);
167 return 0;
171 FILE *
172 fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
174 int flags, oflags;
175 FILE *fp;
176 struct fmemopen_cookie *cookie;
178 if (size < (size_t)1)
179 goto invalid;
181 flags = __sflags(mode, &oflags);
182 if (flags == 0)
183 return NULL;
185 if ((oflags & O_RDWR) == 0 && buf == NULL)
186 goto invalid;
188 fp = __sfp();
189 if (fp == NULL)
190 return NULL;
191 fp->_file = -1;
193 cookie = malloc(sizeof(*cookie));
194 if (cookie == NULL)
195 goto release;
197 if (buf == NULL) {
198 cookie->head = malloc(size);
199 if (cookie->head == NULL) {
200 free(cookie);
201 goto release;
203 *cookie->head = '\0';
204 fp->_close = fmemopen_close1;
205 } else {
206 cookie->head = (char *)buf;
207 if (oflags & O_TRUNC)
208 *cookie->head = '\0';
209 fp->_close = fmemopen_close0;
212 cookie->tail = cookie->head + size;
213 cookie->eob = cookie->head;
214 do {
215 if (*cookie->eob == '\0')
216 break;
217 ++cookie->eob;
218 } while (--size > 0);
220 cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
222 fp->_flags = flags;
223 fp->_write = (flags & __SRD) ? NULL : fmemopen_write;
224 fp->_read = (flags & __SWR) ? NULL : fmemopen_read;
225 fp->_seek = fmemopen_seek;
226 #ifdef notyet
227 fp->_flush = fmemopen_flush;
228 #endif
229 fp->_cookie = (void *)cookie;
231 return fp;
233 invalid:
234 errno = EINVAL;
235 return NULL;
237 release:
238 fp->_flags = 0;
239 return NULL;