xref: /onnv-gate/usr/src/lib/libc/port/stdio/setvbuf.c (revision 6812:febeba71273d)
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
51846Scraigm  * Common Development and Distribution License (the "License").
61846Scraigm  * 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  */
211846Scraigm 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 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 
301846Scraigm #pragma ident	"%Z%%M%	%I%	%E% SMI"
311846Scraigm 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include "file64.h"
340Sstevel@tonic-gate #include "mtlib.h"
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <thread.h>
390Sstevel@tonic-gate #include <synch.h>
400Sstevel@tonic-gate #include "stdiom.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate int
setvbuf(FILE * iop,char * abuf,int type,size_t size)430Sstevel@tonic-gate setvbuf(FILE *iop, char *abuf, int type, size_t size)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	Uchar	*buf = (Uchar *)abuf;
470Sstevel@tonic-gate 	Uchar *temp;
480Sstevel@tonic-gate 	int	sflag = iop->_flag & _IOMYBUF;
490Sstevel@tonic-gate 	rmutex_t *lk;
501846Scraigm 	int fd = GET_FD(iop);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	FLOCKFILE(lk, iop);
530Sstevel@tonic-gate 	iop->_flag &= ~(_IOMYBUF | _IONBF | _IOLBF);
540Sstevel@tonic-gate 	switch (type) {
550Sstevel@tonic-gate 	/* note that the flags are the same as the possible values for type */
560Sstevel@tonic-gate 	case _IONBF:
570Sstevel@tonic-gate 		iop->_flag |= _IONBF;	 /* file is unbuffered */
580Sstevel@tonic-gate #ifndef _STDIO_ALLOCATE
591846Scraigm 		if (fd < 2) {
600Sstevel@tonic-gate 			/* use special buffer for std{in,out} */
611846Scraigm 			buf = (fd == 0) ? _sibuf : _sobuf;
620Sstevel@tonic-gate 			size = BUFSIZ;
630Sstevel@tonic-gate 		} else /* needed for ifdef */
640Sstevel@tonic-gate #endif
651846Scraigm 		if (fd < _NFILE) {
661846Scraigm 			buf = _smbuf[fd];
670Sstevel@tonic-gate 			size = _SMBFSZ - PUSHBACK;
680Sstevel@tonic-gate 		} else
690Sstevel@tonic-gate 			if ((buf = malloc(_SMBFSZ * sizeof (Uchar))) != NULL) {
700Sstevel@tonic-gate 				iop->_flag |= _IOMYBUF;
710Sstevel@tonic-gate 				size = _SMBFSZ - PUSHBACK;
720Sstevel@tonic-gate 			} else {
730Sstevel@tonic-gate 				FUNLOCKFILE(lk);
740Sstevel@tonic-gate 				return (EOF);
750Sstevel@tonic-gate 			}
760Sstevel@tonic-gate 		break;
770Sstevel@tonic-gate 	case _IOLBF:
780Sstevel@tonic-gate 	case _IOFBF:
790Sstevel@tonic-gate 		iop->_flag |= type;	/* buffer file */
800Sstevel@tonic-gate 		/*
810Sstevel@tonic-gate 		 * need at least an 8 character buffer for
820Sstevel@tonic-gate 		 * out_of_sync concerns.
830Sstevel@tonic-gate 		 */
840Sstevel@tonic-gate 		if (size <= _SMBFSZ) {
850Sstevel@tonic-gate 			size = BUFSIZ;
860Sstevel@tonic-gate 			buf = NULL;
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 		if (buf == NULL) {
890Sstevel@tonic-gate 			if ((buf = malloc(sizeof (Uchar) *
900Sstevel@tonic-gate 			    (size + _SMBFSZ))) != NULL)
910Sstevel@tonic-gate 				iop->_flag |= _IOMYBUF;
920Sstevel@tonic-gate 			else {
930Sstevel@tonic-gate 				FUNLOCKFILE(lk);
940Sstevel@tonic-gate 				return (EOF);
950Sstevel@tonic-gate 			}
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 		else
980Sstevel@tonic-gate 			size -= _SMBFSZ;
990Sstevel@tonic-gate 		break;
1000Sstevel@tonic-gate 	default:
1010Sstevel@tonic-gate 		FUNLOCKFILE(lk);
1020Sstevel@tonic-gate 		return (EOF);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	if (iop->_base != NULL && sflag)
1050Sstevel@tonic-gate 		free((char *)iop->_base - PUSHBACK);
1060Sstevel@tonic-gate 	temp = buf + PUSHBACK;
1070Sstevel@tonic-gate 	iop->_base = temp;
1080Sstevel@tonic-gate 	_setbufend(iop, temp + size);
1090Sstevel@tonic-gate 	iop->_ptr = temp;
1100Sstevel@tonic-gate 	iop->_cnt = 0;
1110Sstevel@tonic-gate 	FUNLOCKFILE(lk);
1120Sstevel@tonic-gate 	return (0);
1130Sstevel@tonic-gate }
114