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