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