MIPS64 dependent RTLD.
[glibc/history.git] / stdio / obstream.c
blobf9384bd59749ce923f801a6a9f5deb675543afa8
1 /* Copyright (C) 1992, 1996, 1997 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <stdio.h>
20 #include <obstack.h>
21 #include <stdarg.h>
22 #include <string.h>
24 /* Output-room function for obstack streams. */
26 static void
27 grow (stream, c)
28 FILE *stream;
29 int c;
31 struct obstack *const obstack = (struct obstack *) stream->__cookie;
33 /* Move the end of the object back to include only the portion
34 of the buffer which the user has already written into. */
35 obstack_blank_fast (obstack, - (stream->__put_limit - stream->__bufp));
37 if (stream->__target > obstack_object_size (obstack))
39 /* Our target (where the buffer maps to) is always zero except when
40 the user just did a SEEK_END fseek. If he sought within the
41 buffer, we need do nothing and will zero the target below. If he
42 sought past the end of the object, grow and zero-fill the object
43 up to the target address. */
45 obstack_blank (obstack,
46 stream->__target - obstack_object_size (obstack));
47 /* fseek has just flushed us, so the put limit points
48 to the end of the written data. */
49 bzero (stream->__put_limit,
50 stream->__target - stream->__bufsize);
53 if (c != EOF)
54 obstack_1grow (obstack, (unsigned char) c);
56 /* The stream buffer always maps exactly to the object on the top
57 of the obstack. The start of the buffer is the start of the object.
58 The put limit points just past the end of the object. On fflush, the
59 obstack is sync'd so the end of the object points just past the last
60 character written to the stream. */
62 stream->__target = stream->__offset = 0;
63 stream->__buffer = obstack_base (obstack);
64 stream->__bufsize = obstack_room (obstack);
65 stream->__bufp = obstack_next_free (obstack);
66 stream->__get_limit = stream->__bufp;
68 if (c == EOF)
69 /* This is fflush. Make the stream buffer, the object,
70 and the characters actually written all match. */
71 stream->__put_limit = stream->__get_limit;
72 else
74 /* Extend the buffer (and the object) to include
75 the rest of the obstack chunk (which is uninitialized).
76 Data past bufp is undefined. */
77 stream->__put_limit = stream->__buffer + stream->__bufsize;
78 obstack_blank_fast (obstack, stream->__put_limit - stream->__bufp);
82 /* Seek function for obstack streams.
83 There is no external state to munge. */
85 static int
86 seek (cookie, pos, whence)
87 void *cookie;
88 fpos_t *pos;
89 int whence;
91 switch (whence)
93 case SEEK_SET:
94 case SEEK_CUR:
95 return 0;
97 case SEEK_END:
98 /* Return the position relative to the end of the object.
99 fseek has just flushed us, so the obstack is consistent. */
100 *pos += obstack_object_size ((struct obstack *) cookie);
101 return 0;
103 default:
104 __libc_fatal ("obstream::seek called with bogus WHENCE\n");
105 return -1;
109 /* Input room function for obstack streams.
110 Only what has been written to the stream can be read back. */
112 static int
113 input (stream)
114 FILE *stream;
116 /* Re-sync with the obstack, growing the object if necessary. */
117 grow (stream, EOF);
119 if (stream->__bufp < stream->__get_limit)
120 return (unsigned char) *stream->__bufp++;
122 stream->__eof = 1;
123 return EOF;
126 /* Initialize STREAM to talk to OBSTACK. */
128 static void
129 init_obstream (stream, obstack)
130 FILE *stream;
131 struct obstack *obstack;
133 stream->__mode.__write = 1;
134 stream->__mode.__read = 1;
136 /* Input can read only what has been written. */
137 stream->__room_funcs.__input = input;
139 /* Do nothing for close. */
140 stream->__io_funcs.__close = NULL;
142 /* When the buffer is full, grow the obstack. */
143 stream->__room_funcs.__output = grow;
145 /* Seek within the object, and extend it. */
146 stream->__io_funcs.__seek = seek;
147 stream->__target = stream->__offset = 0;
149 stream->__seen = 1;
151 /* Don't deallocate that buffer! */
152 stream->__userbuf = 1;
154 /* We don't have to initialize the buffer.
155 The first read attempt will call grow, which will do all the work. */
158 FILE *
159 open_obstack_stream (obstack)
160 struct obstack *obstack;
162 register FILE *stream;
164 stream = __newstream ();
165 if (stream == NULL)
166 return NULL;
168 init_obstream (stream, obstack);
169 return stream;
173 obstack_vprintf (obstack, format, args)
174 struct obstack *obstack;
175 const char *format;
176 va_list args;
178 FILE f;
179 bzero (&f, sizeof (f));
180 init_obstream (&f, obstack);
181 return vfprintf (&f, format, args);
185 obstack_printf (struct obstack *obstack, const char *format, ...)
187 int result;
188 va_list ap;
189 va_start (ap, format);
190 result = obstack_vprintf (obstack, format, ap);
191 va_end (ap);
192 return result;