4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 #define _LARGEFILE64_SOURCE 1
39 #include <sys/types.h>
44 * If buffer space has been pre-allocated use it otherwise malloc space.
45 * PUSHBACK causes the base pointer to be bumped forward. At least 4 bytes
46 * of pushback are required to meet international specifications.
47 * Extra space at the end of the buffer allows for synchronization problems.
48 * If malloc() fails stdio bails out; assumption being the system is in trouble.
49 * Associate a buffer with stream; return NULL on error.
60 struct stat64 stbuf
; /* used to get file system block size */
62 if (iop
->_flag
& _IONBF
) /* need a small buffer, at least */
65 size
= _SMBFSZ
- PUSHBACK
;
68 else if ((buf
= (Uchar
*)malloc(_SMBFSZ
* sizeof (Uchar
))) !=
70 iop
->_flag
|= _IOMYBUF
;
72 #ifndef _STDIO_ALLOCATE
73 else if (fd
< 2 && (tty
= isatty(fd
))) {
74 buf
= (fd
== 0) ? _sibuf
: _sobuf
; /* special buffer */
80 * The operating system can tell us the
81 * right size for a buffer;
82 * avoid 0-size buffers as returned for some
83 * special files (doors)
85 if (fstat64(fd
, &stbuf
) == 0 && stbuf
.st_blksize
> 0)
86 size
= stbuf
.st_blksize
;
88 if ((buf
= (Uchar
*)malloc(sizeof (Uchar
)*(size
+_SMBFSZ
))) !=
90 iop
->_flag
|= _IOMYBUF
;
95 return (NULL
); /* malloc() failed */
96 iop
->_base
= buf
+ PUSHBACK
; /* bytes for pushback */
97 iop
->_ptr
= buf
+ PUSHBACK
;
98 endbuf
= iop
->_base
+ size
;
99 _setbufend(iop
, endbuf
);
100 if (!(iop
->_flag
& _IONBF
) && ((tty
!= -1) ? tty
: isatty(fd
)))
101 iop
->_flag
|= _IOLBF
;