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