/*	Copyright (c) 1984, 1986, 1987, 1988 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"@(#)libc-port:stdio/setvbuf.c	1.10.2.1"
/*LINTLIBRARY*/
#include <stdio.h>

extern void free();

int
setvbuf(iop, buf, type, size)
register FILE *iop;
register int type;
register char	*buf;
register int size;
{
	char *malloc();

	switch(type) {
	    case _IONBF:
		    break;
	    case _IOLBF:
	    case _IOFBF:
		    if(size > 0)
			break;
	    default:
		    return(-1);
	}
	if(iop->_base != NULL && iop->_flag & _IOMYBUF)
		free((char*)iop->_base);
	iop->_flag &= ~(_IOMYBUF | _IONBF | _IOLBF);
	iop->_flag |= type;
	switch (type)  {
	    /*note that the flags are the same as the possible values for type*/
	    case _IONBF:
		/* file is unbuffered */
		_bufend(iop) = iop->_base = NULL;
		break;
	    case _IOLBF:
	    case _IOFBF:
		/*
		 * need eight character beyond bufend for stdio slop
		 */
		if (size <= 8) {
 		    size = BUFSIZ;
		    buf = NULL;
		}
		if (buf == NULL) {
		    iop->_base = (unsigned char *)
					malloc((unsigned)(size + 8));
		    iop->_flag |= _IOMYBUF;
		}
		else
		    iop->_base = (unsigned char *) buf;
		_bufend(iop) = iop->_base + size;
		break;
	}
	iop->_ptr = iop->_base;
	iop->_cnt = 0;
	return 0;
}
