Home | History | Annotate | Line # | Download | only in stdio
fmemopen.c revision 1.3
      1 /* $NetBSD: fmemopen.c,v 1.3 2010/09/25 14:00:30 tron 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.3 2010/09/25 14:00:30 tron 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 int
     50 fmemopen_read(void *cookie, char *buf, int nbytes)
     51 {
     52 	struct fmemopen_cookie *p;
     53 	char *s;
     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 		*buf++ = *p->cur++;
     64 	} while (--nbytes > 0);
     65 
     66 	return (int)(p->cur - s);
     67 }
     68 
     69 static int
     70 fmemopen_write(void *cookie, const char *buf, int nbytes)
     71 {
     72 	struct fmemopen_cookie *p;
     73 	char *s, *t;
     74 
     75 	_DIAGASSERT(cookie != NULL);
     76 	_DIAGASSERT(buf != NULL && nbytes > 0);
     77 
     78 	p = (struct fmemopen_cookie *)cookie;
     79 	if (p->cur >= p->tail)
     80 		return 0;
     81 	s = p->cur;
     82 	t = p->tail - 1;
     83 	do {
     84 		if (p->cur == t) {
     85 			if (*buf == '\0')
     86 				*p->cur++ = *buf++;
     87 			break;
     88 		}
     89 		*p->cur++ = *buf++;
     90 	} while (--nbytes > 0);
     91 	*p->cur = '\0';
     92 	if (p->cur > p->eob)
     93 		p->eob = p->cur;
     94 
     95 	return (int)(p->cur - s);
     96 }
     97 
     98 static fpos_t
     99 fmemopen_seek(void *cookie, fpos_t offset, int whence)
    100 {
    101 	struct fmemopen_cookie *p;
    102 
    103 	_DIAGASSERT(cookie != NULL);
    104 
    105 	p = (struct fmemopen_cookie *)cookie;
    106 	switch (whence) {
    107 	case SEEK_SET:
    108 		break;
    109 	case SEEK_CUR:
    110 		offset += p->cur - p->head;
    111 		break;
    112 	case SEEK_END:
    113 		offset += p->eob - p->head;
    114 		break;
    115 	default:
    116 		errno = EINVAL;
    117 		goto error;
    118 	}
    119 	if (offset >= (fpos_t)0 && offset <= p->tail - p->head) {
    120 		p->cur = p->head + (ptrdiff_t)offset;
    121 		return (fpos_t)(p->cur - p->head);
    122 	}
    123 error:
    124 	return (fpos_t)-1;
    125 }
    126 
    127 static int
    128 fmemopen_close0(void *cookie)
    129 {
    130 	_DIAGASSERT(cookie != NULL);
    131 
    132 	free(cookie);
    133 
    134 	return 0;
    135 }
    136 
    137 static int
    138 fmemopen_close1(void *cookie)
    139 {
    140 	struct fmemopen_cookie *p;
    141 
    142 	_DIAGASSERT(cookie != NULL);
    143 
    144 	p = (struct fmemopen_cookie *)cookie;
    145 	free(p->head);
    146 	free(p);
    147 
    148 	return 0;
    149 }
    150 
    151 
    152 FILE *
    153 fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
    154 {
    155 	int oflags;
    156 	FILE *fp;
    157 	struct fmemopen_cookie *cookie;
    158 
    159 	if (size < (size_t)1) {
    160 		errno = EINVAL;
    161 		return NULL;
    162 	}
    163 
    164 	fp = __sfp();
    165 	if (fp == NULL)
    166 		return NULL;
    167 	fp->_file = -1;
    168 
    169 	fp->_flags = __sflags(mode, &oflags);
    170 	if (fp->_flags == 0)
    171 		return NULL;
    172 
    173 	if ((oflags & O_RDWR) == 0 && buf == NULL) {
    174 		errno = EINVAL;
    175 		goto release;
    176 	}
    177 
    178 	cookie = malloc(sizeof(*cookie));
    179 	if (cookie == NULL)
    180 		goto release;
    181 
    182 	if (buf == NULL) {
    183 		cookie->head = malloc(size);
    184 		if (cookie->head == NULL) {
    185 			free(cookie);
    186 			goto release;
    187 		}
    188 		*cookie->head = '\0';
    189 		fp->_close = &fmemopen_close1;
    190 	} else {
    191 		cookie->head = (char *)buf;
    192 		if (oflags & O_TRUNC)
    193 			*cookie->head = '\0';
    194 		fp->_close = &fmemopen_close0;
    195 	}
    196 
    197 	cookie->tail = cookie->head + size;
    198 	cookie->eob  = cookie->head;
    199 	do {
    200 		if (*cookie->eob == '\0')
    201 			break;
    202 		++cookie->eob;
    203 	} while (--size > 0);
    204 
    205 	cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
    206 
    207 	fp->_write  = (fp->_flags & __SRD) ? NULL : &fmemopen_write;
    208 	fp->_read   = (fp->_flags & __SWR) ? NULL : &fmemopen_read;
    209 	fp->_seek   = &fmemopen_seek;
    210 	fp->_cookie = (void *)cookie;
    211 
    212 	return fp;
    213 
    214 release:
    215 	fp->_flags = 0;
    216 	return NULL;
    217 }
    218