]>
git.saurik.com Git - apple/libc.git/blob - stdio.subproj/fvwrite.c
f7d2c86754c8d8bdeae3c46efe6e33f9cc0b153c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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.
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
65 * Write some memory regions. Return zero on success, EOF on error.
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.
73 register struct __suio
*uio
;
77 register struct __siov
*iov
;
82 if ((len
= uio
->uio_resid
) == 0)
84 /* make sure we can write */
88 #define MIN(a, b) ((a) < (b) ? (a) : (b))
89 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
95 #define GETIOV(extra_work) \
102 if (fp
->_flags
& __SNBF
) {
104 * Unbuffered: write up to BUFSIZ bytes at a time.
108 w
= (*fp
->_write
)(fp
->_cookie
, p
, MIN(len
, BUFSIZ
));
113 } while ((uio
->uio_resid
-= w
) != 0);
114 } else if ((fp
->_flags
& __SLBF
) == 0) {
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).
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).
129 if (fp
->_flags
& __SSTR
) {
132 COPY(w
); /* copy MIN(fp->_w,len), */
135 w
= len
; /* but pretend copied all */
136 } else if (fp
->_p
> fp
->_bf
._base
&& len
> w
) {
139 /* fp->_w -= w; */ /* unneeded */
143 } else if (len
>= (w
= fp
->_bf
._size
)) {
145 w
= (*fp
->_write
)(fp
->_cookie
, p
, w
);
157 } while ((uio
->uio_resid
-= w
) != 0);
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).
167 nldist
= 0; /* XXX just to keep gcc happy */
171 nl
= memchr((void *)p
, '\n', len
);
172 nldist
= nl
? nl
+ 1 - p
: len
+ 1;
175 s
= MIN(len
, nldist
);
176 w
= fp
->_w
+ fp
->_bf
._size
;
177 if (fp
->_p
> fp
->_bf
._base
&& s
> w
) {
183 } else if (s
>= (w
= fp
->_bf
._size
)) {
184 w
= (*fp
->_write
)(fp
->_cookie
, p
, w
);
193 if ((nldist
-= w
) == 0) {
194 /* copied the newline: flush and forget */
201 } while ((uio
->uio_resid
-= w
) != 0);
206 fp
->_flags
|= __SERR
;