xref: /onnv-gate/usr/src/ucblib/libucb/port/gen/setbuffer.c (revision 1846:376b8b33ed65)
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  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate /*
250Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
260Sstevel@tonic-gate  * The Regents of the University of California
270Sstevel@tonic-gate  * All Rights Reserved
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
300Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
310Sstevel@tonic-gate  * contributors.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
34*1846Scraigm /*
35*1846Scraigm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
36*1846Scraigm  * Use is subject to license terms.
37*1846Scraigm  */
38*1846Scraigm 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*LINTLIBRARY*/
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include "file64.h"
450Sstevel@tonic-gate #include <stdio.h>
460Sstevel@tonic-gate #include "stdiom.h"
470Sstevel@tonic-gate #include <stdlib.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate extern Uchar _smbuf[][_NFILE];
500Sstevel@tonic-gate 
510Sstevel@tonic-gate void
setbuffer(FILE * iop,char * abuf,int asize)520Sstevel@tonic-gate setbuffer(FILE *iop, char *abuf, int asize)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	Uchar *buf = (Uchar *)abuf;
55*1846Scraigm 	int fno = fileno(iop);  /* file number */
560Sstevel@tonic-gate 	int size = asize - _SMBFSZ;
570Sstevel@tonic-gate 	Uchar *temp;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	if (iop->_base != 0 && iop->_flag & _IOMYBUF)
600Sstevel@tonic-gate 		free((char *)iop->_base - PUSHBACK);
610Sstevel@tonic-gate 	iop->_flag &= ~(_IOMYBUF | _IONBF | _IOLBF);
620Sstevel@tonic-gate 	if (buf == 0) {
630Sstevel@tonic-gate 		iop->_flag |= _IONBF;
640Sstevel@tonic-gate #ifndef _STDIO_ALLOCATE
650Sstevel@tonic-gate 		if (fno < 2) {
660Sstevel@tonic-gate 			/* use special buffer for std{in,out} */
670Sstevel@tonic-gate 			buf = (fno == 0) ? _sibuf : _sobuf;
680Sstevel@tonic-gate 			size = BUFSIZ - _SMBFSZ;
690Sstevel@tonic-gate 		} else /* needed for ifdef */
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 		if (fno < _NFILE) {
720Sstevel@tonic-gate 			buf = _smbuf[fno];
730Sstevel@tonic-gate 			size = _SMBFSZ - PUSHBACK;
740Sstevel@tonic-gate 		} else if ((buf = (Uchar *)malloc(_SMBFSZ * sizeof (Uchar))) !=
750Sstevel@tonic-gate 		    0) {
760Sstevel@tonic-gate 			iop->_flag |= _IOMYBUF;
770Sstevel@tonic-gate 			size = _SMBFSZ - PUSHBACK;
780Sstevel@tonic-gate 		}
790Sstevel@tonic-gate 	} else /* regular buffered I/O, specified buffer size */ {
800Sstevel@tonic-gate 		if (size <= 0)
810Sstevel@tonic-gate 			return;
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	if (buf == 0)
840Sstevel@tonic-gate 		return; /* malloc() failed */
850Sstevel@tonic-gate 	temp = buf + PUSHBACK;
860Sstevel@tonic-gate 	iop->_base = temp;
870Sstevel@tonic-gate 	_setbufend(iop, temp + size);
880Sstevel@tonic-gate 	iop->_ptr = temp;
890Sstevel@tonic-gate 	iop->_cnt = 0;
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * set line buffering
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate 
960Sstevel@tonic-gate int
setlinebuf(FILE * iop)970Sstevel@tonic-gate setlinebuf(FILE *iop)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	char *buf;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	(void) fflush(iop);
1020Sstevel@tonic-gate 	setbuffer(iop, (char *)NULL, 0);
1030Sstevel@tonic-gate 	buf = (char *)malloc(128);
1040Sstevel@tonic-gate 	if (buf != NULL) {
1050Sstevel@tonic-gate 		setbuffer(iop, buf, 128);
1060Sstevel@tonic-gate 		iop->_flag |= _IOLBF|_IOMYBUF;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	return (0);	/* returns no useful value, keep the same prototype */
1090Sstevel@tonic-gate }
110