headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / stdio / fvwrite.c
blob065d5f2e63fa1f8e6634411193a03d22341339c3
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
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.
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.
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
42 #include <errno_private.h>
44 #include "local.h"
45 #include "fvwrite.h"
48 * Write some memory regions. Return zero on success, EOF on error.
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.
53 int
54 __sfvwrite(fp, uio)
55 register FILE *fp;
56 register struct __suio *uio;
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;
65 if ((len = uio->uio_resid) == 0) {
66 return (0);
68 /* make sure we can write */
69 if (cantwrite(fp)) {
70 __set_errno(EBADF);
71 return (EOF);
74 #define MIN(a, b) ((a) < (b) ? (a) : (b))
75 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
77 iov = uio->uio_iov;
78 p = iov->iov_base;
79 len = iov->iov_len;
80 iov++;
81 #define GETIOV(extra_work) \
82 while (len == 0) { \
83 extra_work; \
84 p = iov->iov_base; \
85 len = iov->iov_len; \
86 iov++; \
88 if (fp->_flags & __SNBF) {
90 * Unbuffered: write up to BUFSIZ bytes at a time.
92 do {
93 GETIOV(;);
94 w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
95 if (w <= 0)
96 goto err;
97 p += w;
98 len -= w;
99 } while ((uio->uio_resid -= w) != 0);
100 } else if ((fp->_flags & __SLBF) == 0) {
102 * Fully buffered: fill partially full buffer, if any,
103 * and then flush. If there is no partial buffer, write
104 * one _bf._size byte chunk directly (without copying).
106 * String output is a special case: write as many bytes
107 * as fit, but pretend we wrote everything. This makes
108 * snprintf() return the number of bytes needed, rather
109 * than the number used, and avoids its write function
110 * (so that the write function can be invalid).
112 do {
113 GETIOV(;);
114 if ((fp->_flags & (__SALC | __SSTR)) ==
115 (__SALC | __SSTR) && fp->_w < len) {
116 size_t blen = fp->_p - fp->_bf._base;
117 unsigned char *_base;
118 int _size;
120 /* Allocate space exponentially. */
121 _size = fp->_bf._size;
122 do {
123 _size = (_size << 1) + 1;
124 } while (_size < blen + len);
125 _base = realloc(fp->_bf._base, _size + 1);
126 if (_base == NULL)
127 goto err;
128 fp->_w += _size - fp->_bf._size;
129 fp->_bf._base = _base;
130 fp->_bf._size = _size;
131 fp->_p = _base + blen;
133 w = fp->_w;
134 if (fp->_flags & __SSTR) {
135 if (len < w)
136 w = len;
137 COPY(w); /* copy MIN(fp->_w,len), */
138 fp->_w -= w;
139 fp->_p += w;
140 w = len; /* but pretend copied all */
141 } else if (fp->_p > fp->_bf._base && len > w) {
142 /* fill and flush */
143 COPY(w);
144 /* fp->_w -= w; */ /* unneeded */
145 fp->_p += w;
146 if (fflush(fp))
147 goto err;
148 } else if (len >= (w = fp->_bf._size)) {
149 /* write directly */
150 w = (*fp->_write)(fp->_cookie, p, w);
151 if (w <= 0) {
152 goto err;
154 } else {
155 /* fill and done */
156 w = len;
157 COPY(w);
158 fp->_w -= w;
159 fp->_p += w;
161 p += w;
162 len -= w;
163 } while ((uio->uio_resid -= w) != 0);
164 } else {
166 * Line buffered: like fully buffered, but we
167 * must check for newlines. Compute the distance
168 * to the first newline (including the newline),
169 * or `infinity' if there is none, then pretend
170 * that the amount to write is MIN(len,nldist).
172 nlknown = 0;
173 nldist = 0; /* XXX just to keep gcc happy */
174 do {
175 GETIOV(nlknown = 0);
176 if (!nlknown) {
177 nl = memchr((void *)p, '\n', len);
178 nldist = nl ? nl + 1 - p : len + 1;
179 nlknown = 1;
181 s = MIN(len, nldist);
182 w = fp->_w + fp->_bf._size;
183 if (fp->_p > fp->_bf._base && s > w) {
184 COPY(w);
185 /* fp->_w -= w; */
186 fp->_p += w;
187 if (fflush(fp))
188 goto err;
189 } else if (s >= (w = fp->_bf._size)) {
190 w = (*fp->_write)(fp->_cookie, p, w);
191 if (w <= 0)
192 goto err;
193 } else {
194 w = s;
195 COPY(w);
196 fp->_w -= w;
197 fp->_p += w;
199 if ((nldist -= w) == 0) {
200 /* copied the newline: flush and forget */
201 if (fflush(fp))
202 goto err;
203 nlknown = 0;
205 p += w;
206 len -= w;
207 } while ((uio->uio_resid -= w) != 0);
209 return (0);
211 err:
212 fp->_flags |= __SERR;
213 return (EOF);