summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libsm/setvbuf.c
blob: 172a767b853598becb4ef9d79a6f01db5403217a (plain)
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
/*
 * 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: setvbuf.c,v 1.32 2001/09/11 04:04:49 gshapiro Exp $")
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sm/io.h>
#include <sm/heap.h>
#include <sm/assert.h>
#include <sm/conf.h>
#include "local.h"

/*
**  SM_IO_SETVBUF -- set the buffering type for a file
**
**  Set one of the different kinds of buffering, optionally including
**  a buffer.
**  If 'size' is == 0 then an "optimal" size will be selected.
**  If 'buf' is == NULL then space will be allocated at 'size'.
**
**	Parameters:
**		fp -- the file that buffering is to be changed for
**		timeout -- time allowed for completing the function
**		buf -- buffer to use
**		mode -- buffering method to use
**		size -- size of 'buf'
**
**	Returns:
**		Failure: SM_IO_EOF
**		Success: 0 (zero)
*/

int
sm_io_setvbuf(fp, timeout, buf, mode, size)
	SM_FILE_T *fp;
	int timeout;
	char *buf;
	int mode;
	size_t size;
{
	int ret, flags;
	size_t iosize;
	int ttyflag;
	int fd;
	struct timeval to;

	SM_REQUIRE_ISA(fp, SmFileMagic);

	/*
	**  Verify arguments.  The `int' limit on `size' is due to this
	**  particular implementation.  Note, buf and size are ignored
	**  when setting SM_IO_NBF.
	*/

	if (mode != SM_IO_NBF)
		if ((mode != SM_IO_FBF && mode != SM_IO_LBF &&
		    mode != SM_IO_NOW) || (int) size < 0)
			return SM_IO_EOF;

	/*
	**  Write current buffer, if any.  Discard unread input (including
	**  ungetc data), cancel line buffering, and free old buffer if
	**  malloc()ed.  We also clear any eof condition, as if this were
	**  a seek.
	*/

	ret = 0;
	SM_CONVERT_TIME(fp, fd, timeout, &to);
	(void) sm_flush(fp, &timeout);
	if (HASUB(fp))
		FREEUB(fp);
	fp->f_r = fp->f_lbfsize = 0;
	flags = fp->f_flags;
	if (flags & SMMBF)
	{
		sm_free((void *) fp->f_bf.smb_base);
		fp->f_bf.smb_base = NULL;
	}
	flags &= ~(SMLBF | SMNBF | SMMBF | SMOPT | SMNPT | SMFEOF | SMNOW |
		   SMFBF);

	/* If setting unbuffered mode, skip all the hard work. */
	if (mode == SM_IO_NBF)
		goto nbf;

	/*
	**  Find optimal I/O size for seek optimization.  This also returns
	**  a `tty flag' to suggest that we check isatty(fd), but we do not
	**  care since our caller told us how to buffer.
	*/

	flags |= sm_whatbuf(fp, &iosize, &ttyflag);
	if (size == 0)
	{
		buf = NULL;	/* force local allocation */
		size = iosize;
	}

	/* Allocate buffer if needed. */
	if (buf == NULL)
	{
		if ((buf = sm_malloc(size)) == NULL)
		{
			/*
			**  Unable to honor user's request.  We will return
			**  failure, but try again with file system size.
			*/

			ret = SM_IO_EOF;
			if (size != iosize)
			{
				size = iosize;
				buf = sm_malloc(size);
			}
		}
		if (buf == NULL)
		{
			/* No luck; switch to unbuffered I/O. */
nbf:
			fp->f_flags = flags | SMNBF;
			fp->f_w = 0;
			fp->f_bf.smb_base = fp->f_p = fp->f_nbuf;
			fp->f_bf.smb_size = 1;
			return ret;
		}
		flags |= SMMBF;
	}

	/*
	**  Kill any seek optimization if the buffer is not the
	**  right size.
	**
	**  SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
	*/

	if (size != iosize)
		flags |= SMNPT;

	/*
	**  Fix up the SM_FILE_T fields, and set sm_cleanup for output flush on
	**  exit (since we are buffered in some way).
	*/

	if (mode == SM_IO_LBF)
		flags |= SMLBF;
	else if (mode == SM_IO_NOW)
		flags |= SMNOW;
	else if (mode == SM_IO_FBF)
		flags |= SMFBF;
	fp->f_flags = flags;
	fp->f_bf.smb_base = fp->f_p = (unsigned char *)buf;
	fp->f_bf.smb_size = size;
	/* fp->f_lbfsize is still 0 */
	if (flags & SMWR)
	{
		/*
		**  Begin or continue writing: see sm_wsetup().  Note
		**  that SMNBF is impossible (it was handled earlier).
		*/

		if (flags & SMLBF)
		{
			fp->f_w = 0;
			fp->f_lbfsize = -fp->f_bf.smb_size;
		}
		else
			fp->f_w = size;
	}
	else
	{
		/* begin/continue reading, or stay in intermediate state */
		fp->f_w = 0;
	}

	atexit(sm_cleanup);
	return ret;
}
OpenPOWER on IntegriCloud