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