]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/fvwrite.c
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.
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.
11 * Copyright (c) 1990, 1993
12 * The Regents of the University of California. All rights reserved.
14 * This code is derived from software contributed to Berkeley by
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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
41 NetBSD: fvwrite.c,v 1.16.2.1 2007/05/07 19:49:09 pavel Exp
42 fvwrite.c 8.1 (Berkeley) 6/4/93
44 #include <LibConfig.h>
45 #include <sys/EfiCdefs.h>
52 #include "reentrant.h"
57 * Write some memory regions. Return zero on success, EOF on error.
59 * This routine is large and unsightly, but most of the ugliness due
60 * to the three different kinds of output buffering is handled here.
63 __sfvwrite(FILE *fp
, struct __suio
*uio
)
72 _DIAGASSERT(fp
!= NULL
);
73 _DIAGASSERT(uio
!= NULL
);
79 if ((len
= uio
->uio_resid
) == 0)
81 /* make sure we can write */
87 //#define MIN(a, b) ((a) < (b) ? (a) : (b))
88 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
94 #define GETIOV(extra_work) \
101 if (fp
->_flags
& __SNBF
) {
103 * Unbuffered: write up to BUFSIZ bytes at a time.
107 w
= (*fp
->_write
)(fp
->_cookie
, p
,
108 (int)MIN(len
, BUFSIZ
));
113 } while ((uio
->uio_resid
-= w
) > 0);
114 uio
->uio_resid
= 0; // Just in case it went negative such as when NL is expanded to CR NL
115 } else if ((fp
->_flags
& __SLBF
) == 0) {
117 * Fully buffered: fill partially full buffer, if any,
118 * and then flush. If there is no partial buffer, write
119 * one _bf._size byte chunk directly (without copying).
121 * String output is a special case: write as many bytes
122 * as fit, but pretend we wrote everything. This makes
123 * snprintf() return the number of bytes needed, rather
124 * than the number used, and avoids its write function
125 * (so that the write function can be invalid).
129 if ((fp
->_flags
& (__SALC
| __SSTR
)) ==
130 (__SALC
| __SSTR
) && fp
->_w
< (int)len
) {
131 size_t blen
= fp
->_p
- fp
->_bf
._base
;
132 unsigned char *_base
;
135 /* Allocate space exponentially. */
136 _size
= fp
->_bf
._size
;
138 _size
= (_size
<< 1) + 1;
139 } while (_size
< (int)(blen
+ len
));
140 _base
= realloc(fp
->_bf
._base
,
141 (size_t)(_size
+ 1));
144 fp
->_w
+= _size
- fp
->_bf
._size
;
145 fp
->_bf
._base
= _base
;
146 fp
->_bf
._size
= _size
;
147 fp
->_p
= _base
+ blen
;
150 if (fp
->_flags
& __SSTR
) {
153 COPY(w
); /* copy MIN(fp->_w,len), */
156 w
= (int)len
; /* but pretend copied all */
157 } else if (fp
->_p
> fp
->_bf
._base
&& len
> (size_t)w
) {
160 /* fp->_w -= w; */ /* unneeded */
164 } else if (len
>= (size_t)(w
= fp
->_bf
._size
)) {
166 w
= (*fp
->_write
)(fp
->_cookie
, p
, w
);
178 } while ((uio
->uio_resid
-= w
) != 0);
181 * Line buffered: like fully buffered, but we
182 * must check for newlines. Compute the distance
183 * to the first newline (including the newline),
184 * or `infinity' if there is none, then pretend
185 * that the amount to write is MIN(len,nldist).
188 nldist
= 0; /* XXX just to keep gcc happy */
192 nl
= memchr((void *)p
, '\n', len
); // Divide the string at the first '\n'
193 nldist
= (int)(nl
? nl
+ 1 - p
: len
+ 1);
196 s
= (int)(MIN((int)len
, nldist
));
197 w
= fp
->_w
+ fp
->_bf
._size
;
198 if (fp
->_p
> fp
->_bf
._base
&& s
> w
) {
204 } else if (s
>= (w
= fp
->_bf
._size
)) {
205 w
= (*fp
->_write
)(fp
->_cookie
, p
, w
);
214 if ((nldist
-= w
) == 0) {
215 /* copied the newline: flush and forget */
222 } while ((uio
->uio_resid
-= w
) != 0);
227 fp
->_flags
|= __SERR
;