xref: /netbsd-src/lib/libc/stdio/setvbuf.c (revision 526d942790cc352b6ea7931f9884cc6bdbf20404)
1*526d9427Schristos /*	$NetBSD: setvbuf.c,v 1.19 2012/03/15 18:22:30 christos Exp $	*/
2255db7b2Sjtc 
361f28255Scgd /*-
4255db7b2Sjtc  * Copyright (c) 1990, 1993
5255db7b2Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Chris Torek.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
350c339c44Schristos #include <sys/cdefs.h>
3661f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
37255db7b2Sjtc #if 0
38255db7b2Sjtc static char sccsid[] = "@(#)setvbuf.c	8.2 (Berkeley) 11/16/93";
390c339c44Schristos #else
40*526d9427Schristos __RCSID("$NetBSD: setvbuf.c,v 1.19 2012/03/15 18:22:30 christos Exp $");
41255db7b2Sjtc #endif
4261f28255Scgd #endif /* LIBC_SCCS and not lint */
4361f28255Scgd 
44b48252f3Slukem #include <assert.h>
45b48252f3Slukem #include <errno.h>
4661f28255Scgd #include <stdio.h>
4761f28255Scgd #include <stdlib.h>
4817f3654aSyamt #include <wchar.h>
49b559f98bSjtc #include "reentrant.h"
503fdac2b8Sthorpej #include "local.h"
5161f28255Scgd 
5261f28255Scgd /*
5361f28255Scgd  * Set one of the three kinds of buffering, optionally including
5461f28255Scgd  * a buffer.
5561f28255Scgd  */
56da8d0ce8Sjtc int
setvbuf(FILE * fp,char * buf,int mode,size_t size)57*526d9427Schristos setvbuf(FILE *fp, char *buf, int mode, size_t size)
5861f28255Scgd {
59c8bafd62Sperry 	int ret, flags;
60e9fa4a0dScgd 	size_t iosize;
61e9fa4a0dScgd 	int ttyflag;
6261f28255Scgd 
63b48252f3Slukem 	_DIAGASSERT(fp != NULL);
64b48252f3Slukem 	/* buf may be NULL */
65b48252f3Slukem 
6661f28255Scgd 	/*
6761f28255Scgd 	 * Verify arguments.  The `int' limit on `size' is due to this
68e9fa4a0dScgd 	 * particular implementation.  Note, buf and size are ignored
69e9fa4a0dScgd 	 * when setting _IONBF.
7061f28255Scgd 	 */
71e9fa4a0dScgd 	if (mode != _IONBF)
72e9fa4a0dScgd 		if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
73*526d9427Schristos 			return -1;
7461f28255Scgd 
75b559f98bSjtc 	FLOCKFILE(fp);
7661f28255Scgd 	/*
77527838aaScgd 	 * Write current buffer, if any.  Discard unread input (including
78527838aaScgd 	 * ungetc data), cancel line buffering, and free old buffer if
79527838aaScgd 	 * malloc()ed.  We also clear any eof condition, as if this were
80527838aaScgd 	 * a seek.
8161f28255Scgd 	 */
82e9fa4a0dScgd 	ret = 0;
8361f28255Scgd 	(void)__sflush(fp);
84527838aaScgd 	if (HASUB(fp))
85527838aaScgd 		FREEUB(fp);
8617f3654aSyamt 	WCIO_FREE(fp);
87e9fa4a0dScgd 	fp->_r = fp->_lbfsize = 0;
88e9fa4a0dScgd 	flags = fp->_flags;
89e9fa4a0dScgd 	if (flags & __SMBF)
9061f28255Scgd 		free((void *)fp->_bf._base);
91527838aaScgd 	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF);
92e9fa4a0dScgd 
93e9fa4a0dScgd 	/* If setting unbuffered mode, skip all the hard work. */
94e9fa4a0dScgd 	if (mode == _IONBF)
95e9fa4a0dScgd 		goto nbf;
9661f28255Scgd 
9761f28255Scgd 	/*
98e9fa4a0dScgd 	 * Find optimal I/O size for seek optimization.  This also returns
99e9fa4a0dScgd 	 * a `tty flag' to suggest that we check isatty(fd), but we do not
100e9fa4a0dScgd 	 * care since our caller told us how to buffer.
10161f28255Scgd 	 */
102e9fa4a0dScgd 	flags |= __swhatbuf(fp, &iosize, &ttyflag);
103e9fa4a0dScgd 	if (size == 0) {
104e9fa4a0dScgd 		buf = NULL;	/* force local allocation */
105e9fa4a0dScgd 		size = iosize;
106e9fa4a0dScgd 	}
10761f28255Scgd 
108e9fa4a0dScgd 	/* Allocate buffer if needed. */
109e9fa4a0dScgd 	if (buf == NULL) {
110e9fa4a0dScgd 		if ((buf = malloc(size)) == NULL) {
111e9fa4a0dScgd 			/*
112e9fa4a0dScgd 			 * Unable to honor user's request.  We will return
113e9fa4a0dScgd 			 * failure, but try again with file system size.
114e9fa4a0dScgd 			 */
115f47d490fSkleink 			ret = -1;
116e9fa4a0dScgd 			if (size != iosize) {
117e9fa4a0dScgd 				size = iosize;
118e9fa4a0dScgd 				buf = malloc(size);
119e9fa4a0dScgd 			}
120e9fa4a0dScgd 		}
121e9fa4a0dScgd 		if (buf == NULL) {
122e9fa4a0dScgd 			/* No luck; switch to unbuffered I/O. */
123e9fa4a0dScgd nbf:
124e9fa4a0dScgd 			fp->_flags = flags | __SNBF;
125e9fa4a0dScgd 			fp->_w = 0;
12661f28255Scgd 			fp->_bf._base = fp->_p = fp->_nbuf;
12761f28255Scgd 			fp->_bf._size = 1;
128b559f98bSjtc 			FUNLOCKFILE(fp);
129*526d9427Schristos 			return ret;
130e9fa4a0dScgd 		}
131e9fa4a0dScgd 		flags |= __SMBF;
13261f28255Scgd 	}
13361f28255Scgd 
13461f28255Scgd 	/*
135e9fa4a0dScgd 	 * Kill any seek optimization if the buffer is not the
136e9fa4a0dScgd 	 * right size.
137e9fa4a0dScgd 	 *
138e9fa4a0dScgd 	 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
13961f28255Scgd 	 */
140e9fa4a0dScgd 	if (size != iosize)
141e9fa4a0dScgd 		flags |= __SNPT;
14261f28255Scgd 
143e9fa4a0dScgd 	/*
144e9fa4a0dScgd 	 * Fix up the FILE fields, and set __cleanup for output flush on
145527838aaScgd 	 * exit (since we are buffered in some way).
146e9fa4a0dScgd 	 */
14727c29363Scgd 	if (mode == _IOLBF)
148e9fa4a0dScgd 		flags |= __SLBF;
149e9fa4a0dScgd 	fp->_flags = flags;
150e9fa4a0dScgd 	fp->_bf._base = fp->_p = (unsigned char *)buf;
151c5e820caSchristos 	_DIAGASSERT(__type_fit(int, size));
152c5e820caSchristos 	fp->_bf._size = (int)size;
153527838aaScgd 	/* fp->_lbfsize is still 0 */
154527838aaScgd 	if (flags & __SWR) {
155527838aaScgd 		/*
156527838aaScgd 		 * Begin or continue writing: see __swsetup().  Note
157527838aaScgd 		 * that __SNBF is impossible (it was handled earlier).
158527838aaScgd 		 */
159527838aaScgd 		if (flags & __SLBF) {
160527838aaScgd 			fp->_w = 0;
161527838aaScgd 			fp->_lbfsize = -fp->_bf._size;
162c5e820caSchristos 		} else {
163c5e820caSchristos 			_DIAGASSERT(__type_fit(int, size));
164c5e820caSchristos 			fp->_w = (int)size;
165c5e820caSchristos 		}
166527838aaScgd 	} else {
167527838aaScgd 		/* begin/continue reading, or stay in intermediate state */
168527838aaScgd 		fp->_w = 0;
169527838aaScgd 	}
170e9fa4a0dScgd 	__cleanup = _cleanup;
171e9fa4a0dScgd 
172b559f98bSjtc 	FUNLOCKFILE(fp);
173*526d9427Schristos 	return ret;
17461f28255Scgd }
175