2 * Copyright (c) 1990, 2006, 2007 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 /* No user fns here. Pesch 15apr92. */
27 #define MIN(a, b) ((a) < (b) ? (a) : (b))
28 #define COPY(n) _CAST_VOID memmove ((_PTR) fp->_p, (_PTR) p, (size_t) (n))
30 #define GETIOV(extra_work) \
40 * Write some memory regions. Return zero on success, EOF on error.
42 * This routine is large and unsightly, but most of the ugliness due
43 * to the three different kinds of output buffering is handled here.
47 _DEFUN(__sfvwrite_r, (ptr, fp, uio),
48 struct _reent *ptr _AND
49 register FILE *fp _AND
50 register struct __suio *uio)
53 register _CONST char *p = NULL;
54 register struct __siov *iov;
59 if ((len = uio->uio_resid) == 0)
62 /* make sure we can write */
63 if (cantwrite (ptr, fp))
74 if (fp->_flags & __SCLE) /* text mode */
81 if (putc (*p, fp) == EOF)
88 while (uio->uio_resid > 0);
93 if (fp->_flags & __SNBF)
96 * Unbuffered: write up to BUFSIZ bytes at a time.
101 w = (*fp->_write) (fp->_cookie, p, MIN (len, BUFSIZ));
107 while ((uio->uio_resid -= w) != 0);
109 else if ((fp->_flags & __SLBF) == 0)
112 * Fully buffered: fill partially full buffer, if any,
113 * and then flush. If there is no partial buffer, write
114 * one _bf._size byte chunk directly (without copying).
116 * String output is a special case: write as many bytes
117 * as fit, but pretend we wrote everything. This makes
118 * snprintf() return the number of bytes needed, rather
119 * than the number used, and avoids its write function
120 * (so that the write function can be invalid). If
121 * we are dealing with the asprintf routines, we will
122 * dynamically increase the buffer size as needed.
128 if (fp->_flags & __SSTR)
130 if (len >= w && fp->_flags & __SMBF)
131 { /* must be asprintf family */
133 int curpos = (fp->_p - fp->_bf._base);
134 /* Choose a geometric growth factor to avoid
135 quadratic realloc behavior, but use a rate less
136 than (1+sqrt(5))/2 to accomodate malloc
137 overhead. asprintf EXPECTS us to overallocate, so
138 that it can add a trailing \0 without
139 reallocating. The new allocation should thus be
140 max(prev_size*1.5, curpos+len+1). */
141 int newsize = fp->_bf._size * 3 / 2;
142 if (newsize < curpos + len + 1)
143 newsize = curpos + len + 1;
144 str = (unsigned char *)_realloc_r (ptr, fp->_bf._base,
148 /* Free buffer which is no longer used. */
149 _free_r (ptr, fp->_bf._base);
150 /* Ensure correct errno, even if free changed it. */
151 ptr->_errno = ENOMEM;
155 fp->_p = str + curpos;
156 fp->_bf._size = newsize;
158 fp->_w = newsize - curpos;
162 COPY (w); /* copy MIN(fp->_w,len), */
165 w = len; /* but pretend copied all */
167 else if (fp->_p > fp->_bf._base && len > w)
171 /* fp->_w -= w; *//* unneeded */
176 else if (len >= (w = fp->_bf._size))
179 w = (*fp->_write) (fp->_cookie, p, w);
194 while ((uio->uio_resid -= w) != 0);
199 * Line buffered: like fully buffered, but we
200 * must check for newlines. Compute the distance
201 * to the first newline (including the newline),
202 * or `infinity' if there is none, then pretend
203 * that the amount to write is MIN(len,nldist).
209 GETIOV (nlknown = 0);
212 nl = memchr ((_PTR) p, '\n', len);
213 nldist = nl ? nl + 1 - p : len + 1;
216 s = MIN (len, nldist);
217 w = fp->_w + fp->_bf._size;
218 if (fp->_p > fp->_bf._base && s > w)
226 else if (s >= (w = fp->_bf._size))
228 w = (*fp->_write) (fp->_cookie, p, w);
239 if ((nldist -= w) == 0)
241 /* copied the newline: flush and forget */
249 while ((uio->uio_resid -= w) != 0);
254 fp->_flags |= __SERR;