1 /* $OpenBSD: fvwrite.c,v 1.16 2009/10/22 01:23:16 guenther Exp $ */
3 * SPDX-License-Identifier: BSD-3-Clause
5 * Copyright (C) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
21 * Write some memory regions. Return zero on success, EOF on error.
23 * This routine is large and unsightly, but most of the ugliness due
24 * to the three different kinds of output buffering is handled here.
27 __sfvwrite(LTTNG_UST_LFILE
*fp
, struct __lttng_ust_suio
*uio
)
31 struct __lttng_ust_siov
*iov
;
36 if ((len
= uio
->uio_resid
) == 0)
38 /* make sure we can write */
44 #define MIN(a, b) ((a) < (b) ? (a) : (b))
45 #define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
51 #define GETIOV(extra_work) \
58 if (fp
->_flags
& __SNBF
) {
60 * Unbuffered: write up to BUFSIZ bytes at a time.
64 w
= (*fp
->_write
)(fp
->_cookie
, p
, MIN(len
, BUFSIZ
));
69 } while ((uio
->uio_resid
-= w
) != 0);
70 } else if ((fp
->_flags
& __SLBF
) == 0) {
72 * Fully buffered: fill partially full buffer, if any,
73 * and then flush. If there is no partial buffer, write
74 * one _bf._size byte chunk directly (without copying).
76 * String output is a special case: write as many bytes
77 * as fit, but pretend we wrote everything. This makes
78 * snprintf() return the number of bytes needed, rather
79 * than the number used, and avoids its write function
80 * (so that the write function can be invalid).
84 if ((fp
->_flags
& (__SALC
| __SSTR
)) ==
85 (__SALC
| __SSTR
) && fp
->_w
< len
) {
86 size_t blen
= fp
->_p
- fp
->_bf
._base
;
90 /* Allocate space exponentially. */
91 _size
= fp
->_bf
._size
;
93 _size
= (_size
<< 1) + 1;
94 } while (_size
< blen
+ len
);
95 _base
= realloc(fp
->_bf
._base
, _size
+ 1);
98 fp
->_w
+= _size
- fp
->_bf
._size
;
99 fp
->_bf
._base
= _base
;
100 fp
->_bf
._size
= _size
;
101 fp
->_p
= _base
+ blen
;
104 if (fp
->_flags
& __SSTR
) {
107 COPY(w
); /* copy MIN(fp->_w,len), */
110 w
= len
; /* but pretend copied all */
111 } else if (fp
->_p
> fp
->_bf
._base
&& len
> w
) {
114 /* fp->_w -= w; */ /* unneeded */
116 if (ust_safe_fflush(fp
))
118 } else if (len
>= (w
= fp
->_bf
._size
)) {
120 w
= (*fp
->_write
)(fp
->_cookie
, p
, w
);
132 } while ((uio
->uio_resid
-= w
) != 0);
135 * Line buffered: like fully buffered, but we
136 * must check for newlines. Compute the distance
137 * to the first newline (including the newline),
138 * or `infinity' if there is none, then pretend
139 * that the amount to write is MIN(len,nldist).
142 nldist
= 0; /* XXX just to keep gcc happy */
146 nl
= memchr((void *)p
, '\n', len
);
147 nldist
= nl
? nl
+ 1 - p
: len
+ 1;
150 s
= MIN(len
, nldist
);
151 w
= fp
->_w
+ fp
->_bf
._size
;
152 if (fp
->_p
> fp
->_bf
._base
&& s
> w
) {
156 if (ust_safe_fflush(fp
))
158 } else if (s
>= (w
= fp
->_bf
._size
)) {
159 w
= (*fp
->_write
)(fp
->_cookie
, p
, w
);
168 if ((nldist
-= w
) == 0) {
169 /* copied the newline: flush and forget */
170 if (ust_safe_fflush(fp
))
176 } while ((uio
->uio_resid
-= w
) != 0);
181 fp
->_flags
|= __SERR
;
This page took 0.043866 seconds and 4 git commands to generate.