1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
32 * SUCH DAMAGE.
33 */
34
35#include "namespace.h"
36#include <limits.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include "un-namespace.h"
40#include "local.h"
41#include "libc_private.h"
42
43/*
44 * Set one of the three kinds of buffering, optionally including
45 * a buffer.
46 */
47int
48setvbuf(FILE * __restrict fp, char * __restrict buf, int mode, size_t size)
49{
50	int ret, flags;
51	size_t iosize;
52	int ttyflag;
53
54	/*
55	 * Verify arguments.  The `int' limit on `size' is due to this
56	 * particular implementation.  Note, buf and size are ignored
57	 * when setting _IONBF.
58	 */
59	if (mode != _IONBF)
60		if ((mode != _IOFBF && mode != _IOLBF) || size > INT_MAX)
61			return (EOF);
62
63	FLOCKFILE_CANCELSAFE(fp);
64	/*
65	 * Write current buffer, if any.  Discard unread input (including
66	 * ungetc data), cancel line buffering, and free old buffer if
67	 * malloc()ed.  We also clear any eof condition, as if this were
68	 * a seek.
69	 */
70	ret = 0;
71	(void)__sflush(fp);
72	if (HASUB(fp))
73		FREEUB(fp);
74	fp->_r = fp->_lbfsize = 0;
75	flags = fp->_flags;
76	if (flags & __SMBF)
77		free((void *)fp->_bf._base);
78	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SOFF | __SNPT | __SEOF);
79
80	/* If setting unbuffered mode, skip all the hard work. */
81	if (mode == _IONBF)
82		goto nbf;
83
84	/*
85	 * Find optimal I/O size for seek optimization.  This also returns
86	 * a `tty flag' to suggest that we check isatty(fd), but we do not
87	 * care since our caller told us how to buffer.
88	 */
89	flags |= __swhatbuf(fp, &iosize, &ttyflag);
90	if (size == 0) {
91		buf = NULL;	/* force local allocation */
92		size = iosize;
93	}
94
95	/* Allocate buffer if needed. */
96	if (buf == NULL) {
97		if ((buf = malloc(size)) == NULL) {
98			/*
99			 * Unable to honor user's request.  We will return
100			 * failure, but try again with file system size.
101			 */
102			ret = EOF;
103			if (size != iosize) {
104				size = iosize;
105				buf = malloc(size);
106			}
107		}
108		if (buf == NULL) {
109			/* No luck; switch to unbuffered I/O. */
110nbf:
111			fp->_flags = flags | __SNBF;
112			fp->_w = 0;
113			fp->_bf._base = fp->_p = fp->_nbuf;
114			fp->_bf._size = 1;
115			goto end;
116		}
117		flags |= __SMBF;
118	}
119
120	/*
121	 * Kill any seek optimization if the buffer is not the
122	 * right size.
123	 *
124	 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
125	 */
126	if (size != iosize)
127		flags |= __SNPT;
128
129	/*
130	 * Fix up the FILE fields, and set __cleanup for output flush on
131	 * exit (since we are buffered in some way).
132	 */
133	if (mode == _IOLBF)
134		flags |= __SLBF;
135	fp->_flags = flags;
136	fp->_bf._base = fp->_p = (unsigned char *)buf;
137	fp->_bf._size = size;
138	/* fp->_lbfsize is still 0 */
139	if (flags & __SWR) {
140		/*
141		 * Begin or continue writing: see __swsetup().  Note
142		 * that __SNBF is impossible (it was handled earlier).
143		 */
144		if (flags & __SLBF) {
145			fp->_w = 0;
146			fp->_lbfsize = -fp->_bf._size;
147		} else
148			fp->_w = size;
149	} else {
150		/* begin/continue reading, or stay in intermediate state */
151		fp->_w = 0;
152	}
153	__cleanup = _cleanup;
154
155end:
156	FUNLOCKFILE_CANCELSAFE();
157	return (ret);
158}
159