libutil: add O_NOCTTY back to old pty open code
[minix.git] / lib / libc / stdio / fmemopen.c
blob33840e367b8c869af5d3189b7fb9f9b95336b6b3
1 /* $NetBSD: fmemopen.c,v 1.5 2010/09/27 17:08:29 tnozaki 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.5 2010/09/27 17:08:29 tnozaki 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 int
50 fmemopen_read(void *cookie, char *buf, int nbytes)
52 struct fmemopen_cookie *p;
53 char *s;
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 *buf++ = *p->cur++;
64 } while (--nbytes > 0);
66 return (int)(p->cur - s);
69 static int
70 fmemopen_write(void *cookie, const char *buf, int nbytes)
72 struct fmemopen_cookie *p;
73 char *s;
75 _DIAGASSERT(cookie != NULL);
76 _DIAGASSERT(buf != NULL && nbytes > 0);
78 p = (struct fmemopen_cookie *)cookie;
79 if (p->cur >= p->tail)
80 return 0;
81 s = p->cur;
82 do {
83 if (p->cur == p->tail - 1) {
84 if (*buf == '\0') {
85 *p->cur++ = '\0';
86 goto ok;
88 break;
90 *p->cur++ = *buf++;
91 } while (--nbytes > 0);
92 *p->cur = '\0';
93 ok:
94 if (p->cur > p->eob)
95 p->eob = p->cur;
97 return (int)(p->cur - s);
100 static fpos_t
101 fmemopen_seek(void *cookie, fpos_t offset, int whence)
103 struct fmemopen_cookie *p;
105 _DIAGASSERT(cookie != NULL);
107 p = (struct fmemopen_cookie *)cookie;
108 switch (whence) {
109 case SEEK_SET:
110 break;
111 case SEEK_CUR:
112 offset += p->cur - p->head;
113 break;
114 case SEEK_END:
115 offset += p->eob - p->head;
116 break;
117 default:
118 errno = EINVAL;
119 goto error;
121 if (offset >= (fpos_t)0 && offset <= p->tail - p->head) {
122 p->cur = p->head + (ptrdiff_t)offset;
123 return (fpos_t)(p->cur - p->head);
125 error:
126 return (fpos_t)-1;
129 static int
130 fmemopen_close0(void *cookie)
132 _DIAGASSERT(cookie != NULL);
134 free(cookie);
136 return 0;
139 static int
140 fmemopen_close1(void *cookie)
142 struct fmemopen_cookie *p;
144 _DIAGASSERT(cookie != NULL);
146 p = (struct fmemopen_cookie *)cookie;
147 free(p->head);
148 free(p);
150 return 0;
154 FILE *
155 fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
157 int flags, oflags;
158 FILE *fp;
159 struct fmemopen_cookie *cookie;
161 if (size < (size_t)1)
162 goto invalid;
164 flags = __sflags(mode, &oflags);
165 if (flags == 0)
166 return NULL;
168 if ((oflags & O_RDWR) == 0 && buf == NULL)
169 goto invalid;
171 fp = __sfp();
172 if (fp == NULL)
173 return NULL;
174 fp->_file = -1;
176 cookie = malloc(sizeof(*cookie));
177 if (cookie == NULL)
178 goto release;
180 if (buf == NULL) {
181 cookie->head = malloc(size);
182 if (cookie->head == NULL) {
183 free(cookie);
184 goto release;
186 *cookie->head = '\0';
187 fp->_close = &fmemopen_close1;
188 } else {
189 cookie->head = (char *)buf;
190 if (oflags & O_TRUNC)
191 *cookie->head = '\0';
192 fp->_close = &fmemopen_close0;
195 cookie->tail = cookie->head + size;
196 cookie->eob = cookie->head;
197 do {
198 if (*cookie->eob == '\0')
199 break;
200 ++cookie->eob;
201 } while (--size > 0);
203 cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
205 fp->_flags = flags;
206 fp->_write = (flags & __SRD) ? NULL : &fmemopen_write;
207 fp->_read = (flags & __SWR) ? NULL : &fmemopen_read;
208 fp->_seek = &fmemopen_seek;
209 fp->_cookie = (void *)cookie;
211 return fp;
213 invalid:
214 errno = EINVAL;
215 return NULL;
217 release:
218 fp->_flags = 0;
219 return NULL;