1 /* Fmemopen implementation.
2 Copyright (C) 2000-2015 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, see
18 <http://www.gnu.org/licenses/>. */
21 * fmemopen() - "my" version of a string stream
22 * Hanno Mueller, kontakt@hanno.de
25 * I needed fmemopen() for an application that I currently work on,
26 * but couldn't find it in libio. The following snippet of code is an
27 * attempt to implement what glibc's documentation describes.
31 * I already see some potential problems:
33 * - I never used the "original" fmemopen(). I am sure that "my"
34 * fmemopen() behaves differently than the original version.
36 * - The documentation doesn't say wether a string stream allows
37 * seeks. I checked the old fmemopen implementation in glibc's stdio
38 * directory, wasn't quite able to see what is going on in that
39 * source, but as far as I understand there was no seek there. For
40 * my application, I needed fseek() and ftell(), so it's here.
42 * - "append" mode and fseek(p, SEEK_END) have two different ideas
43 * about the "end" of the stream.
45 * As described in the documentation, when opening the file in
46 * "append" mode, the position pointer will be set to the first null
47 * character of the string buffer (yet the buffer may already
48 * contain more data). For fseek(), the last byte of the buffer is
49 * used as the end of the stream.
51 * - It is unclear to me what the documentation tries to say when it
52 * explains what happens when you use fmemopen with a NULL
55 * Quote: "fmemopen [then] allocates an array SIZE bytes long. This
56 * is really only useful if you are going to write things to the
57 * buffer and then read them back in again."
59 * What does that mean if the original fmemopen() did not allow
60 * seeking? How do you read what you just wrote without seeking back
61 * to the beginning of the stream?
63 * - I think there should be a second version of fmemopen() that does
64 * not add null characters for each write. (At least in my
65 * application, I am not actually using strings but binary data and
66 * so I don't need the stream to add null characters on its own.)
75 #include <sys/types.h>
79 typedef struct fmemopen_cookie_struct fmemopen_cookie_t
;
80 struct fmemopen_cookie_struct
92 fmemopen_read (void *cookie
, char *b
, size_t s
)
96 c
= (fmemopen_cookie_t
*) cookie
;
98 if (c
->pos
+ s
> c
->size
)
100 if ((size_t) c
->pos
== c
->size
)
102 s
= c
->size
- c
->pos
;
105 memcpy (b
, &(c
->buffer
[c
->pos
]), s
);
108 if ((size_t) c
->pos
> c
->maxpos
)
116 fmemopen_write (void *cookie
, const char *b
, size_t s
)
118 fmemopen_cookie_t
*c
;
121 c
= (fmemopen_cookie_t
*) cookie
;
123 addnullc
= c
->binmode
== 0 && (s
== 0 || b
[s
- 1] != '\0');
125 if (c
->pos
+ s
+ addnullc
> c
->size
)
127 if ((size_t) (c
->pos
+ addnullc
) == c
->size
)
129 __set_errno (ENOSPC
);
132 s
= c
->size
- c
->pos
- addnullc
;
135 memcpy (&(c
->buffer
[c
->pos
]), b
, s
);
138 if ((size_t) c
->pos
> c
->maxpos
)
142 c
->buffer
[c
->maxpos
] = '\0';
150 fmemopen_seek (void *cookie
, _IO_off64_t
*p
, int w
)
153 fmemopen_cookie_t
*c
;
155 c
= (fmemopen_cookie_t
*) cookie
;
168 np
= (c
->binmode
? c
->size
: c
->maxpos
) - *p
;
175 if (np
< 0 || (size_t) np
> c
->size
)
185 fmemopen_close (void *cookie
)
187 fmemopen_cookie_t
*c
;
189 c
= (fmemopen_cookie_t
*) cookie
;
200 fmemopen (void *buf
, size_t len
, const char *mode
)
202 cookie_io_functions_t iof
;
203 fmemopen_cookie_t
*c
;
205 if (__glibc_unlikely (len
== 0))
208 __set_errno (EINVAL
);
212 c
= (fmemopen_cookie_t
*) malloc (sizeof (fmemopen_cookie_t
));
216 c
->mybuffer
= (buf
== NULL
);
220 c
->buffer
= (char *) malloc (len
);
221 if (c
->buffer
== NULL
)
231 if (__glibc_unlikely ((uintptr_t) len
> -(uintptr_t) buf
))
242 c
->maxpos
= strnlen (c
->buffer
, len
);
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
);
261 libc_hidden_def (fmemopen
)