aboutsummaryrefslogtreecommitdiff
path: root/contrib/sendmail/libsm/fvwrite.c
blob: 234197841e2fb931a559ae845b927c754f2244a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
 *      All rights reserved.
 * Copyright (c) 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Chris Torek.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: fvwrite.c,v 1.1.1.1 2002/02/17 21:56:43 gshapiro Exp $")
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <sm/io.h>
#include <sm/setjmp.h>
#include <sm/conf.h>
#include "local.h"
#include "fvwrite.h"

/*
**  SM_FVWRITE -- write memory regions and buffer for file pointer
**
**	Parameters:
**		fp -- the file pointer to write to
**		timeout -- time length for function to return by
**		uio -- the memory regions to write
**
**	Returns:
**		Failure: returns SM_IO_EOF and sets errno
**		Success: returns 0 (zero)
**
**	This routine is large and unsightly, but most of the ugliness due
**	to the different kinds of output buffering handled here.
*/

#define COPY(n)	  (void)memcpy((void *)fp->f_p, (void *)p, (size_t)(n))
#define GETIOV(extra_work)		\
	while (len == 0)		\
	{				\
		extra_work;		\
		p = iov->iov_base;	\
		len = iov->iov_len;	\
		iov++;			\
	}

int
sm_fvwrite(fp, timeout, uio)
	register SM_FILE_T *fp;
	int timeout;
	register struct sm_uio *uio;
{
	register size_t len;
	register char *p;
	register struct sm_iov *iov;
	register int w, s;
	char *nl;
	int nlknown, nldist;
	int fd;
	struct timeval to;

	if (uio->uio_resid == 0)
		return 0;

	/* make sure we can write */
	if (cantwrite(fp))
	{
		errno = EBADF;
		return SM_IO_EOF;
	}

	SM_CONVERT_TIME(fp, fd, timeout, &to);

	iov = uio->uio_iov;
	p = iov->iov_base;
	len = iov->iov_len;
	iov++;
	if (fp->f_flags & SMNBF)
	{
		/* Unbuffered: write up to BUFSIZ bytes at a time. */
		do
		{
			GETIOV(;);
			errno = 0; /* needed to ensure EOF correctly found */
			w = (*fp->f_write)(fp, p, SM_MIN(len, SM_IO_BUFSIZ));
			if (w <= 0)
			{
				if (w == 0 && errno == 0)
					break; /* EOF found */
				if (IS_IO_ERROR(fd, w, timeout))
					goto err; /* errno set */

				/* write would block */
				SM_IO_WR_TIMEOUT(fp, fd, timeout);
				w = 0;
			}
			else
			{
				p += w;
				len -= w;
			}
		} while ((uio->uio_resid -= w) != 0);
	}
	else if ((fp->f_flags & SMLBF) == 0)
	{
		/*
		**  Not SMLBF (line-buffered). Either SMFBF or SMNOW
		**  buffered: fill partially full buffer, if any,
		**  and then flush.  If there is no partial buffer, write
		**  one bf._size byte chunk directly (without copying).
		**
		**  String output is a special case: write as many bytes
		**  as fit, but pretend we wrote everything.  This makes
		**  snprintf() return the number of bytes needed, rather
		**  than the number used, and avoids its write function
		**  (so that the write function can be invalid).
		*/

		do
		{
			GETIOV(;);
			if ((((fp->f_flags & (SMALC | SMSTR)) == (SMALC | SMSTR))
			    || ((fp->f_flags & SMNOW) != 0))
			    && (size_t) fp->f_w < len)
			{
				size_t blen = fp->f_p - fp->f_bf.smb_base;
				unsigned char *tbase;
				int tsize;

				/* Allocate space exponentially. */
				tsize = fp->f_bf.smb_size;
				do
				{
					tsize = (tsize << 1) + 1;
				} while ((size_t) tsize < blen + len);
				tbase = (unsigned char *) sm_realloc(fp->f_bf.smb_base,
								     tsize + 1);
				if (tbase == NULL)
				{
					errno = ENOMEM;
					goto err; /* errno set */
				}
				fp->f_w += tsize - fp->f_bf.smb_size;
				fp->f_bf.smb_base = tbase;
				fp->f_bf.smb_size = tsize;
				fp->f_p = tbase + blen;
			}
			w = fp->f_w;
			errno = 0; /* needed to ensure EOF correctly found */
			if (fp->f_flags & SMSTR)
			{
				if (len < (size_t) w)
					w = len;
				COPY(w);	/* copy SM_MIN(fp->f_w,len), */
				fp->f_w -= w;
				fp->f_p += w;
				w = len;	/* but pretend copied all */
			}
			else if (fp->f_p > fp->f_bf.smb_base
				 && len > (size_t) w)
			{
				/* fill and flush */
				COPY(w);
				fp->f_p += w;
				if (sm_flush(fp, &timeout))
					goto err; /* errno set */
			}
			else if (len >= (size_t) (w = fp->f_bf.smb_size))
			{
				/* write directly */
				w = (*fp->f_write)(fp, p, w);
				if (w <= 0)
				{
					if (w == 0 && errno == 0)
						break; /* EOF found */
					if (IS_IO_ERROR(fd, w, timeout))
						goto err; /* errno set */

					/* write would block */
					SM_IO_WR_TIMEOUT(fp, fd, timeout);
					w = 0;
				}
			}
			else
			{
				/* fill and done */
				w = len;
				COPY(w);
				fp->f_w -= w;
				fp->f_p += w;
			}
			p += w;
			len -= w;
		} while ((uio->uio_resid -= w) != 0);

		if ((fp->f_flags & SMNOW) != 0 && sm_flush(fp, &timeout))
			goto err; /* errno set */
	}
	else
	{
		/*
		**  Line buffered: like fully buffered, but we
		**  must check for newlines.  Compute the distance
		**  to the first newline (including the newline),
		**  or `infinity' if there is none, then pretend
		**  that the amount to write is SM_MIN(len,nldist).
		*/

		nlknown = 0;
		nldist = 0;	/* XXX just to keep gcc happy */
		do
		{
			GETIOV(nlknown = 0);
			if (!nlknown)
			{
				nl = memchr((void *)p, '\n', len);
				nldist = nl != NULL ? nl + 1 - p : len + 1;
				nlknown = 1;
			}
			s = SM_MIN(len, ((size_t) nldist));
			w = fp->f_w + fp->f_bf.smb_size;
			errno = 0; /* needed to ensure EOF correctly found */
			if (fp->f_p > fp->f_bf.smb_base && s > w)
			{
				COPY(w);
				/* fp->f_w -= w; */
				fp->f_p += w;
				if (sm_flush(fp, &timeout))
					goto err; /* errno set */
			}
			else if (s >= (w = fp->f_bf.smb_size))
			{
				w = (*fp->f_write)(fp, p, w);
				if (w <= 0)
				{
					if (w == 0 && errno == 0)
						break; /* EOF found */
					if (IS_IO_ERROR(fd, w, timeout))
						goto err; /* errno set */

					/* write would block */
					SM_IO_WR_TIMEOUT(fp, fd, timeout);
					w = 0;
				}
			}
			else
			{
				w = s;
				COPY(w);
				fp->f_w -= w;
				fp->f_p += w;
			}
			if ((nldist -= w) == 0)
			{
				/* copied the newline: flush and forget */
				if (sm_flush(fp, &timeout))
					goto err; /* errno set */
				nlknown = 0;
			}
			p += w;
			len -= w;
		} while ((uio->uio_resid -= w) != 0);
	}

	return 0;

err:
	/* errno set before goto places us here */
	fp->f_flags |= SMERR;
	return SM_IO_EOF;
}