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