11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Chris Torek.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
16249808Semaste * 3. Neither the name of the University nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
341573Srgrimesstatic char sccsid[] = "@(#)setvbuf.c	8.2 (Berkeley) 11/16/93";
351573Srgrimes#endif /* LIBC_SCCS and not lint */
3692986Sobrien#include <sys/cdefs.h>
3792986Sobrien__FBSDID("$FreeBSD: stable/10/lib/libc/stdio/setvbuf.c 321074 2017-07-17 14:09:34Z kib $");
381573Srgrimes
3971579Sdeischen#include "namespace.h"
401573Srgrimes#include <stdio.h>
411573Srgrimes#include <stdlib.h>
4271579Sdeischen#include "un-namespace.h"
431573Srgrimes#include "local.h"
4435129Sjb#include "libc_private.h"
451573Srgrimes
461573Srgrimes/*
471573Srgrimes * Set one of the three kinds of buffering, optionally including
481573Srgrimes * a buffer.
491573Srgrimes */
5013545Sjulianint
51103012Stjrsetvbuf(FILE * __restrict fp, char * __restrict buf, int mode, size_t size)
521573Srgrimes{
5392889Sobrien	int ret, flags;
541573Srgrimes	size_t iosize;
551573Srgrimes	int ttyflag;
561573Srgrimes
571573Srgrimes	/*
581573Srgrimes	 * Verify arguments.  The `int' limit on `size' is due to this
591573Srgrimes	 * particular implementation.  Note, buf and size are ignored
601573Srgrimes	 * when setting _IONBF.
611573Srgrimes	 */
621573Srgrimes	if (mode != _IONBF)
631573Srgrimes		if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
641573Srgrimes			return (EOF);
651573Srgrimes
66321074Skib	FLOCKFILE_CANCELSAFE(fp);
671573Srgrimes	/*
681573Srgrimes	 * Write current buffer, if any.  Discard unread input (including
691573Srgrimes	 * ungetc data), cancel line buffering, and free old buffer if
701573Srgrimes	 * malloc()ed.  We also clear any eof condition, as if this were
711573Srgrimes	 * a seek.
721573Srgrimes	 */
731573Srgrimes	ret = 0;
741573Srgrimes	(void)__sflush(fp);
751573Srgrimes	if (HASUB(fp))
761573Srgrimes		FREEUB(fp);
771573Srgrimes	fp->_r = fp->_lbfsize = 0;
781573Srgrimes	flags = fp->_flags;
791573Srgrimes	if (flags & __SMBF)
801573Srgrimes		free((void *)fp->_bf._base);
8182839Sache	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SOFF | __SNPT | __SEOF);
821573Srgrimes
831573Srgrimes	/* If setting unbuffered mode, skip all the hard work. */
841573Srgrimes	if (mode == _IONBF)
851573Srgrimes		goto nbf;
861573Srgrimes
871573Srgrimes	/*
881573Srgrimes	 * Find optimal I/O size for seek optimization.  This also returns
891573Srgrimes	 * a `tty flag' to suggest that we check isatty(fd), but we do not
901573Srgrimes	 * care since our caller told us how to buffer.
911573Srgrimes	 */
921573Srgrimes	flags |= __swhatbuf(fp, &iosize, &ttyflag);
931573Srgrimes	if (size == 0) {
941573Srgrimes		buf = NULL;	/* force local allocation */
951573Srgrimes		size = iosize;
961573Srgrimes	}
971573Srgrimes
981573Srgrimes	/* Allocate buffer if needed. */
991573Srgrimes	if (buf == NULL) {
1001573Srgrimes		if ((buf = malloc(size)) == NULL) {
1011573Srgrimes			/*
1021573Srgrimes			 * Unable to honor user's request.  We will return
1031573Srgrimes			 * failure, but try again with file system size.
1041573Srgrimes			 */
1051573Srgrimes			ret = EOF;
1061573Srgrimes			if (size != iosize) {
1071573Srgrimes				size = iosize;
1081573Srgrimes				buf = malloc(size);
1091573Srgrimes			}
1101573Srgrimes		}
1111573Srgrimes		if (buf == NULL) {
1121573Srgrimes			/* No luck; switch to unbuffered I/O. */
1131573Srgrimesnbf:
1141573Srgrimes			fp->_flags = flags | __SNBF;
1151573Srgrimes			fp->_w = 0;
1161573Srgrimes			fp->_bf._base = fp->_p = fp->_nbuf;
1171573Srgrimes			fp->_bf._size = 1;
118321074Skib			goto end;
1191573Srgrimes		}
1201573Srgrimes		flags |= __SMBF;
1211573Srgrimes	}
1221573Srgrimes
1231573Srgrimes	/*
1241573Srgrimes	 * Kill any seek optimization if the buffer is not the
1251573Srgrimes	 * right size.
1261573Srgrimes	 *
1271573Srgrimes	 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
1281573Srgrimes	 */
1291573Srgrimes	if (size != iosize)
1301573Srgrimes		flags |= __SNPT;
1311573Srgrimes
1321573Srgrimes	/*
1331573Srgrimes	 * Fix up the FILE fields, and set __cleanup for output flush on
1341573Srgrimes	 * exit (since we are buffered in some way).
1351573Srgrimes	 */
1361573Srgrimes	if (mode == _IOLBF)
1371573Srgrimes		flags |= __SLBF;
1381573Srgrimes	fp->_flags = flags;
1391573Srgrimes	fp->_bf._base = fp->_p = (unsigned char *)buf;
1401573Srgrimes	fp->_bf._size = size;
1411573Srgrimes	/* fp->_lbfsize is still 0 */
1421573Srgrimes	if (flags & __SWR) {
1431573Srgrimes		/*
1441573Srgrimes		 * Begin or continue writing: see __swsetup().  Note
1451573Srgrimes		 * that __SNBF is impossible (it was handled earlier).
1461573Srgrimes		 */
1471573Srgrimes		if (flags & __SLBF) {
1481573Srgrimes			fp->_w = 0;
1491573Srgrimes			fp->_lbfsize = -fp->_bf._size;
1501573Srgrimes		} else
1511573Srgrimes			fp->_w = size;
1521573Srgrimes	} else {
1531573Srgrimes		/* begin/continue reading, or stay in intermediate state */
1541573Srgrimes		fp->_w = 0;
1551573Srgrimes	}
1561573Srgrimes	__cleanup = _cleanup;
1571573Srgrimes
158321074Skibend:
159321074Skib	FUNLOCKFILE_CANCELSAFE();
1601573Srgrimes	return (ret);
1611573Srgrimes}
162