2.9
[glibc/nacl-glibc.git] / libio / fmemopen.c
blobb618ce585d95412c555efd2c35c3f04a4c6545e3
1 /* Fmemopen implementation.
2 Copyright (C) 2000, 2002, 2005, 2006, 2008 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Hanno Mueller, kontakt@hanno.de, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
22 * fmemopen() - "my" version of a string stream
23 * Hanno Mueller, kontakt@hanno.de
26 * I needed fmemopen() for an application that I currently work on,
27 * but couldn't find it in libio. The following snippet of code is an
28 * attempt to implement what glibc's documentation describes.
32 * I already see some potential problems:
34 * - I never used the "original" fmemopen(). I am sure that "my"
35 * fmemopen() behaves differently than the original version.
37 * - The documentation doesn't say wether a string stream allows
38 * seeks. I checked the old fmemopen implementation in glibc's stdio
39 * directory, wasn't quite able to see what is going on in that
40 * source, but as far as I understand there was no seek there. For
41 * my application, I needed fseek() and ftell(), so it's here.
43 * - "append" mode and fseek(p, SEEK_END) have two different ideas
44 * about the "end" of the stream.
46 * As described in the documentation, when opening the file in
47 * "append" mode, the position pointer will be set to the first null
48 * character of the string buffer (yet the buffer may already
49 * contain more data). For fseek(), the last byte of the buffer is
50 * used as the end of the stream.
52 * - It is unclear to me what the documentation tries to say when it
53 * explains what happens when you use fmemopen with a NULL
54 * buffer.
56 * Quote: "fmemopen [then] allocates an array SIZE bytes long. This
57 * is really only useful if you are going to write things to the
58 * buffer and then read them back in again."
60 * What does that mean if the original fmemopen() did not allow
61 * seeking? How do you read what you just wrote without seeking back
62 * to the beginning of the stream?
64 * - I think there should be a second version of fmemopen() that does
65 * not add null characters for each write. (At least in my
66 * application, I am not actually using strings but binary data and
67 * so I don't need the stream to add null characters on its own.)
70 #include <errno.h>
71 #include <libio.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <stdint.h>
75 #include <string.h>
76 #include <sys/types.h>
77 #include "libioP.h"
80 typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
81 struct fmemopen_cookie_struct
83 char *buffer;
84 int mybuffer;
85 int binmode;
86 size_t size;
87 _IO_off64_t pos;
88 size_t maxpos;
92 static ssize_t
93 fmemopen_read (void *cookie, char *b, size_t s)
95 fmemopen_cookie_t *c;
97 c = (fmemopen_cookie_t *) cookie;
99 if (c->pos + s > c->size)
101 if ((size_t) c->pos == c->size)
102 return 0;
103 s = c->size - c->pos;
106 memcpy (b, &(c->buffer[c->pos]), s);
108 c->pos += s;
109 if ((size_t) c->pos > c->maxpos)
110 c->maxpos = c->pos;
112 return s;
116 static ssize_t
117 fmemopen_write (void *cookie, const char *b, size_t s)
119 fmemopen_cookie_t *c;
120 int addnullc;
122 c = (fmemopen_cookie_t *) cookie;
124 addnullc = c->binmode == 0 && (s == 0 || b[s - 1] != '\0');
126 if (c->pos + s + addnullc > c->size)
128 if ((size_t) (c->pos + addnullc) == c->size)
130 __set_errno (ENOSPC);
131 return 0;
133 s = c->size - c->pos - addnullc;
136 memcpy (&(c->buffer[c->pos]), b, s);
138 c->pos += s;
139 if ((size_t) c->pos > c->maxpos)
141 c->maxpos = c->pos;
142 if (addnullc)
143 c->buffer[c->maxpos] = '\0';
146 return s;
150 static int
151 fmemopen_seek (void *cookie, _IO_off64_t *p, int w)
153 _IO_off64_t np;
154 fmemopen_cookie_t *c;
156 c = (fmemopen_cookie_t *) cookie;
158 switch (w)
160 case SEEK_SET:
161 np = *p;
162 break;
164 case SEEK_CUR:
165 np = c->pos + *p;
166 break;
168 case SEEK_END:
169 np = (c->binmode ? c->size : c->maxpos) - *p;
170 break;
172 default:
173 return -1;
176 if (np < 0 || (size_t) np > c->size)
177 return -1;
179 *p = c->pos = np;
181 return 0;
185 static int
186 fmemopen_close (void *cookie)
188 fmemopen_cookie_t *c;
190 c = (fmemopen_cookie_t *) cookie;
192 if (c->mybuffer)
193 free (c->buffer);
194 free (c);
196 return 0;
200 FILE *
201 fmemopen (void *buf, size_t len, const char *mode)
203 cookie_io_functions_t iof;
204 fmemopen_cookie_t *c;
206 if (__builtin_expect (len == 0, 0))
208 einval:
209 __set_errno (EINVAL);
210 return NULL;
213 c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
214 if (c == NULL)
215 return NULL;
217 c->mybuffer = (buf == NULL);
219 if (c->mybuffer)
221 c->buffer = (char *) malloc (len);
222 if (c->buffer == NULL)
224 free (c);
225 return NULL;
227 c->buffer[0] = '\0';
229 else
231 if (__builtin_expect ((uintptr_t) len > -(uintptr_t) buf, 0))
233 free (c);
234 goto einval;
237 c->buffer = buf;
240 c->size = len;
242 if (mode[0] == 'w')
243 c->buffer[0] = '\0';
245 c->maxpos = strlen (c->buffer);
247 if (mode[0] == 'a')
248 c->pos = c->maxpos;
249 else
250 c->pos = 0;
252 c->binmode = mode[0] != '\0' && mode[1] == 'b';
254 iof.read = fmemopen_read;
255 iof.write = fmemopen_write;
256 iof.seek = fmemopen_seek;
257 iof.close = fmemopen_close;
259 return _IO_fopencookie (c, mode, iof);