* Make-dist (subdir): Make empty value really empty.
[glibc.git] / stdio / memstream.c
blob1a8b35081d8bd6611de37bba375ed14e9bd48f0d
1 /* Copyright (C) 1991, 1992, 1994, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
25 struct memstream_info
27 char **buffer;
28 size_t *bufsize;
31 /* Enlarge STREAM's buffer. */
32 static void
33 DEFUN(enlarge_buffer, (stream, c),
34 register FILE *stream AND int c)
36 struct memstream_info *info = (struct memstream_info *) stream->__cookie;
37 size_t need;
39 if (stream->__put_limit != stream->__buffer)
40 /* Record how much has actually been written into the buffer. */
41 *info->bufsize = stream->__bufp - stream->__buffer;
43 if (stream->__target != -1
44 && stream->__target > *info->bufsize)
45 /* Our target (where the buffer maps to) is always zero except when
46 the user just did a SEEK_END fseek. If he sought within the
47 buffer, we need do nothing and will zero the target below. If he
48 sought past the end of the object, grow and zero-fill the buffer
49 up to the target address. */
50 need = stream->__target;
51 else
52 need = *info->bufsize;
54 /* We always need an extra character in the buffer. Either we are
55 writing C, or we are flushing and need to write a NUL terminator. */
56 ++need;
58 if (stream->__bufsize < need)
60 /* Enlarge the buffer. */
61 char *newbuf;
62 size_t newsize;
63 if (stream->__bufsize * 2 < need)
64 newsize = need;
65 else
66 newsize = stream->__bufsize * 2;
67 newbuf = (char *) realloc ((PTR) stream->__buffer, newsize);
68 if (newbuf == NULL)
70 stream->__error = 1;
71 return;
73 *info->buffer = stream->__buffer = newbuf;
74 stream->__bufsize = newsize;
77 stream->__target = stream->__offset = 0;
78 stream->__get_limit = stream->__bufp = stream->__buffer + *info->bufsize;
79 stream->__put_limit = stream->__buffer + stream->__bufsize;
81 need -= stream->__bufp - stream->__buffer + 1;
82 if (need > 0)
84 /* We are extending the buffer after an fseek; zero-fill new space. */
85 bzero (stream->__bufp, need);
86 stream->__bufp += need;
89 if (c != EOF)
90 *stream->__bufp++ = (unsigned char) c;
91 else
92 *stream->__bufp = '\0';
95 /* Seek function for memstreams.
96 There is no external state to munge. */
98 static int
99 DEFUN(seek, (cookie, pos, whence),
100 PTR cookie AND fpos_t *pos AND int whence)
102 switch (whence)
104 case SEEK_SET:
105 case SEEK_CUR:
106 return 0;
108 case SEEK_END:
109 /* Return the position relative to the end of the object.
110 fseek has just flushed us, so the info is consistent. */
111 *pos += *((struct memstream_info *) cookie)->bufsize;
112 return 0;
114 default:
115 __libc_fatal ("memstream::seek called with bogus WHENCE\n");
116 return -1;
120 static int
121 DEFUN(free_info, (cookie), PTR cookie)
123 #if 0
124 struct memstream_info *info = (struct memstream_info *) cookie;
125 char *buf;
127 buf = (char *) realloc ((PTR) *info->buffer, *info->bufsize);
128 if (buf != NULL)
129 *info->buffer = buf;
130 #endif
132 free (cookie);
134 return 0;
137 /* Open a stream that writes into a malloc'd buffer that is expanded as
138 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
139 and the number of characters written on fflush or fclose. */
140 FILE *
141 DEFUN(open_memstream, (bufloc, sizeloc),
142 char **bufloc AND size_t *sizeloc)
144 FILE *stream;
145 struct memstream_info *info;
147 if (bufloc == NULL || sizeloc == NULL)
149 errno = EINVAL;
150 return NULL;
153 stream = fmemopen ((char *) NULL, BUFSIZ, "w+");
154 if (stream == NULL)
155 return NULL;
157 info = (struct memstream_info *) malloc (sizeof (struct memstream_info));
158 if (info == NULL)
160 int save = errno;
161 (void) fclose (stream);
162 errno = save;
163 return NULL;
166 stream->__room_funcs.__output = enlarge_buffer;
167 stream->__io_funcs.__seek = seek;
168 stream->__io_funcs.__close = free_info;
169 stream->__cookie = (PTR) info;
170 stream->__userbuf = 1;
172 info->buffer = bufloc;
173 info->bufsize = sizeloc;
175 *bufloc = stream->__buffer;
177 return stream;