Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Libc/stdio/fvwrite.c

  • Property svn:executable set to *
1/*-
2 * Copyright (c) 1990, 1993
3 *The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)fvwrite.c8.1 (Berkeley) 6/4/93";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: src/lib/libc/stdio/fvwrite.c,v 1.19 2009/11/25 04:21:42 wollman Exp $");
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include "local.h"
43#include "fvwrite.h"
44
45/*
46 * Write some memory regions. Return zero on success, EOF on error.
47 *
48 * This routine is large and unsightly, but most of the ugliness due
49 * to the three different kinds of output buffering is handled here.
50 */
51int
52__sfvwrite(fp, uio)
53FILE *fp;
54struct __suio *uio;
55{
56size_t len;
57char *p;
58struct __siov *iov;
59int w, s;
60char *nl;
61int nlknown, nldist;
62
63if (uio->uio_resid == 0)
64return (0);
65/* make sure we can write */
66if (prepwrite(fp) != 0)
67return (EOF);
68
69#defineMIN(a, b) ((a) < (b) ? (a) : (b))
70#defineCOPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
71
72iov = uio->uio_iov;
73p = iov->iov_base;
74len = iov->iov_len;
75iov++;
76#define GETIOV(extra_work) \
77while (len == 0) { \
78extra_work; \
79p = iov->iov_base; \
80len = iov->iov_len; \
81iov++; \
82}
83if (fp->_flags & __SNBF) {
84/*
85 * Unbuffered: write up to BUFSIZ bytes at a time.
86 */
87do {
88GETIOV(;);
89w = _swrite(fp, p, MIN(len, BUFSIZ));
90if (w <= 0)
91goto err;
92p += w;
93len -= w;
94} while ((uio->uio_resid -= w) != 0);
95} else if ((fp->_flags & __SLBF) == 0) {
96/*
97 * Fully buffered: fill partially full buffer, if any,
98 * and then flush. If there is no partial buffer, write
99 * one _bf._size byte chunk directly (without copying).
100 *
101 * String output is a special case: write as many bytes
102 * as fit, but pretend we wrote everything. This makes
103 * snprintf() return the number of bytes needed, rather
104 * than the number used, and avoids its write function
105 * (so that the write function can be invalid).
106 */
107do {
108GETIOV(;);
109if ((fp->_flags & (__SALC | __SSTR)) ==
110 (__SALC | __SSTR) && fp->_w < len) {
111size_t blen = fp->_p - fp->_bf._base;
112
113/*
114 * Alloc an extra 128 bytes (+ 1 for NULL)
115 * so we don't call realloc(3) so often.
116 */
117fp->_w = len + 128;
118fp->_bf._size = blen + len + 128;
119fp->_bf._base =
120 reallocf(fp->_bf._base, fp->_bf._size + 1);
121if (fp->_bf._base == NULL)
122goto err;
123fp->_p = fp->_bf._base + blen;
124}
125w = fp->_w;
126if (fp->_flags & __SSTR) {
127if (len < w)
128w = len;
129if (w > 0) {
130COPY(w); /* copy MIN(fp->_w,len), */
131fp->_w -= w;
132fp->_p += w;
133}
134w = len;/* but pretend copied all */
135} else if (fp->_p > fp->_bf._base && len > w) {
136/* fill and flush */
137COPY(w);
138/* fp->_w -= w; */ /* unneeded */
139fp->_p += w;
140if (__fflush(fp))
141goto err;
142} else if (len >= (w = fp->_bf._size)) {
143/* write directly */
144w = _swrite(fp, p, w);
145if (w <= 0)
146goto err;
147} else {
148/* fill and done */
149w = len;
150COPY(w);
151fp->_w -= w;
152fp->_p += w;
153}
154p += w;
155len -= w;
156} while ((uio->uio_resid -= w) != 0);
157} else {
158/*
159 * Line buffered: like fully buffered, but we
160 * must check for newlines. Compute the distance
161 * to the first newline (including the newline),
162 * or `infinity' if there is none, then pretend
163 * that the amount to write is MIN(len,nldist).
164 */
165nlknown = 0;
166nldist = 0;/* XXX just to keep gcc happy */
167do {
168GETIOV(nlknown = 0);
169if (!nlknown) {
170nl = memchr((void *)p, '\n', len);
171nldist = nl ? nl + 1 - p : len + 1;
172nlknown = 1;
173}
174s = MIN(len, nldist);
175w = fp->_w + fp->_bf._size;
176if (fp->_p > fp->_bf._base && s > w) {
177COPY(w);
178/* fp->_w -= w; */
179fp->_p += w;
180if (__fflush(fp))
181goto err;
182} else if (s >= (w = fp->_bf._size)) {
183w = _swrite(fp, p, w);
184if (w <= 0)
185 goto err;
186} else {
187w = s;
188COPY(w);
189fp->_w -= w;
190fp->_p += w;
191}
192if ((nldist -= w) == 0) {
193/* copied the newline: flush and forget */
194if (__fflush(fp))
195goto err;
196nlknown = 0;
197}
198p += w;
199len -= w;
200} while ((uio->uio_resid -= w) != 0);
201}
202return (0);
203
204err:
205fp->_flags |= __SERR;
206return (EOF);
207}
208

Archive Download this file

Revision: 2182