xref: /onnv-gate/usr/src/lib/libc/port/stdio/_findbuf.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 
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #define	_LARGEFILE64_SOURCE	1
340Sstevel@tonic-gate 
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include "file64.h"
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <stdio.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include "stdiom.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * If buffer space has been pre-allocated use it otherwise malloc space.
450Sstevel@tonic-gate  * PUSHBACK causes the base pointer to be bumped forward. At least 4 bytes
460Sstevel@tonic-gate  * of pushback are required to meet international specifications.
470Sstevel@tonic-gate  * Extra space at the end of the buffer allows for synchronization problems.
480Sstevel@tonic-gate  * If malloc() fails stdio bails out; assumption being the system is in trouble.
490Sstevel@tonic-gate  * Associate a buffer with stream; return NULL on error.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate Uchar *
_findbuf(FILE * iop)520Sstevel@tonic-gate _findbuf(FILE *iop)
530Sstevel@tonic-gate {
541846Scraigm 	int fd = GET_FD(iop);
550Sstevel@tonic-gate 	Uchar *buf;
560Sstevel@tonic-gate 	int size = BUFSIZ;
570Sstevel@tonic-gate 	Uchar *endbuf;
580Sstevel@tonic-gate 	int tty = -1;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	struct stat64 stbuf;		/* used to get file system block size */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (iop->_flag & _IONBF)	/* need a small buffer, at least */
630Sstevel@tonic-gate 	{
640Sstevel@tonic-gate 	trysmall:;
650Sstevel@tonic-gate 		size = _SMBFSZ - PUSHBACK;
660Sstevel@tonic-gate 		if (fd < _NFILE)
670Sstevel@tonic-gate 			buf = _smbuf[fd];
680Sstevel@tonic-gate 		else if ((buf = (Uchar *)malloc(_SMBFSZ * sizeof (Uchar))) !=
690Sstevel@tonic-gate 		    NULL)
700Sstevel@tonic-gate 			iop->_flag |= _IOMYBUF;
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate #ifndef _STDIO_ALLOCATE
730Sstevel@tonic-gate 	else if (fd < 2 && (tty = isatty(fd))) {
740Sstevel@tonic-gate 		buf = (fd == 0) ? _sibuf : _sobuf; /* special buffer */
750Sstevel@tonic-gate 						/* for std{in,out} */
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate #endif
780Sstevel@tonic-gate 	else {
790Sstevel@tonic-gate 		/*
800Sstevel@tonic-gate 		 * The operating system can tell us the
810Sstevel@tonic-gate 		 * right size for a buffer;
820Sstevel@tonic-gate 		 * avoid 0-size buffers as returned for some
830Sstevel@tonic-gate 		 * special files (doors)
840Sstevel@tonic-gate 		 */
850Sstevel@tonic-gate 		if (fstat64(fd, &stbuf) == 0 && stbuf.st_blksize > 0)
860Sstevel@tonic-gate 			size = stbuf.st_blksize;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 		if ((buf = (Uchar *)malloc(sizeof (Uchar)*(size+_SMBFSZ))) !=
890Sstevel@tonic-gate 		    NULL)
900Sstevel@tonic-gate 			iop->_flag |= _IOMYBUF;
910Sstevel@tonic-gate 		else
920Sstevel@tonic-gate 			goto trysmall;
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 	if (buf == NULL)
950Sstevel@tonic-gate 		return (NULL); 	/* malloc() failed */
960Sstevel@tonic-gate 	iop->_base = buf + PUSHBACK;	/* bytes for pushback */
970Sstevel@tonic-gate 	iop->_ptr = buf + PUSHBACK;
980Sstevel@tonic-gate 	endbuf = iop->_base + size;
990Sstevel@tonic-gate 	_setbufend(iop, endbuf);
1000Sstevel@tonic-gate 	if (!(iop->_flag & _IONBF) && ((tty != -1) ? tty : isatty(fd)))
1010Sstevel@tonic-gate 		iop->_flag |= _IOLBF;
1020Sstevel@tonic-gate 	return (endbuf);
1030Sstevel@tonic-gate }
104