1 /* Copyright (C) 1991, 1993, 1995, 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 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.
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 Lesser General Public License for more details.
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
25 /* Make STREAM use the buffering method given in MODE.
26 If MODE indicates full or line buffering, use BUF,
27 a buffer of SIZE bytes; if BUF is NULL, malloc a buffer. */
29 setvbuf (stream
, buf
, mode
, size
)
35 if (!__validfp (stream
))
41 /* The ANSI standard says setvbuf can only be called before any I/O is done,
42 but we allow it to replace an old buffer, flushing it first. */
43 if (stream
->__buffer
!= NULL
)
45 (void) fflush (stream
);
46 /* Free the old buffer if it was malloc'd. */
47 if (!stream
->__userbuf
)
48 free(stream
->__buffer
);
51 stream
->__get_limit
= stream
->__put_limit
= NULL
;
52 stream
->__bufp
= stream
->__buffer
= NULL
;
53 stream
->__userbuf
= stream
->__linebuf
= stream
->__linebuf_active
= 0;
60 case _IONBF
: /* Unbuffered. */
61 stream
->__buffer
= NULL
;
62 stream
->__bufsize
= 0;
63 stream
->__userbuf
= 1;
65 case _IOLBF
: /* Line buffered. */
66 stream
->__linebuf
= 1;
67 case _IOFBF
: /* Fully buffered. */
73 stream
->__bufsize
= size
;
75 stream
->__userbuf
= 1;
76 else if ((buf
= (char *) malloc (size
)) == NULL
)
78 stream
->__buffer
= buf
;
82 stream
->__bufp
= stream
->__buffer
;
83 stream
->__get_limit
= stream
->__buffer
;
84 /* The next output operation will prime the stream for writing. */
85 stream
->__put_limit
= stream
->__buffer
;