xref: /dragonfly/lib/libc/stdio/setvbuf.c (revision 9348a738)
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  * @(#)setvbuf.c	8.2 (Berkeley) 11/16/93
33  * $FreeBSD: src/lib/libc/stdio/setvbuf.c,v 1.14 2007/01/09 00:28:07 imp Exp $
34  */
35 
36 #include "namespace.h"
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "un-namespace.h"
40 #include "local.h"
41 #include "libc_private.h"
42 
43 /*
44  * Set one of the three kinds of buffering, optionally including
45  * a buffer.
46  */
47 int
48 setvbuf(FILE * __restrict fp, char * __restrict buf, int mode, size_t size)
49 {
50 	int ret, flags;
51 	size_t iosize;
52 	int ttyflag;
53 
54 	/*
55 	 * Verify arguments.  The `int' limit on `size' is due to this
56 	 * particular implementation.  Note, buf and size are ignored
57 	 * when setting _IONBF.
58 	 */
59 	if (mode != _IONBF)
60 		if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
61 			return (EOF);
62 
63 	FLOCKFILE(fp);
64 	/*
65 	 * Write current buffer, if any.  Discard unread input (including
66 	 * ungetc data), cancel line buffering, and free old buffer if
67 	 * malloc()ed.  We also clear any eof condition, as if this were
68 	 * a seek.
69 	 */
70 	ret = 0;
71 	__sflush(fp);
72 	if (HASUB(fp))
73 		FREEUB(fp);
74 	fp->pub._r = fp->pub._lbfsize = 0;
75 	flags = fp->pub._flags;
76 	if (flags & __SMBF)
77 		free((void *)fp->_bf._base);
78 	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SOFF | __SNPT | __SEOF);
79 
80 	/* If setting unbuffered mode, skip all the hard work. */
81 	if (mode == _IONBF)
82 		goto nbf;
83 
84 	/*
85 	 * Find optimal I/O size for seek optimization.  This also returns
86 	 * a `tty flag' to suggest that we check isatty(fd), but we do not
87 	 * care since our caller told us how to buffer.
88 	 */
89 	flags |= __swhatbuf(fp, &iosize, &ttyflag);
90 	if (size == 0) {
91 		buf = NULL;	/* force local allocation */
92 		size = iosize;
93 	}
94 
95 	/* Allocate buffer if needed. */
96 	if (buf == NULL) {
97 		if ((buf = malloc(size)) == NULL) {
98 			/*
99 			 * Unable to honor user's request.  We will return
100 			 * failure, but try again with file system size.
101 			 */
102 			ret = EOF;
103 			if (size != iosize) {
104 				size = iosize;
105 				buf = malloc(size);
106 			}
107 		}
108 		if (buf == NULL) {
109 			/* No luck; switch to unbuffered I/O. */
110 nbf:
111 			fp->pub._flags = flags | __SNBF;
112 			fp->pub._w = 0;
113 			fp->_bf._base = fp->pub._p = fp->_nbuf;
114 			fp->_bf._size = 1;
115 			FUNLOCKFILE(fp);
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->pub._flags = flags;
137 	fp->_bf._base = fp->pub._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->pub._w = 0;
147 			fp->pub._lbfsize = -fp->_bf._size;
148 		} else
149 			fp->pub._w = size;
150 	} else {
151 		/* begin/continue reading, or stay in intermediate state */
152 		fp->pub._w = 0;
153 	}
154 	__cleanup = _cleanup;
155 
156 	FUNLOCKFILE(fp);
157 	return (ret);
158 }
159