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
430Sstevel@tonic-gate void
setbuf(FILE * iop,char * abuf)440Sstevel@tonic-gate setbuf(FILE *iop, char *abuf)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate Uchar *buf = (Uchar *)abuf;
471846Scraigm int fno = GET_FD(iop); /* file number */
480Sstevel@tonic-gate int size = BUFSIZ - _SMBFSZ;
490Sstevel@tonic-gate Uchar *temp;
500Sstevel@tonic-gate rmutex_t *lk;
510Sstevel@tonic-gate
520Sstevel@tonic-gate FLOCKFILE(lk, iop);
530Sstevel@tonic-gate if ((iop->_base != 0) && (iop->_flag & _IOMYBUF))
540Sstevel@tonic-gate free((char *)iop->_base - PUSHBACK);
550Sstevel@tonic-gate iop->_flag &= ~(_IOMYBUF | _IONBF | _IOLBF);
560Sstevel@tonic-gate if (buf == 0) {
570Sstevel@tonic-gate iop->_flag |= _IONBF;
580Sstevel@tonic-gate #ifndef _STDIO_ALLOCATE
590Sstevel@tonic-gate if (fno < 2) {
600Sstevel@tonic-gate /* use special buffer for std{in,out} */
610Sstevel@tonic-gate buf = (fno == 0) ? _sibuf : _sobuf;
620Sstevel@tonic-gate } else /* needed for ifndef */
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate if (fno < _NFILE) {
650Sstevel@tonic-gate buf = _smbuf[fno];
660Sstevel@tonic-gate size = _SMBFSZ - PUSHBACK;
670Sstevel@tonic-gate } else
680Sstevel@tonic-gate if ((buf = (Uchar *)malloc(_SMBFSZ * sizeof (Uchar))) != 0) {
690Sstevel@tonic-gate iop->_flag |= _IOMYBUF;
700Sstevel@tonic-gate size = _SMBFSZ - PUSHBACK;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate } else { /* regular buffered I/O, standard buffer size */
730Sstevel@tonic-gate if (isatty(fno))
740Sstevel@tonic-gate iop->_flag |= _IOLBF;
750Sstevel@tonic-gate }
760Sstevel@tonic-gate if (buf == 0) {
770Sstevel@tonic-gate FUNLOCKFILE(lk);
780Sstevel@tonic-gate return; /* malloc() failed */
790Sstevel@tonic-gate }
800Sstevel@tonic-gate temp = buf + PUSHBACK;
810Sstevel@tonic-gate iop->_base = temp;
820Sstevel@tonic-gate _setbufend(iop, temp + size);
830Sstevel@tonic-gate iop->_ptr = temp;
840Sstevel@tonic-gate iop->_cnt = 0;
850Sstevel@tonic-gate FUNLOCKFILE(lk);
860Sstevel@tonic-gate }
87