]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio/memstream.c
Update to LGPL v2.1.
[thirdparty/glibc.git] / stdio / memstream.c
CommitLineData
c84142e8
UD
1/* Copyright (C) 1991, 92, 94, 95, 96, 97 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
c84142e8
UD
8
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
41bdb6e2 12 Lesser General Public License for more details.
c84142e8 13
41bdb6e2
AJ
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
28f540f4 18
28f540f4
RM
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
60092701 22#include <errno.h>
28f540f4
RM
23
24struct memstream_info
25 {
26 char **buffer;
27 size_t *bufsize;
28 };
29
30/* Enlarge STREAM's buffer. */
31static void
ebe3b3eb 32enlarge_buffer (register FILE *stream, int c)
28f540f4
RM
33{
34 struct memstream_info *info = (struct memstream_info *) stream->__cookie;
35 size_t need;
36
37 if (stream->__put_limit != stream->__buffer)
38 /* Record how much has actually been written into the buffer. */
39 *info->bufsize = stream->__bufp - stream->__buffer;
40
41 if (stream->__target != -1
9a0a462c 42 && (size_t) stream->__target > *info->bufsize)
28f540f4
RM
43 /* Our target (where the buffer maps to) is always zero except when
44 the user just did a SEEK_END fseek. If he sought within the
45 buffer, we need do nothing and will zero the target below. If he
46 sought past the end of the object, grow and zero-fill the buffer
47 up to the target address. */
48 need = stream->__target;
49 else
50 need = *info->bufsize;
51
52 /* We always need an extra character in the buffer. Either we are
53 writing C, or we are flushing and need to write a NUL terminator. */
54 ++need;
55
56 if (stream->__bufsize < need)
57 {
58 /* Enlarge the buffer. */
59 char *newbuf;
60 size_t newsize;
61 if (stream->__bufsize * 2 < need)
62 newsize = need;
63 else
64 newsize = stream->__bufsize * 2;
c4029823 65 newbuf = (char *) realloc ((void *) stream->__buffer, newsize);
28f540f4
RM
66 if (newbuf == NULL)
67 {
68 stream->__error = 1;
69 return;
70 }
71 *info->buffer = stream->__buffer = newbuf;
72 stream->__bufsize = newsize;
73 }
74
75 stream->__target = stream->__offset = 0;
76 stream->__get_limit = stream->__bufp = stream->__buffer + *info->bufsize;
77 stream->__put_limit = stream->__buffer + stream->__bufsize;
78
79 need -= stream->__bufp - stream->__buffer + 1;
80 if (need > 0)
81 {
82 /* We are extending the buffer after an fseek; zero-fill new space. */
c4029823 83 memset (stream->__bufp, '\0', need);
28f540f4
RM
84 stream->__bufp += need;
85 }
86
87 if (c != EOF)
88 *stream->__bufp++ = (unsigned char) c;
89 else
90 *stream->__bufp = '\0';
91}
92
93/* Seek function for memstreams.
94 There is no external state to munge. */
95
96static int
ebe3b3eb 97seek (void *cookie, fpos_t *pos, int whence)
28f540f4
RM
98{
99 switch (whence)
100 {
101 case SEEK_SET:
102 case SEEK_CUR:
103 return 0;
104
105 case SEEK_END:
106 /* Return the position relative to the end of the object.
107 fseek has just flushed us, so the info is consistent. */
108 *pos += *((struct memstream_info *) cookie)->bufsize;
109 return 0;
110
111 default:
112 __libc_fatal ("memstream::seek called with bogus WHENCE\n");
113 return -1;
114 }
115}
116
117static int
ebe3b3eb 118free_info (void *cookie)
28f540f4
RM
119{
120#if 0
121 struct memstream_info *info = (struct memstream_info *) cookie;
122 char *buf;
123
124 buf = (char *) realloc ((PTR) *info->buffer, *info->bufsize);
125 if (buf != NULL)
126 *info->buffer = buf;
127#endif
128
129 free (cookie);
130
131 return 0;
132}
133\f
134/* Open a stream that writes into a malloc'd buffer that is expanded as
135 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
136 and the number of characters written on fflush or fclose. */
137FILE *
c4029823
UD
138open_memstream (bufloc, sizeloc)
139 char **bufloc;
140 size_t *sizeloc;
28f540f4
RM
141{
142 FILE *stream;
143 struct memstream_info *info;
144
145 if (bufloc == NULL || sizeloc == NULL)
146 {
c4029823 147 __set_errno (EINVAL);
28f540f4
RM
148 return NULL;
149 }
150
151 stream = fmemopen ((char *) NULL, BUFSIZ, "w+");
152 if (stream == NULL)
153 return NULL;
154
155 info = (struct memstream_info *) malloc (sizeof (struct memstream_info));
156 if (info == NULL)
157 {
158 int save = errno;
159 (void) fclose (stream);
c4029823 160 __set_errno (save);
28f540f4
RM
161 return NULL;
162 }
163
164 stream->__room_funcs.__output = enlarge_buffer;
165 stream->__io_funcs.__seek = seek;
166 stream->__io_funcs.__close = free_info;
c4029823 167 stream->__cookie = (void *) info;
28f540f4
RM
168 stream->__userbuf = 1;
169
170 info->buffer = bufloc;
171 info->bufsize = sizeloc;
172
173 *bufloc = stream->__buffer;
174
175 return stream;
176}