| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: fvwrite.c,v 1.28 2021/07/16 12:34:10 christos Exp $ */ | | 1 | /* $NetBSD: fvwrite.c,v 1.29 2021/07/19 10:00:32 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1990, 1993 | | 4 | * Copyright (c) 1990, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to Berkeley by | | 7 | * This code is derived from software contributed to Berkeley by |
8 | * Chris Torek. | | 8 | * Chris Torek. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -27,64 +27,63 @@ | | | @@ -27,64 +27,63 @@ |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * SUCH DAMAGE. | | 32 | * SUCH DAMAGE. |
33 | */ | | 33 | */ |
34 | | | 34 | |
35 | #include <sys/cdefs.h> | | 35 | #include <sys/cdefs.h> |
36 | #if defined(LIBC_SCCS) && !defined(lint) | | 36 | #if defined(LIBC_SCCS) && !defined(lint) |
37 | #if 0 | | 37 | #if 0 |
38 | static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93"; | | 38 | static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93"; |
39 | #else | | 39 | #else |
40 | __RCSID("$NetBSD: fvwrite.c,v 1.28 2021/07/16 12:34:10 christos Exp $"); | | 40 | __RCSID("$NetBSD: fvwrite.c,v 1.29 2021/07/19 10:00:32 christos Exp $"); |
41 | #endif | | 41 | #endif |
42 | #endif /* LIBC_SCCS and not lint */ | | 42 | #endif /* LIBC_SCCS and not lint */ |
43 | | | 43 | |
44 | #include <assert.h> | | 44 | #include <assert.h> |
45 | #include <stddef.h> | | 45 | #include <stddef.h> |
46 | #include <errno.h> | | 46 | #include <errno.h> |
47 | #include <stdio.h> | | 47 | #include <stdio.h> |
48 | #include <stdlib.h> | | 48 | #include <stdlib.h> |
49 | #include <string.h> | | 49 | #include <string.h> |
50 | #include "reentrant.h" | | 50 | #include "reentrant.h" |
51 | #include "local.h" | | 51 | #include "local.h" |
52 | #include "fvwrite.h" | | 52 | #include "fvwrite.h" |
53 | | | 53 | |
54 | static int | | 54 | static int |
55 | flush_adj(FILE *fp, struct __suio *uio, struct __siov *iov, ssize_t w) | | 55 | flush_adj(FILE *fp, struct __suio *uio, ssize_t w) |
56 | { | | 56 | { |
57 | int rc; | | 57 | int rc; |
58 | | | 58 | |
59 | _DIAGASSERT(w >= 0); | | 59 | _DIAGASSERT(w >= 0); |
60 | _DIAGASSERT(fp->_w >= 0); | | 60 | _DIAGASSERT(fp->_w >= 0); |
61 | | | 61 | |
62 | if ((rc = fflush(fp)) == 0) | | 62 | if ((rc = fflush(fp)) == 0) |
63 | return 0; | | 63 | return 0; |
64 | | | 64 | |
65 | /* | | 65 | /* |
66 | * If we have to return without writing the whole buffer, | | 66 | * If we have to return without writing the whole buffer, |
67 | * adjust for how much fflush() has written for us. | | 67 | * adjust for how much fflush() has written for us. |
68 | * `w' is the amt. of new user data just copied into our | | 68 | * `w' is the amt. of new user data just copied into our |
69 | * internal buffer in _this_ fwrite() call. | | 69 | * internal buffer in _this_ fwrite() call. |
70 | */ | | 70 | */ |
71 | if (fp->_w < w) { | | 71 | if (fp->_w < w) { |
72 | /* some new data was also written */ | | 72 | /* some new data was also written */ |
73 | ssize_t i = w - fp->_w; | | 73 | ssize_t i = w - fp->_w; |
74 | | | 74 | |
75 | /* adjust amt. written */ | | 75 | /* adjust amt. written */ |
76 | uio->uio_resid -= i; | | 76 | uio->uio_resid -= i; |
77 | iov->iov_len -= i; | | | |
78 | } else { | | 77 | } else { |
79 | /* only old stuff was written */ | | 78 | /* only old stuff was written */ |
80 | | | 79 | |
81 | /* adjust _p and _w so user can retry */ | | 80 | /* adjust _p and _w so user can retry */ |
82 | fp->_p -= w; | | 81 | fp->_p -= w; |
83 | fp->_w += w; | | 82 | fp->_w += w; |
84 | } | | 83 | } |
85 | return rc; | | 84 | return rc; |
86 | } | | 85 | } |
87 | | | 86 | |
88 | /* | | 87 | /* |
89 | * Write some memory regions. Return zero on success, EOF on error. | | 88 | * Write some memory regions. Return zero on success, EOF on error. |
90 | * | | 89 | * |
| @@ -126,27 +125,27 @@ __sfvwrite(FILE *fp, struct __suio *uio) | | | @@ -126,27 +125,27 @@ __sfvwrite(FILE *fp, struct __suio *uio) |
126 | iov++; | | 125 | iov++; |
127 | #define GETIOV(extra_work) \ | | 126 | #define GETIOV(extra_work) \ |
128 | while (len == 0) { \ | | 127 | while (len == 0) { \ |
129 | extra_work; \ | | 128 | extra_work; \ |
130 | p = iov->iov_base; \ | | 129 | p = iov->iov_base; \ |
131 | len = iov->iov_len; \ | | 130 | len = iov->iov_len; \ |
132 | iov++; \ | | 131 | iov++; \ |
133 | } | | 132 | } |
134 | #define WRITE(nw) \ | | 133 | #define WRITE(nw) \ |
135 | w = (*fp->_write)(fp->_cookie, p, nw); \ | | 134 | w = (*fp->_write)(fp->_cookie, p, nw); \ |
136 | if (w <= 0) \ | | 135 | if (w <= 0) \ |
137 | goto err | | 136 | goto err |
138 | #define FLUSH(nw) \ | | 137 | #define FLUSH(nw) \ |
139 | if (flush_adj(fp, uio, iov - 1, nw)) \ | | 138 | if (flush_adj(fp, uio, nw)) \ |
140 | goto err | | 139 | goto err |
141 | | | 140 | |
142 | if (fp->_flags & __SNBF) { | | 141 | if (fp->_flags & __SNBF) { |
143 | /* | | 142 | /* |
144 | * Unbuffered: write up to BUFSIZ bytes at a time. | | 143 | * Unbuffered: write up to BUFSIZ bytes at a time. |
145 | */ | | 144 | */ |
146 | do { | | 145 | do { |
147 | GETIOV(;); | | 146 | GETIOV(;); |
148 | WRITE(MIN(len, BUFSIZ)); | | 147 | WRITE(MIN(len, BUFSIZ)); |
149 | p += w; | | 148 | p += w; |
150 | len -= w; | | 149 | len -= w; |
151 | } while ((uio->uio_resid -= w) != 0); | | 150 | } while ((uio->uio_resid -= w) != 0); |
152 | } else if ((fp->_flags & __SLBF) == 0) { | | 151 | } else if ((fp->_flags & __SLBF) == 0) { |