10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1846Scraigm  * Common Development and Distribution License (the "License").
6*1846Scraigm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*1846Scraigm 
220Sstevel@tonic-gate /*
23*1846Scraigm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*1846Scraigm #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*1846Scraigm 
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "synonyms.h"
340Sstevel@tonic-gate #include "file64.h"
350Sstevel@tonic-gate #include "mtlib.h"
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <thread.h>
400Sstevel@tonic-gate #include <synch.h>
410Sstevel@tonic-gate #include "stdiom.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate int
440Sstevel@tonic-gate setvbuf(FILE *iop, char *abuf, int type, size_t size)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 
470Sstevel@tonic-gate 	Uchar	*buf = (Uchar *)abuf;
480Sstevel@tonic-gate 	Uchar *temp;
490Sstevel@tonic-gate 	int	sflag = iop->_flag & _IOMYBUF;
500Sstevel@tonic-gate 	rmutex_t *lk;
51*1846Scraigm 	int fd = GET_FD(iop);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
540Sstevel@tonic-gate 	iop->_flag &= ~(_IOMYBUF | _IONBF | _IOLBF);
550Sstevel@tonic-gate 	switch (type) {
560Sstevel@tonic-gate 	/* note that the flags are the same as the possible values for type */
570Sstevel@tonic-gate 	case _IONBF:
580Sstevel@tonic-gate 		iop->_flag |= _IONBF;	 /* file is unbuffered */
590Sstevel@tonic-gate #ifndef _STDIO_ALLOCATE
60*1846Scraigm 		if (fd < 2) {
610Sstevel@tonic-gate 			/* use special buffer for std{in,out} */
62*1846Scraigm 			buf = (fd == 0) ? _sibuf : _sobuf;
630Sstevel@tonic-gate 			size = BUFSIZ;
640Sstevel@tonic-gate 		} else /* needed for ifdef */
650Sstevel@tonic-gate #endif
66*1846Scraigm 		if (fd < _NFILE) {
67*1846Scraigm 			buf = _smbuf[fd];
680Sstevel@tonic-gate 			size = _SMBFSZ - PUSHBACK;
690Sstevel@tonic-gate 		} else
700Sstevel@tonic-gate 			if ((buf = malloc(_SMBFSZ * sizeof (Uchar))) != NULL) {
710Sstevel@tonic-gate 				iop->_flag |= _IOMYBUF;
720Sstevel@tonic-gate 				size = _SMBFSZ - PUSHBACK;
730Sstevel@tonic-gate 			} else {
740Sstevel@tonic-gate 				FUNLOCKFILE(lk);
750Sstevel@tonic-gate 				return (EOF);
760Sstevel@tonic-gate 			}
770Sstevel@tonic-gate 		break;
780Sstevel@tonic-gate 	case _IOLBF:
790Sstevel@tonic-gate 	case _IOFBF:
800Sstevel@tonic-gate 		iop->_flag |= type;	/* buffer file */
810Sstevel@tonic-gate 		/*
820Sstevel@tonic-gate 		 * need at least an 8 character buffer for
830Sstevel@tonic-gate 		 * out_of_sync concerns.
840Sstevel@tonic-gate 		 */
850Sstevel@tonic-gate 		if (size <= _SMBFSZ) {
860Sstevel@tonic-gate 			size = BUFSIZ;
870Sstevel@tonic-gate 			buf = NULL;
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 		if (buf == NULL) {
900Sstevel@tonic-gate 			if ((buf = malloc(sizeof (Uchar) *
910Sstevel@tonic-gate 			    (size + _SMBFSZ))) != NULL)
920Sstevel@tonic-gate 				iop->_flag |= _IOMYBUF;
930Sstevel@tonic-gate 			else {
940Sstevel@tonic-gate 				FUNLOCKFILE(lk);
950Sstevel@tonic-gate 				return (EOF);
960Sstevel@tonic-gate 			}
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 		else
990Sstevel@tonic-gate 			size -= _SMBFSZ;
1000Sstevel@tonic-gate 		break;
1010Sstevel@tonic-gate 	default:
1020Sstevel@tonic-gate 		FUNLOCKFILE(lk);
1030Sstevel@tonic-gate 		return (EOF);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	if (iop->_base != NULL && sflag)
1060Sstevel@tonic-gate 		free((char *)iop->_base - PUSHBACK);
1070Sstevel@tonic-gate 	temp = buf + PUSHBACK;
1080Sstevel@tonic-gate 	iop->_base = temp;
1090Sstevel@tonic-gate 	_setbufend(iop, temp + size);
1100Sstevel@tonic-gate 	iop->_ptr = temp;
1110Sstevel@tonic-gate 	iop->_cnt = 0;
1120Sstevel@tonic-gate 	FUNLOCKFILE(lk);
1130Sstevel@tonic-gate 	return (0);
1140Sstevel@tonic-gate }
115