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