]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/setvbuf.c
2 Implementation of setvbuf as declared in <stdio.h>.
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License that accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 Copyright (c) 1990, 1993
14 The Regents of the University of California. All rights reserved.
16 This code is derived from software contributed to Berkeley by
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions
22 - Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24 - Redistributions in binary form must reproduce the above copyright
25 notice, this list of conditions and the following disclaimer in the
26 documentation and/or other materials provided with the distribution.
27 - Neither the name of the University nor the names of its contributors
28 may be used to endorse or promote products derived from this software
29 without specific prior written permission.
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
35 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 POSSIBILITY OF SUCH DAMAGE.
43 NetBSD: setvbuf.c,v 1.17 2003/08/07 16:43:31 agc Exp
44 setvbuf.c 8.2 (Berkeley) 11/16/93
46 #include <LibConfig.h>
53 #include "reentrant.h"
58 * Set one of the three kinds of buffering, optionally including
62 setvbuf(FILE *fp
, char *buf
, int mode
, size_t size
)
68 _DIAGASSERT(fp
!= NULL
);
72 * Verify arguments. The `int' limit on `size' is due to this
73 * particular implementation. Note, buf and size are ignored
74 * when setting _IONBF.
77 if ((mode
!= _IOFBF
&& mode
!= _IOLBF
) || (int)size
< 0)
82 * Write current buffer, if any. Discard unread input (including
83 * ungetc data), cancel line buffering, and free old buffer if
84 * malloc()ed. We also clear any eof condition, as if this were
92 fp
->_r
= fp
->_lbfsize
= 0;
95 free((void *)fp
->_bf
._base
);
96 flags
&= ~(__SLBF
| __SNBF
| __SMBF
| __SOPT
| __SNPT
| __SEOF
);
98 /* If setting unbuffered mode, skip all the hard work. */
103 * Find optimal I/O size for seek optimization. This also returns
104 * a `tty flag' to suggest that we check isatty(fd), but we do not
105 * care since our caller told us how to buffer.
107 flags
|= __swhatbuf(fp
, &iosize
, &ttyflag
);
109 buf
= NULL
; /* force local allocation */
113 /* Allocate buffer if needed. */
115 if ((buf
= malloc(size
)) == NULL
) {
117 * Unable to honor user's request. We will return
118 * failure, but try again with file system size.
121 if (size
!= iosize
) {
127 /* No luck; switch to unbuffered I/O. */
129 fp
->_flags
= (unsigned short)(flags
| __SNBF
);
131 fp
->_bf
._base
= fp
->_p
= fp
->_nbuf
;
140 * Kill any seek optimization if the buffer is not the
143 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
149 * Fix up the FILE fields, and set gMD->cleanup for output flush on
150 * exit (since we are buffered in some way).
154 fp
->_flags
= (unsigned short)flags
;
155 fp
->_bf
._base
= fp
->_p
= (unsigned char *)buf
;
156 fp
->_bf
._size
= (int)size
;
157 /* fp->_lbfsize is still 0 */
160 * Begin or continue writing: see __swsetup(). Note
161 * that __SNBF is impossible (it was handled earlier).
163 if (flags
& __SLBF
) {
165 fp
->_lbfsize
= -fp
->_bf
._size
;
169 /* begin/continue reading, or stay in intermediate state */
172 gMD
->cleanup
= _cleanup
;