xref: /freebsd/lib/libc/stdio/setvbuf.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
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 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)setvbuf.c	8.2 (Berkeley) 11/16/93";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 #include "namespace.h"
40 #include <limits.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include "un-namespace.h"
44 #include "local.h"
45 #include "libc_private.h"
46 
47 /*
48  * Set one of the three kinds of buffering, optionally including
49  * a buffer.
50  */
51 int
52 setvbuf(FILE * __restrict fp, char * __restrict buf, int mode, size_t size)
53 {
54 	int ret, flags;
55 	size_t iosize;
56 	int ttyflag;
57 
58 	/*
59 	 * Verify arguments.  The `int' limit on `size' is due to this
60 	 * particular implementation.  Note, buf and size are ignored
61 	 * when setting _IONBF.
62 	 */
63 	if (mode != _IONBF)
64 		if ((mode != _IOFBF && mode != _IOLBF) || size > INT_MAX)
65 			return (EOF);
66 
67 	FLOCKFILE_CANCELSAFE(fp);
68 	/*
69 	 * Write current buffer, if any.  Discard unread input (including
70 	 * ungetc data), cancel line buffering, and free old buffer if
71 	 * malloc()ed.  We also clear any eof condition, as if this were
72 	 * a seek.
73 	 */
74 	ret = 0;
75 	(void)__sflush(fp);
76 	if (HASUB(fp))
77 		FREEUB(fp);
78 	fp->_r = fp->_lbfsize = 0;
79 	flags = fp->_flags;
80 	if (flags & __SMBF)
81 		free((void *)fp->_bf._base);
82 	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SOFF | __SNPT | __SEOF);
83 
84 	/* If setting unbuffered mode, skip all the hard work. */
85 	if (mode == _IONBF)
86 		goto nbf;
87 
88 	/*
89 	 * Find optimal I/O size for seek optimization.  This also returns
90 	 * a `tty flag' to suggest that we check isatty(fd), but we do not
91 	 * care since our caller told us how to buffer.
92 	 */
93 	flags |= __swhatbuf(fp, &iosize, &ttyflag);
94 	if (size == 0) {
95 		buf = NULL;	/* force local allocation */
96 		size = iosize;
97 	}
98 
99 	/* Allocate buffer if needed. */
100 	if (buf == NULL) {
101 		if ((buf = malloc(size)) == NULL) {
102 			/*
103 			 * Unable to honor user's request.  We will return
104 			 * failure, but try again with file system size.
105 			 */
106 			ret = EOF;
107 			if (size != iosize) {
108 				size = iosize;
109 				buf = malloc(size);
110 			}
111 		}
112 		if (buf == NULL) {
113 			/* No luck; switch to unbuffered I/O. */
114 nbf:
115 			fp->_flags = flags | __SNBF;
116 			fp->_w = 0;
117 			fp->_bf._base = fp->_p = fp->_nbuf;
118 			fp->_bf._size = 1;
119 			goto end;
120 		}
121 		flags |= __SMBF;
122 	}
123 
124 	/*
125 	 * Kill any seek optimization if the buffer is not the
126 	 * right size.
127 	 *
128 	 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
129 	 */
130 	if (size != iosize)
131 		flags |= __SNPT;
132 
133 	/*
134 	 * Fix up the FILE fields, and set __cleanup for output flush on
135 	 * exit (since we are buffered in some way).
136 	 */
137 	if (mode == _IOLBF)
138 		flags |= __SLBF;
139 	fp->_flags = flags;
140 	fp->_bf._base = fp->_p = (unsigned char *)buf;
141 	fp->_bf._size = size;
142 	/* fp->_lbfsize is still 0 */
143 	if (flags & __SWR) {
144 		/*
145 		 * Begin or continue writing: see __swsetup().  Note
146 		 * that __SNBF is impossible (it was handled earlier).
147 		 */
148 		if (flags & __SLBF) {
149 			fp->_w = 0;
150 			fp->_lbfsize = -fp->_bf._size;
151 		} else
152 			fp->_w = size;
153 	} else {
154 		/* begin/continue reading, or stay in intermediate state */
155 		fp->_w = 0;
156 	}
157 	__cleanup = _cleanup;
158 
159 end:
160 	FUNLOCKFILE_CANCELSAFE();
161 	return (ret);
162 }
163