xref: /minix3/lib/libc/stdio/setvbuf.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: setvbuf.c,v 1.19 2012/03/15 18:22:30 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Chris Torek.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
33b7061124SArun Thomas  */
34b7061124SArun Thomas 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)setvbuf.c	8.2 (Berkeley) 11/16/93";
392fe8fb19SBen Gras #else
40*f14fb602SLionel Sambuc __RCSID("$NetBSD: setvbuf.c,v 1.19 2012/03/15 18:22:30 christos Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include <assert.h>
452fe8fb19SBen Gras #include <errno.h>
46b7061124SArun Thomas #include <stdio.h>
47b7061124SArun Thomas #include <stdlib.h>
482fe8fb19SBen Gras #include <wchar.h>
492fe8fb19SBen Gras #include "reentrant.h"
502fe8fb19SBen Gras #include "local.h"
51b7061124SArun Thomas 
522fe8fb19SBen Gras /*
532fe8fb19SBen Gras  * Set one of the three kinds of buffering, optionally including
542fe8fb19SBen Gras  * a buffer.
552fe8fb19SBen Gras  */
56b7061124SArun Thomas int
setvbuf(FILE * fp,char * buf,int mode,size_t size)57*f14fb602SLionel Sambuc setvbuf(FILE *fp, char *buf, int mode, size_t size)
58b7061124SArun Thomas {
592fe8fb19SBen Gras 	int ret, flags;
602fe8fb19SBen Gras 	size_t iosize;
612fe8fb19SBen Gras 	int ttyflag;
62b7061124SArun Thomas 
632fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
642fe8fb19SBen Gras 	/* buf may be NULL */
65b7061124SArun Thomas 
662fe8fb19SBen Gras 	/*
672fe8fb19SBen Gras 	 * Verify arguments.  The `int' limit on `size' is due to this
682fe8fb19SBen Gras 	 * particular implementation.  Note, buf and size are ignored
692fe8fb19SBen Gras 	 * when setting _IONBF.
702fe8fb19SBen Gras 	 */
712fe8fb19SBen Gras 	if (mode != _IONBF)
722fe8fb19SBen Gras 		if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
73*f14fb602SLionel Sambuc 			return -1;
74b7061124SArun Thomas 
752fe8fb19SBen Gras 	FLOCKFILE(fp);
762fe8fb19SBen Gras 	/*
772fe8fb19SBen Gras 	 * Write current buffer, if any.  Discard unread input (including
782fe8fb19SBen Gras 	 * ungetc data), cancel line buffering, and free old buffer if
792fe8fb19SBen Gras 	 * malloc()ed.  We also clear any eof condition, as if this were
802fe8fb19SBen Gras 	 * a seek.
812fe8fb19SBen Gras 	 */
822fe8fb19SBen Gras 	ret = 0;
832fe8fb19SBen Gras 	(void)__sflush(fp);
842fe8fb19SBen Gras 	if (HASUB(fp))
852fe8fb19SBen Gras 		FREEUB(fp);
862fe8fb19SBen Gras 	WCIO_FREE(fp);
872fe8fb19SBen Gras 	fp->_r = fp->_lbfsize = 0;
882fe8fb19SBen Gras 	flags = fp->_flags;
892fe8fb19SBen Gras 	if (flags & __SMBF)
902fe8fb19SBen Gras 		free((void *)fp->_bf._base);
912fe8fb19SBen Gras 	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF);
92b7061124SArun Thomas 
932fe8fb19SBen Gras 	/* If setting unbuffered mode, skip all the hard work. */
942fe8fb19SBen Gras 	if (mode == _IONBF)
952fe8fb19SBen Gras 		goto nbf;
962fe8fb19SBen Gras 
972fe8fb19SBen Gras 	/*
982fe8fb19SBen Gras 	 * Find optimal I/O size for seek optimization.  This also returns
992fe8fb19SBen Gras 	 * a `tty flag' to suggest that we check isatty(fd), but we do not
1002fe8fb19SBen Gras 	 * care since our caller told us how to buffer.
1012fe8fb19SBen Gras 	 */
1022fe8fb19SBen Gras 	flags |= __swhatbuf(fp, &iosize, &ttyflag);
1032fe8fb19SBen Gras 	if (size == 0) {
1042fe8fb19SBen Gras 		buf = NULL;	/* force local allocation */
1052fe8fb19SBen Gras 		size = iosize;
1062fe8fb19SBen Gras 	}
1072fe8fb19SBen Gras 
1082fe8fb19SBen Gras 	/* Allocate buffer if needed. */
1092fe8fb19SBen Gras 	if (buf == NULL) {
1102fe8fb19SBen Gras 		if ((buf = malloc(size)) == NULL) {
1112fe8fb19SBen Gras 			/*
1122fe8fb19SBen Gras 			 * Unable to honor user's request.  We will return
1132fe8fb19SBen Gras 			 * failure, but try again with file system size.
1142fe8fb19SBen Gras 			 */
1152fe8fb19SBen Gras 			ret = -1;
1162fe8fb19SBen Gras 			if (size != iosize) {
1172fe8fb19SBen Gras 				size = iosize;
1182fe8fb19SBen Gras 				buf = malloc(size);
1192fe8fb19SBen Gras 			}
1202fe8fb19SBen Gras 		}
1212fe8fb19SBen Gras 		if (buf == NULL) {
1222fe8fb19SBen Gras 			/* No luck; switch to unbuffered I/O. */
1232fe8fb19SBen Gras nbf:
1242fe8fb19SBen Gras 			fp->_flags = flags | __SNBF;
1252fe8fb19SBen Gras 			fp->_w = 0;
1262fe8fb19SBen Gras 			fp->_bf._base = fp->_p = fp->_nbuf;
1272fe8fb19SBen Gras 			fp->_bf._size = 1;
1282fe8fb19SBen Gras 			FUNLOCKFILE(fp);
129*f14fb602SLionel Sambuc 			return ret;
1302fe8fb19SBen Gras 		}
1312fe8fb19SBen Gras 		flags |= __SMBF;
1322fe8fb19SBen Gras 	}
1332fe8fb19SBen Gras 
1342fe8fb19SBen Gras 	/*
1352fe8fb19SBen Gras 	 * Kill any seek optimization if the buffer is not the
1362fe8fb19SBen Gras 	 * right size.
1372fe8fb19SBen Gras 	 *
1382fe8fb19SBen Gras 	 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
1392fe8fb19SBen Gras 	 */
1402fe8fb19SBen Gras 	if (size != iosize)
1412fe8fb19SBen Gras 		flags |= __SNPT;
1422fe8fb19SBen Gras 
1432fe8fb19SBen Gras 	/*
1442fe8fb19SBen Gras 	 * Fix up the FILE fields, and set __cleanup for output flush on
1452fe8fb19SBen Gras 	 * exit (since we are buffered in some way).
1462fe8fb19SBen Gras 	 */
1472fe8fb19SBen Gras 	if (mode == _IOLBF)
1482fe8fb19SBen Gras 		flags |= __SLBF;
1492fe8fb19SBen Gras 	fp->_flags = flags;
1502fe8fb19SBen Gras 	fp->_bf._base = fp->_p = (unsigned char *)buf;
151*f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int, size));
152*f14fb602SLionel Sambuc 	fp->_bf._size = (int)size;
1532fe8fb19SBen Gras 	/* fp->_lbfsize is still 0 */
1542fe8fb19SBen Gras 	if (flags & __SWR) {
1552fe8fb19SBen Gras 		/*
1562fe8fb19SBen Gras 		 * Begin or continue writing: see __swsetup().  Note
1572fe8fb19SBen Gras 		 * that __SNBF is impossible (it was handled earlier).
1582fe8fb19SBen Gras 		 */
1592fe8fb19SBen Gras 		if (flags & __SLBF) {
1602fe8fb19SBen Gras 			fp->_w = 0;
1612fe8fb19SBen Gras 			fp->_lbfsize = -fp->_bf._size;
162*f14fb602SLionel Sambuc 		} else {
163*f14fb602SLionel Sambuc 			_DIAGASSERT(__type_fit(int, size));
164*f14fb602SLionel Sambuc 			fp->_w = (int)size;
165*f14fb602SLionel Sambuc 		}
166b7061124SArun Thomas 	} else {
1672fe8fb19SBen Gras 		/* begin/continue reading, or stay in intermediate state */
1682fe8fb19SBen Gras 		fp->_w = 0;
169b7061124SArun Thomas 	}
1702fe8fb19SBen Gras 	__cleanup = _cleanup;
171b7061124SArun Thomas 
1722fe8fb19SBen Gras 	FUNLOCKFILE(fp);
173*f14fb602SLionel Sambuc 	return ret;
174b7061124SArun Thomas }
175