blob: 888be1ea3bfdd07e58a27363c141e92936df65b5 [file] [log] [blame]
Vishal Bhoj82c80712015-12-15 21:13:33 +05301/*
2 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
3 This program and the accompanying materials are licensed and made available
4 under the terms and conditions of the BSD License that accompanies this
5 distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 * Copyright (c) 1990, 1993
12 * The Regents of the University of California. All rights reserved.
13 *
14 * This code is derived from software contributed to Berkeley by
15 * Chris Torek.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40
41 NetBSD: wbuf.c,v 1.13 2003/08/07 16:43:35 agc Exp
42 wbuf.c 8.1 (Berkeley) 6/4/93
43*/
44#include <LibConfig.h>
45#include <sys/EfiCdefs.h>
46
47#include <assert.h>
48#include <errno.h>
49#include <stdio.h>
50#include "reentrant.h"
51#include "local.h"
52
53/*
54 * Write the given character into the (probably full) buffer for
55 * the given file. Flush the buffer out if it is or becomes full,
56 * or if c=='\n' and the file is line buffered.
57 */
58int
59__swbuf(int c, FILE *fp)
60{
61 int n;
62
63 //_DIAGASSERT(fp != NULL);
64 if(fp == NULL) {
65 errno = EINVAL;
66 return (EOF);
67 }
68
69 _SET_ORIENTATION(fp, -1);
70
71 /*
72 * In case we cannot write, or longjmp takes us out early,
73 * make sure _w is 0 (if fully- or un-buffered) or -_bf._size
74 * (if line buffered) so that we will get called again.
75 * If we did not do this, a sufficient number of putc()
76 * calls might wrap _w from negative to positive.
77 */
78 fp->_w = fp->_lbfsize;
79 if (cantwrite(fp)) {
80 errno = EBADF;
81 return (EOF);
82 }
83 c = (unsigned char)c;
84
85 /*
86 * If it is completely full, flush it out. Then, in any case,
87 * stuff c into the buffer. If this causes the buffer to fill
88 * completely, or if c is '\n' and the file is line buffered,
89 * flush it (perhaps a second time). The second flush will always
90 * happen on unbuffered streams, where _bf._size==1; fflush()
91 * guarantees that putc() will always call wbuf() by setting _w
92 * to 0, so we need not do anything else.
93 */
94 n = (int)(fp->_p - fp->_bf._base);
95 if (n >= fp->_bf._size) {
96 if (fflush(fp))
97 return (EOF);
98 n = 0;
99 }
100 fp->_w--;
101 *fp->_p++ = (unsigned char)c;
102 if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n'))
103 if (fflush(fp))
104 return (EOF);
105 return (c);
106}