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