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