xref: /csrg-svn/lib/libc/stdio/setvbuf.c (revision 64906)
146111Sbostic /*-
261180Sbostic  * Copyright (c) 1990, 1993
361180Sbostic  *	The Regents of the University of California.  All rights reserved.
446111Sbostic  *
546111Sbostic  * This code is derived from software contributed to Berkeley by
646111Sbostic  * Chris Torek.
746111Sbostic  *
846111Sbostic  * %sccs.include.redist.c%
946111Sbostic  */
1046111Sbostic 
1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*64906Storek static char sccsid[] = "@(#)setvbuf.c	8.2 (Berkeley) 11/16/93";
1346111Sbostic #endif /* LIBC_SCCS and not lint */
1446111Sbostic 
1546111Sbostic #include <stdio.h>
1646111Sbostic #include <stdlib.h>
1746111Sbostic #include "local.h"
1846111Sbostic 
1946111Sbostic /*
2046111Sbostic  * Set one of the three kinds of buffering, optionally including
2146111Sbostic  * a buffer.
2246111Sbostic  */
setvbuf(fp,buf,mode,size)2346111Sbostic setvbuf(fp, buf, mode, size)
2446111Sbostic 	register FILE *fp;
2546111Sbostic 	char *buf;
2646111Sbostic 	register int mode;
2746111Sbostic 	register size_t size;
2846111Sbostic {
2956990Storek 	register int ret, flags;
3059778Storek 	size_t iosize;
3159778Storek 	int ttyflag;
3246111Sbostic 
3346111Sbostic 	/*
3446111Sbostic 	 * Verify arguments.  The `int' limit on `size' is due to this
3559778Storek 	 * particular implementation.  Note, buf and size are ignored
3659778Storek 	 * when setting _IONBF.
3746111Sbostic 	 */
3859778Storek 	if (mode != _IONBF)
3959778Storek 		if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
4059778Storek 			return (EOF);
4146111Sbostic 
4246111Sbostic 	/*
43*64906Storek 	 * Write current buffer, if any.  Discard unread input (including
44*64906Storek 	 * ungetc data), cancel line buffering, and free old buffer if
45*64906Storek 	 * malloc()ed.  We also clear any eof condition, as if this were
46*64906Storek 	 * a seek.
4746111Sbostic 	 */
4856990Storek 	ret = 0;
4959778Storek 	(void)__sflush(fp);
50*64906Storek 	if (HASUB(fp))
51*64906Storek 		FREEUB(fp);
5259778Storek 	fp->_r = fp->_lbfsize = 0;
5356990Storek 	flags = fp->_flags;
5456990Storek 	if (flags & __SMBF)
5546111Sbostic 		free((void *)fp->_bf._base);
56*64906Storek 	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF);
5746111Sbostic 
5859778Storek 	/* If setting unbuffered mode, skip all the hard work. */
5959778Storek 	if (mode == _IONBF)
6059778Storek 		goto nbf;
6159778Storek 
6259778Storek 	/*
6359778Storek 	 * Find optimal I/O size for seek optimization.  This also returns
6459778Storek 	 * a `tty flag' to suggest that we check isatty(fd), but we do not
6559778Storek 	 * care since our caller told us how to buffer.
6659778Storek 	 */
6759778Storek 	flags |= __swhatbuf(fp, &iosize, &ttyflag);
6859778Storek 	if (size == 0) {
6959778Storek 		buf = NULL;	/* force local allocation */
7059778Storek 		size = iosize;
7159778Storek 	}
7259778Storek 
7359778Storek 	/* Allocate buffer if needed. */
7459778Storek 	if (buf == NULL) {
7556990Storek 		if ((buf = malloc(size)) == NULL) {
7659778Storek 			/*
7759778Storek 			 * Unable to honor user's request.  We will return
7859778Storek 			 * failure, but try again with file system size.
7959778Storek 			 */
8056990Storek 			ret = EOF;
8159778Storek 			if (size != iosize) {
8259778Storek 				size = iosize;
8359778Storek 				buf = malloc(size);
8459778Storek 			}
8559778Storek 		}
8659778Storek 		if (buf == NULL) {
8759778Storek 			/* No luck; switch to unbuffered I/O. */
8859778Storek nbf:
8959778Storek 			fp->_flags = flags | __SNBF;
9059778Storek 			fp->_w = 0;
9159778Storek 			fp->_bf._base = fp->_p = fp->_nbuf;
9259778Storek 			fp->_bf._size = 1;
9359778Storek 			return (ret);
9459778Storek 		}
9559778Storek 		flags |= __SMBF;
9656990Storek 	}
9756990Storek 
9846111Sbostic 	/*
9959778Storek 	 * Kill any seek optimization if the buffer is not the
10059778Storek 	 * right size.
10159778Storek 	 *
10259778Storek 	 * SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
10346111Sbostic 	 */
10459778Storek 	if (size != iosize)
10559778Storek 		flags |= __SNPT;
10646111Sbostic 
10759778Storek 	/*
10859778Storek 	 * Fix up the FILE fields, and set __cleanup for output flush on
109*64906Storek 	 * exit (since we are buffered in some way).
11059778Storek 	 */
11160232Storek 	if (mode == _IOLBF)
11256990Storek 		flags |= __SLBF;
11356990Storek 	fp->_flags = flags;
11459778Storek 	fp->_bf._base = fp->_p = (unsigned char *)buf;
11559778Storek 	fp->_bf._size = size;
116*64906Storek 	/* fp->_lbfsize is still 0 */
117*64906Storek 	if (flags & __SWR) {
118*64906Storek 		/*
119*64906Storek 		 * Begin or continue writing: see __swsetup().  Note
120*64906Storek 		 * that __SNBF is impossible (it was handled earlier).
121*64906Storek 		 */
122*64906Storek 		if (flags & __SLBF) {
123*64906Storek 			fp->_w = 0;
124*64906Storek 			fp->_lbfsize = -fp->_bf._size;
125*64906Storek 		} else
126*64906Storek 			fp->_w = size;
127*64906Storek 	} else {
128*64906Storek 		/* begin/continue reading, or stay in intermediate state */
129*64906Storek 		fp->_w = 0;
130*64906Storek 	}
13159778Storek 	__cleanup = _cleanup;
13246111Sbostic 
13356990Storek 	return (ret);
13446111Sbostic }
135