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