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