Home | History | Annotate | Line # | Download | only in stdio
fmemopen.c revision 1.7
      1 /* $NetBSD: fmemopen.c,v 1.7 2012/03/27 15:05:42 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c)2007, 2010 Takehiko NOZAKI,
      5  * All rights reserved.
      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:
     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.
     15  *
     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.
     27  *
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 #if defined(LIBC_SCCS) && !defined(lint)
     32 __RCSID("$NetBSD: fmemopen.c,v 1.7 2012/03/27 15:05:42 christos Exp $");
     33 #endif /* LIBC_SCCS and not lint */
     34 
     35 #include <assert.h>
     36 #include <errno.h>
     37 #include <fcntl.h>
     38 #include <stddef.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 
     42 #include "reentrant.h"
     43 #include "local.h"
     44 
     45 struct fmemopen_cookie {
     46 	char *head, *tail, *cur, *eob;
     47 };
     48 
     49 static ssize_t
     50 fmemopen_read(void *cookie, void *buf, size_t nbytes)
     51 {
     52 	struct fmemopen_cookie *p;
     53 	char *s, *b = buf;
     54 
     55 	_DIAGASSERT(cookie != NULL);
     56 	_DIAGASSERT(buf != NULL && nbytes > 0);
     57 
     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);
     65 
     66 	return (ssize_t)(p->cur - s);
     67 }
     68 
     69 static ssize_t
     70 fmemopen_write(void *cookie, const void *buf, size_t nbytes)
     71 {
     72 	struct fmemopen_cookie *p;
     73 	char *s;
     74 	const char *b = buf;
     75 
     76 	_DIAGASSERT(cookie != NULL);
     77 	_DIAGASSERT(buf != NULL && nbytes > 0);
     78 
     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;
     88 			}
     89 			break;
     90 		}
     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;
     97 
     98 	return (ssize_t)(p->cur - s);
     99 }
    100 
    101 static int
    102 fmemopen_flush(void *cookie)
    103 {
    104 	struct fmemopen_cookie *p;
    105 
    106 	_DIAGASSERT(cookie != NULL);
    107 
    108 	p = (struct fmemopen_cookie *)cookie;
    109 	if (p->cur >= p->tail)
    110 		return -1;
    111 	*p->cur = '\0';
    112 	return 0;
    113 }
    114 
    115 static off_t
    116 fmemopen_seek(void *cookie, off_t offset, int whence)
    117 {
    118 	struct fmemopen_cookie *p;
    119 
    120 	_DIAGASSERT(cookie != NULL);
    121 
    122 	p = (struct fmemopen_cookie *)cookie;
    123 	switch (whence) {
    124 	case SEEK_SET:
    125 		break;
    126 	case SEEK_CUR:
    127 		offset += p->cur - p->head;
    128 		break;
    129 	case SEEK_END:
    130 		offset += p->eob - p->head;
    131 		break;
    132 	default:
    133 		errno = EINVAL;
    134 		goto error;
    135 	}
    136 	if (offset >= (off_t)0 && offset <= p->tail - p->head) {
    137 		p->cur = p->head + (ptrdiff_t)offset;
    138 		return (off_t)(p->cur - p->head);
    139 	}
    140 error:
    141 	return (off_t)-1;
    142 }
    143 
    144 static int
    145 fmemopen_close0(void *cookie)
    146 {
    147 	_DIAGASSERT(cookie != NULL);
    148 
    149 	free(cookie);
    150 
    151 	return 0;
    152 }
    153 
    154 static int
    155 fmemopen_close1(void *cookie)
    156 {
    157 	struct fmemopen_cookie *p;
    158 
    159 	_DIAGASSERT(cookie != NULL);
    160 
    161 	p = (struct fmemopen_cookie *)cookie;
    162 	free(p->head);
    163 	free(p);
    164 
    165 	return 0;
    166 }
    167 
    168 
    169 FILE *
    170 fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
    171 {
    172 	int flags, oflags;
    173 	FILE *fp;
    174 	struct fmemopen_cookie *cookie;
    175 
    176 	if (size < (size_t)1)
    177 		goto invalid;
    178 
    179 	flags = __sflags(mode, &oflags);
    180 	if (flags == 0)
    181 		return NULL;
    182 
    183 	if ((oflags & O_RDWR) == 0 && buf == NULL)
    184 		goto invalid;
    185 
    186 	fp = __sfp();
    187 	if (fp == NULL)
    188 		return NULL;
    189 	fp->_file = -1;
    190 
    191 	cookie = malloc(sizeof(*cookie));
    192 	if (cookie == NULL)
    193 		goto release;
    194 
    195 	if (buf == NULL) {
    196 		cookie->head = malloc(size);
    197 		if (cookie->head == NULL) {
    198 			free(cookie);
    199 			goto release;
    200 		}
    201 		*cookie->head = '\0';
    202 		fp->_close = fmemopen_close1;
    203 	} else {
    204 		cookie->head = (char *)buf;
    205 		if (oflags & O_TRUNC)
    206 			*cookie->head = '\0';
    207 		fp->_close = fmemopen_close0;
    208 	}
    209 
    210 	cookie->tail = cookie->head + size;
    211 	cookie->eob  = cookie->head;
    212 	do {
    213 		if (*cookie->eob == '\0')
    214 			break;
    215 		++cookie->eob;
    216 	} while (--size > 0);
    217 
    218 	cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
    219 
    220 	fp->_flags  = flags;
    221 	fp->_write  = (flags & __SRD) ? NULL : fmemopen_write;
    222 	fp->_read   = (flags & __SWR) ? NULL : fmemopen_read;
    223 	fp->_seek   = fmemopen_seek;
    224 	fp->_flush  = fmemopen_flush;
    225 	fp->_cookie = (void *)cookie;
    226 
    227 	return fp;
    228 
    229 invalid:
    230 	errno = EINVAL;
    231 	return NULL;
    232 
    233 release:
    234 	fp->_flags = 0;
    235 	return NULL;
    236 }
    237