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