Update.
[glibc.git] / libio / fmemopen.c
blob3afc04929cd62983cf078cb8f4fb527f1532707b
1 /* Fmemopen implementation.
2 Copyright (C) 2000 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.
30 * No, it isn't really tested yet. :-)
34 * I already see some potential problems:
36 * - I never used the "original" fmemopen(). I am sure that "my"
37 * fmemopen() behaves differently than the original version.
39 * - The documentation doesn't say wether a string stream allows
40 * seeks. I checked the old fmemopen implementation in glibc's stdio
41 * directory, wasn't quite able to see what is going on in that
42 * source, but as far as I understand there was no seek there. For
43 * my application, I needed fseek() and ftell(), so it's here.
45 * - "append" mode and fseek(p, SEEK_END) have two different ideas
46 * about the "end" of the stream.
48 * As described in the documentation, when opening the file in
49 * "append" mode, the position pointer will be set to the first null
50 * character of the string buffer (yet the buffer may already
51 * contain more data). For fseek(), the last byte of the buffer is
52 * used as the end of the stream.
54 * - It is unclear to me what the documentation tries to say when it
55 * explains what happens when you use fmemopen with a NULL
56 * buffer.
58 * Quote: "fmemopen [then] allocates an array SIZE bytes long. This
59 * is really only useful if you are going to write things to the
60 * buffer and then read them back in again."
62 * What does that mean if the original fmemopen() did not allow
63 * seeking? How do you read what you just wrote without seeking back
64 * to the beginning of the stream?
66 * - I think there should be a second version of fmemopen() that does
67 * not add null characters for each write. (At least in my
68 * application, I am not actually using strings but binary data and
69 * so I don't need the stream to add null characters on its own.)
72 #include <errno.h>
73 #include <libio.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <sys/types.h>
79 typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
80 struct fmemopen_cookie_struct
82 char *buffer;
83 int mybuffer;
84 size_t size;
85 _IO_off64_t pos;
86 size_t maxpos;
90 static ssize_t
91 fmemopen_read (void *cookie, char *b, size_t s)
93 fmemopen_cookie_t *c;
95 c = (fmemopen_cookie_t *) cookie;
97 if (c->pos + s > c->size)
99 if (c->pos == c->size)
100 return 0;
101 s = c->size - c->pos;
104 memcpy (b, &(c->buffer[c->pos]), s);
106 c->pos += s;
107 if (c->pos > c->maxpos)
108 c->maxpos = c->pos;
110 return s;
114 static ssize_t
115 fmemopen_write (void *cookie, const char *b, size_t s)
117 fmemopen_cookie_t *c;
118 int addnullc;
120 c = (fmemopen_cookie_t *) cookie;
122 addnullc = s == 0 || b[s - 1] != '\0';
124 if (c->pos + s + addnullc > c->size)
126 if (c->pos + addnullc == c->size)
128 __set_errno (ENOSPC);
129 return -1;
131 s = c->size - c->pos - addnullc;
134 memcpy (&(c->buffer[c->pos]), b, s);
136 c->pos += s;
137 if (c->pos > c->maxpos)
139 c->maxpos = c->pos;
140 if (addnullc)
141 c->buffer[c->maxpos] = '\0';
144 return s;
148 static int
149 fmemopen_seek (void *cookie, _IO_off64_t *p, int w)
151 _IO_off64_t np;
152 fmemopen_cookie_t *c;
154 c = (fmemopen_cookie_t *) cookie;
156 switch (w)
158 case SEEK_SET:
159 np = *p;
160 break;
162 case SEEK_CUR:
163 np = c->pos + *p;
164 break;
166 case SEEK_END:
167 np = c->size - *p;
168 break;
170 default:
171 return -1;
174 if (np < 0 || np > c->size)
175 return -1;
177 c->pos = np;
179 return np;
183 static int
184 fmemopen_close (void *cookie)
186 fmemopen_cookie_t *c;
188 c = (fmemopen_cookie_t *) cookie;
190 if (c->mybuffer)
191 free (c->buffer);
192 free (c);
194 return 0;
198 FILE *
199 fmemopen (void *buf, size_t len, const char *mode)
201 cookie_io_functions_t iof;
202 fmemopen_cookie_t *c;
204 c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
205 if (c == NULL)
206 return NULL;
208 c->mybuffer = (buf == NULL);
210 if (c->mybuffer)
212 c->buffer = (char *) malloc (len);
213 if (c->buffer == NULL)
215 free (c);
216 return NULL;
218 c->buffer[0] = '\0';
220 else
221 c->buffer = buf;
223 c->size = len;
225 if (mode[0] == 'w')
226 c->buffer[0] = '\0';
228 c->maxpos = strlen (c->buffer);
230 if (mode[0] == 'a')
231 c->pos = c->maxpos;
232 else
233 c->pos = 0;
235 iof.read = fmemopen_read;
236 iof.write = fmemopen_write;
237 iof.seek = fmemopen_seek;
238 iof.close = fmemopen_close;
240 return fopencookie (c, mode, iof);