Regenerated from ../sysdeps/mach/hurd/errnos.awk ../manual/errno.texi ../../mach...
[platform/upstream/glibc.git] / stdio / obstream.c
1 /* Copyright (C) 1992 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
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.
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
12 Library General Public License for more details.
13
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.  */
18
19 #include <ansidecl.h>
20 #include <stdio.h>
21 #include <obstack.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 /* Output-room function for obstack streams.  */
26
27 static void
28 DEFUN(grow, (stream, c), FILE *stream AND int c)
29 {
30   struct obstack *const obstack = (struct obstack *) stream->__cookie;
31
32   /* Move the end of the object back to include only the portion
33      of the buffer which the user has already written into.  */ 
34   obstack_blank_fast (obstack, - (stream->__put_limit - stream->__bufp));
35
36   if (stream->__target > obstack_object_size (obstack))
37     {
38       /* Our target (where the buffer maps to) is always zero except when
39          the user just did a SEEK_END fseek.  If he sought within the
40          buffer, we need do nothing and will zero the target below.  If he
41          sought past the end of the object, grow and zero-fill the object
42          up to the target address.  */
43
44       obstack_blank (obstack,
45                      stream->__target - obstack_object_size (obstack));
46       /* fseek has just flushed us, so the put limit points
47          to the end of the written data.  */
48       bzero (stream->__put_limit,
49              stream->__target - stream->__bufsize);
50     }
51
52   if (c != EOF)
53     obstack_1grow (obstack, (unsigned char) c);
54
55   /* The stream buffer always maps exactly to the object on the top
56      of the obstack.  The start of the buffer is the start of the object.
57      The put limit points just past the end of the object.  On fflush, the
58      obstack is sync'd so the end of the object points just past the last
59      character written to the stream.  */
60
61   stream->__target = stream->__offset = 0;
62   stream->__buffer = obstack_base (obstack);
63   stream->__bufsize = obstack_room (obstack);
64   stream->__bufp = obstack_next_free (obstack);
65   stream->__get_limit = stream->__bufp;
66
67   if (c == EOF)
68     /* This is fflush.  Make the stream buffer, the object,
69        and the characters actually written all match.  */
70     stream->__put_limit = stream->__get_limit;
71   else
72     {
73       /* Extend the buffer (and the object) to include
74          the rest of the obstack chunk (which is unitialized).
75          Data past bufp is undefined.  */
76       stream->__put_limit = stream->__buffer + stream->__bufsize;
77       obstack_blank_fast (obstack, stream->__put_limit - stream->__bufp);
78     }
79 }
80
81 /* Seek function for obstack streams.
82    There is no external state to munge.  */
83
84 static int
85 DEFUN(seek, (cookie, pos, whence),
86       PTR cookie AND fpos_t *pos AND int whence)
87 {
88   switch (whence)
89     {
90     case SEEK_SET:
91     case SEEK_CUR:
92       return 0;
93
94     case SEEK_END:
95       /* Return the position relative to the end of the object.
96          fseek has just flushed us, so the obstack is consistent.  */
97       *pos += obstack_object_size ((struct obstack *) cookie);
98       return 0;
99
100     default:
101       __libc_fatal ("obstream::seek called with bogus WHENCE\n");
102       return -1;
103     }
104 }
105
106 /* Input room function for obstack streams.
107    Only what has been written to the stream can be read back.  */
108
109 static int
110 DEFUN(input, (stream), FILE *stream)
111 {
112   /* Re-sync with the obstack, growing the object if necessary.  */
113   grow (stream, EOF);
114
115   if (stream->__bufp < stream->__get_limit)
116     return (unsigned char) *stream->__bufp++;
117
118   stream->__eof = 1;
119   return EOF;
120 }
121 \f
122 /* Initialize STREAM to talk to OBSTACK.  */
123
124 static void
125 DEFUN(init_obstream, (stream, obstack),
126       FILE *stream AND struct obstack *obstack)
127 {
128   stream->__mode.__write = 1;
129   stream->__mode.__read = 1;
130
131   /* Input can read only what has been written.  */
132   stream->__room_funcs.__input = input;
133
134   /* Do nothing for close.  */
135   stream->__io_funcs.__close = NULL;
136
137   /* When the buffer is full, grow the obstack.  */
138   stream->__room_funcs.__output = grow;
139
140   /* Seek within the object, and extend it.  */
141   stream->__io_funcs.__seek = seek;
142   stream->__target = stream->__offset = 0;
143
144   stream->__seen = 1;
145
146   /* Don't deallocate that buffer!  */
147   stream->__userbuf = 1;
148
149   /* We don't have to initialize the buffer.
150      The first read attempt will call grow, which will do all the work.  */
151 }
152
153 FILE *
154 open_obstack_stream (obstack)
155      struct obstack *obstack;
156 {
157   register FILE *stream;
158
159   stream = __newstream ();
160   if (stream == NULL)
161     return NULL;
162
163   init_obstream (stream, obstack);
164   return stream;
165 }
166
167 int
168 DEFUN(obstack_vprintf, (obstack, format, args),
169       struct obstack *obstack AND const char *format AND va_list args)
170 {
171   FILE f;
172   bzero (&f, sizeof (f));
173   init_obstream (&f, obstack);
174   return vfprintf (&f, format, args);
175 }
176
177 int
178 DEFUN(obstack_printf, (obstack, format),
179       struct obstack *obstack AND const char *format DOTS)
180 {
181   int result;
182   va_list ap;
183   va_start (ap, format);
184   result = obstack_vprintf (obstack, format, ap);
185   va_end (ap);
186   return result;
187 }