2 * Copyright (c) 2011 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Venkatesh Srinivas <me@endeavour.zapto.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * ----------------------------------------------------------------------------
36 * "THE BEER-WARE LICENSE" (Revision 42):
37 * <hiten@uk.FreeBSD.ORG> wrote this file. As long as you retain this notice
38 * you can do whatever you want with this stuff. If we meet some day, and you
39 * think this stuff is worth it, you can buy me a beer in return. Hiten Pandya.
40 * ----------------------------------------------------------------------------
42 * $FreeBSD: src/sys/dev/md/md.c,v 1.8.2.2 2002/08/19 17:43:34 jdp Exp $
46 * fmemopen -- Open a memory buffer stream
54 #include <sys/types.h>
57 static int __fmemopen_closefn (void *);
58 static int __fmemopen_readfn(void *, char *, int);
59 static fpos_t __fmemopen_seekfn (void *, fpos_t, int);
60 static int __fmemopen_writefn(void *, const char *, int);
62 struct fmemopen_cookie {
71 __fmemopen_readfn(void *cookie, char *buf, int len)
73 struct fmemopen_cookie *c;
75 c = (struct fmemopen_cookie *) cookie;
81 if ((c->pos + len) > c->size) {
82 if (c->pos == c->size)
84 len = c->size - c->pos;
87 memcpy(buf, &(c->buffer[c->pos]), len);
91 if (c->pos > c->maxpos)
98 __fmemopen_writefn (void *cookie, const char *buf, int len)
100 struct fmemopen_cookie *c;
103 c = (struct fmemopen_cookie *) cookie;
109 addnullc = ((len == 0) || (buf[len - 1] != '\0')) ? 1 : 0;
111 if ((c->pos + len + addnullc) > c->size) {
112 if ((c->pos + addnullc) == c->size)
114 len = c->size - c->pos - addnullc;
117 memcpy(&(c->buffer[c->pos]), buf, len);
120 if (c->pos > c->maxpos) {
123 c->buffer[c->maxpos] = '\0';
130 __fmemopen_seekfn(void *cookie, fpos_t pos, int whence)
133 struct fmemopen_cookie *c;
135 c = (struct fmemopen_cookie *) cookie;
149 if ((np < 0) || (np > c->size))
158 __fmemopen_closefn (void *cookie)
160 struct fmemopen_cookie *c;
162 c = (struct fmemopen_cookie*) cookie;
172 fmemopen(void *restrict buffer, size_t s, const char *restrict mode)
175 struct fmemopen_cookie *c;
177 c = malloc(sizeof (struct fmemopen_cookie));
181 c->mybuffer = (buffer == NULL);
184 c->buffer = malloc(s);
185 if (c->buffer == NULL) {
196 c->maxpos = strlen(c->buffer);
204 __fmemopen_readfn, /* string stream read */
205 __fmemopen_writefn, /* string stream write */
206 __fmemopen_seekfn, /* string stream seek */
207 __fmemopen_closefn /* string stream close */