1 /* Copyright (C) 1991 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. */
26 /* Minimum size of a buffer we will allocate by default.
27 If this much memory is not available,
28 the stream in question will be made unbuffered instead. */
29 #define MIN_BUFSIZE 128
31 /* Figure out what kind of buffering (none, line, or full)
32 and what buffer size to give FP. */
34 DEFUN(init_stream, (fp), register FILE *fp)
38 /* Initialize the stream's info, including buffering info.
39 This may give a buffer, change I/O functions, etc.
40 If no buffer is set (and the stream is not made explicitly
41 unbuffered), we allocate a buffer below, using the bufsize
42 set by this function. */
43 extern void EXFUN(__stdio_init_stream, (FILE *));
44 fp->__room_funcs = __default_room_functions;
45 fp->__io_funcs = __default_io_functions;
46 __stdio_init_stream(fp);
50 if (fp->__buffer == NULL && !fp->__userbuf)
54 if (fp->__bufsize == 0)
55 fp->__bufsize = BUFSIZ;
57 /* Try to get however many bytes of buffering __stdio_pickbuf
58 specified, but if that much memory isn't available,
59 try half as much each time until it succeeds or the buffer
60 size becomes too small to be useful. */
62 while (fp->__bufsize >= MIN_BUFSIZE)
64 fp->__buffer = (char *) malloc(fp->__bufsize);
65 if (fp->__buffer == NULL)
72 if (fp->__buffer == NULL)
74 /* We can't get space for the buffer, so make it unbuffered. */
80 if (fp->__bufp == NULL)
82 /* Set the buffer pointer to the beginning of the buffer. */
83 fp->__bufp = fp->__buffer;
84 fp->__put_limit = fp->__get_limit = fp->__buffer;
89 /* Determine the current file position of STREAM if it is unknown. */
91 DEFUN(__stdio_check_offset, (stream), FILE *stream)
95 if (stream->__offset == (fpos_t) -1)
97 /* This stream's offset is unknown or unknowable. */
98 if (stream->__io_funcs.__seek == NULL)
106 /* Unknown. Find it out. */
107 fpos_t pos = (fpos_t) 0;
108 if ((*stream->__io_funcs.__seek)(stream->__cookie,
112 /* Object is incapable of seeking. */
113 stream->__io_funcs.__seek = NULL;
116 stream->__offset = pos;
120 if (stream->__target == (fpos_t) -1)
121 /* This stream was opened on an existing object with
122 an unknown file position. The position is now known.
123 Make this the target position. */
124 stream->__target = stream->__offset;
130 /* Move FP's file position to its target file position,
131 seeking as necessary and updating its `offset' field.
132 Sets ferror(FP) (and possibly errno) for errors. */
134 DEFUN(seek_to_target, (fp), FILE *fp)
136 if (__stdio_check_offset (fp) == EOF)
138 else if (fp->__target != fp->__offset)
140 /* We are not at the target file position.
141 Seek to that position. */
142 if (fp->__io_funcs.__seek == NULL)
150 fpos_t pos = fp->__target;
151 if ((*fp->__io_funcs.__seek)(fp->__cookie, &pos, SEEK_SET) < 0)
157 if (pos != fp->__target)
158 /* Seek didn't go to the right place! */
165 /* Flush the buffer for FP.
166 If C is not EOF, it is also to be written.
167 If the stream is line buffered and C is a newline, it is written
168 to the output, otherwise it is put in the buffer after it has been
169 flushed to avoid a system call for a single character.
170 This is the default `output room' function. */
172 DEFUN(flushbuf, (fp, c),
173 register FILE *fp AND int c)
175 int flush_only = c == EOF;
176 size_t buffer_written;
179 /* Set if target and get_limit have already been twiddled appropriately. */
182 if (fp->__put_limit == fp->__buffer)
184 /* The stream needs to be primed for writing. */
186 size_t buffer_offset = 0;
188 /* If the user has read some of the buffer, the target position
189 is incremented for each character he has read. */
190 fp->__target += fp->__bufp - fp->__buffer;
192 if (fp->__mode.__read && fp->__room_funcs.__input != NULL)
195 CONST int aligned = (__stdio_check_offset(fp) == EOF ||
196 fp->__target % fp->__bufsize == 0);
201 /* Move to a block (buffer size) boundary and read in a block.
202 Then the output will be written as a whole block, too. */
203 CONST size_t o = fp->__target % fp->__bufsize;
205 if ((*fp->__room_funcs.__input)(fp) == EOF && ferror(fp))
210 if (fp->__get_limit - fp->__buffer < o)
211 /* Oops. We didn't read enough (probably because we got EOF).
212 Forget we even mentioned it. */
215 /* Start bufp as far into the buffer as we were into
216 this block before we read it. */
220 /* The target position is now set to where the beginning of the
221 buffer maps to; and the get_limit was set by the input-room
226 if (fp->__buffer != NULL)
228 /* Set up to write output into the buffer. */
229 fp->__put_limit = fp->__buffer + fp->__bufsize;
230 fp->__bufp = fp->__buffer + buffer_offset;
233 *fp->__bufp++ = (unsigned char) c;
234 if (!fp->__linebuf || (unsigned char) c != '\n')
241 /* If there is read data in the buffer past what was written,
242 write all of that as well. Otherwise, just write what has been
243 written into the buffer. */
244 buffer_written = fp->__bufp - fp->__buffer;
245 to_write = (buffer_written == 0 ? 0 :
246 fp->__get_limit > fp->__bufp ?
247 fp->__get_limit - fp->__buffer :
250 if (fp->__io_funcs.__write == NULL || (to_write == 0 && flush_only))
252 /* There is no writing function or we're coming from an fflush
253 call with nothing in the buffer, so just say the buffer's
254 been flushed, increment the file offset, and return. */
255 fp->__bufp = fp->__buffer;
256 fp->__offset += to_write;
264 /* Go to the target file position. */
269 /* Write out the buffered data. */
270 wrote = (*fp->__io_funcs.__write)(fp->__cookie, fp->__buffer,
273 /* Record that we've moved forward in the file. */
274 fp->__offset += wrote;
275 if (wrote < (int) to_write)
276 /* The writing function should always write
277 the whole buffer unless there is an error. */
282 /* Reset the buffer pointer to the beginning of the buffer. */
283 fp->__bufp = fp->__buffer;
285 /* If we're not just flushing, write the last character, C. */
286 if (!flush_only && !ferror(fp))
288 if (fp->__buffer == NULL || (fp->__linebuf && (unsigned char) c == '\n'))
290 /* Either we're unbuffered, or we're line-buffered and
291 C is a newline, so really write it out immediately. */
292 char cc = (unsigned char) c;
293 if ((*fp->__io_funcs.__write)(fp->__cookie, &cc, 1) < 1)
296 /* Record that we've moved forward in the file. */
300 /* Just put C in the buffer. */
301 *fp->__bufp++ = (unsigned char) c;
308 /* The new target position moves up as
309 much as the user wrote into the buffer. */
310 fp->__target += buffer_written;
312 /* Set the reading limit to the beginning of the buffer,
313 so the next `getc' will call __fillbf. */
314 fp->__get_limit = fp->__buffer;
317 if (feof(fp) || ferror(fp))
318 fp->__bufp = fp->__put_limit;
322 /* Fill the buffer for FP and return the first character read (or EOF).
323 This is the default `input_room' function. */
325 DEFUN(fillbuf, (fp), register FILE *fp)
327 /* How far into the buffer we read we want to start bufp. */
328 size_t buffer_offset = 0;
329 register char *buffer;
330 register size_t to_read, nread = 0;
333 if (fp->__io_funcs.__read == NULL)
335 /* There is no read function, so always return EOF. */
340 if (fp->__buffer == NULL)
342 /* We're unbuffered, so we want to read only one character. */
348 /* We're buffered, so try to fill the buffer. */
349 buffer = fp->__buffer;
350 to_read = fp->__bufsize;
353 /* We're reading, so we're not at the end-of-file. */
356 /* Go to the target file position. */
359 if (__stdio_check_offset (fp) == 0 && fp->__target != fp->__offset)
361 /* Move to a block (buffer size) boundary. */
362 if (fp->__bufsize != 0)
364 buffer_offset = fp->__target % fp->__bufsize;
365 fp->__target -= buffer_offset;
372 while (!ferror(fp) && !feof(fp) && nread <= buffer_offset)
374 /* Try to fill the buffer. */
375 int count = (*fp->__io_funcs.__read)(fp->__cookie, buffer, to_read);
385 /* Record that we've moved forward in the file. */
386 fp->__offset += count;
390 if (fp->__buffer == NULL)
391 /* There is no buffer, so return the character we read
392 without all the buffer pointer diddling. */
393 return (feof(fp) || ferror(fp)) ? EOF : c;
395 /* Reset the buffer pointer to the beginning of the buffer
396 (plus whatever offset we may have set above). */
397 fp->__bufp = fp->__buffer + buffer_offset;
401 if (feof(fp) || ferror(fp))
403 /* Set both end pointers to the beginning of the buffer so
404 the next i/o call will force a call to __fillbf/__flshfp. */
405 fp->__put_limit = fp->__get_limit = fp->__buffer;
409 /* Set the end pointer to one past the last character we read. */
410 fp->__get_limit = fp->__buffer + nread;
412 /* Make it so the next `putc' will call __flshfp. */
413 fp->__put_limit = fp->__buffer;
415 /* Return the first character in the buffer. */
416 return (unsigned char) *fp->__bufp++;
420 /* Default I/O and room functions. */
422 extern __io_read __stdio_read;
423 extern __io_write __stdio_write;
424 extern __io_seek __stdio_seek;
425 extern __io_close __stdio_close;
426 CONST __io_functions __default_io_functions =
428 __stdio_read, __stdio_write, __stdio_seek, __stdio_close
431 CONST __room_functions __default_room_functions =
437 /* Flush the buffer for FP and also write C if FLUSH_ONLY is nonzero.
438 This is the function used by putc and fflush. */
440 DEFUN(__flshfp, (fp, c),
441 register FILE *fp AND int c)
443 int flush_only = c == EOF;
445 if (!__validfp(fp) || !fp->__mode.__write)
454 /* Make sure the stream is initialized (has functions and buffering). */
457 if (!flush_only && fp->__get_limit == fp->__buffer &&
458 fp->__bufp > fp->__buffer && fp->__bufp < fp->__buffer + fp->__bufsize)
460 /* The character will fit in the buffer, so put it there. */
461 *fp->__bufp++ = (unsigned char) c;
462 if (fp->__linebuf && (unsigned char) c == '\n')
465 return (unsigned char) c;
468 if (fp->__room_funcs.__output == NULL)
470 /* A NULL `output room' function means
471 to always return an output error. */
476 if (!flush_only || fp->__bufp > fp->__buffer)
478 if (fp->__linebuf && fp->__bufp > fp->__buffer)
479 /* This is a line-buffered stream, so its put-limit is
480 set to the beginning of the buffer in order to force
481 a __flshfp call on each putc. We undo this hack here
482 (by setting the limit to the end of the buffer) to simplify
483 the interface with the output-room function.
484 However, we must make sure we don't do this if there isn't
485 actually anything in the buffer, because in that case we
486 want to give the output-room function a chance to prime
487 the stream for writing in whatever fashion turns it on. */
488 fp->__put_limit = fp->__buffer + fp->__bufsize;
490 /* Make room in the buffer. */
491 (*fp->__room_funcs.__output)(fp, flush_only ? EOF : (unsigned char) c);
494 /* Set the end pointer to the beginning of the buffer,
495 so the next `putc' call will force a call to this function
496 (see comment above about line-buffered streams). */
497 fp->__put_limit = fp->__buffer;
504 return (unsigned char) c;
508 /* Fill the buffer for FP and return the first character read.
509 This is the function used by getc. */
511 DEFUN(__fillbf, (fp), register FILE *fp)
516 if (!__validfp(fp) || !fp->__mode.__read)
522 if (fp->__pushed_back)
524 /* Return the char pushed back by ungetc. */
525 fp->__bufp = fp->__pushback_bufp;
526 fp->__pushed_back = 0;
527 return fp->__pushback;
530 /* Make sure the stream is initialized (has functions and buffering). */
533 /* If we're trying to read the first character of a new
534 line of input from an unbuffered or line buffered stream,
535 we must flush all line-buffered output streams. */
536 if (fp->__buffer == NULL || fp->__linebuf)
539 for (f = __stdio_head; f != NULL; f = f->__next)
540 if (__validfp(f) && f->__linebuf && f->__mode.__write &&
541 f->__put_limit > f->__buffer)
542 (void) __flshfp(f, EOF);
545 /* We want the beginning of the buffer to now
546 map to just past the last data we read. */
547 new_target = fp->__target + (fp->__get_limit - fp->__buffer);
549 if (fp->__put_limit > fp->__buffer)
551 /* There is written data in the buffer.
553 if (fp->__room_funcs.__output == NULL)
556 (*fp->__room_funcs.__output)(fp, EOF);
559 fp->__target = new_target;
563 else if (fp->__room_funcs.__input != NULL)
565 c = (*fp->__room_funcs.__input)(fp);
566 if (fp->__buffer == NULL)
567 /* This is an unbuffered stream, so the target sync above
568 won't do anything the next time around. Instead, note that
569 we have read one character. The (nonexistent) buffer now
570 maps to the position just past that character. */
575 /* A NULL `input_room' function means always return EOF. */
584 /* Nuke a stream, but don't kill its link in the chain. */
586 DEFUN(__invalidate, (stream), register FILE *stream)
589 register FILE *next = stream->__next;
591 /* Pulverize the fucker. */
592 memset((PTR) stream, 0, sizeof(FILE));
594 /* Restore the deceased's link. */
595 stream->__next = next;
599 /* Abort with an error message. */
602 DEFUN(__libc_fatal, (message), CONST char *message)
604 __stdio_errmsg(message, strlen(message));