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 /*
235891Sraf * 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
33*6812Sraf #pragma weak __filbuf = _filbuf
340Sstevel@tonic-gate
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include "file64.h"
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include "stdiom.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate
440Sstevel@tonic-gate static int
_xpg4_check(void)450Sstevel@tonic-gate _xpg4_check(void)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate extern int __xpg4;
480Sstevel@tonic-gate
490Sstevel@tonic-gate return (__xpg4);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate
520Sstevel@tonic-gate /* fill buffer, return first character or EOF */
530Sstevel@tonic-gate int
_filbuf(FILE * iop)540Sstevel@tonic-gate _filbuf(FILE *iop)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate ssize_t res;
570Sstevel@tonic-gate size_t nbyte;
580Sstevel@tonic-gate Uchar *endbuf;
590Sstevel@tonic-gate #ifdef _LP64
600Sstevel@tonic-gate unsigned int flag;
610Sstevel@tonic-gate #else
620Sstevel@tonic-gate unsigned char flag;
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate
650Sstevel@tonic-gate if (!(iop->_flag & _IOREAD)) /* check, correct permissions */
660Sstevel@tonic-gate {
670Sstevel@tonic-gate if (iop->_flag & _IORW)
680Sstevel@tonic-gate iop->_flag |= _IOREAD; /* change direction */
690Sstevel@tonic-gate /* to read - fseek */
700Sstevel@tonic-gate else {
710Sstevel@tonic-gate errno = EBADF;
720Sstevel@tonic-gate return (EOF);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
760Sstevel@tonic-gate if (iop->_base == 0) {
770Sstevel@tonic-gate if ((endbuf = _findbuf(iop)) == 0) /* get buffer and */
780Sstevel@tonic-gate /* end_of_buffer */
790Sstevel@tonic-gate return (EOF);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate else
820Sstevel@tonic-gate endbuf = _bufend(iop);
830Sstevel@tonic-gate
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate * Flush all line-buffered streams before we
860Sstevel@tonic-gate * read no-buffered or line-buffered input.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate if (iop->_flag & (_IONBF | _IOLBF))
890Sstevel@tonic-gate _flushlbf();
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * Changed the get family fns in Solaris 10 to comply with the
920Sstevel@tonic-gate * 1990 C Standard and standards based upon it. If the
930Sstevel@tonic-gate * end-of-file indicator for the stream is set, or if the stream
940Sstevel@tonic-gate * is at end-of-file, the function will return EOF, and the file
950Sstevel@tonic-gate * position indicator for the stream will not be advanced.
960Sstevel@tonic-gate * Additional bytes appended to the file do not clear the EOF
970Sstevel@tonic-gate * indicator.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate if ((flag = iop->_flag) & _IOEOF) {
1000Sstevel@tonic-gate if (_xpg4_check()) {
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * A previous read() has returned 0 (below),
1030Sstevel@tonic-gate * therefore iop->_cnt was set to 0, and the EOF
1040Sstevel@tonic-gate * indicator was set before returning EOF. Reset
1050Sstevel@tonic-gate * iop->_cnt to 0; it has likely been changed by
1060Sstevel@tonic-gate * a function such as getc().
1070Sstevel@tonic-gate */
1080Sstevel@tonic-gate iop->_cnt = 0;
1090Sstevel@tonic-gate return (EOF);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * Fill buffer or read 1 byte for unbuffered, handling any errors.
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate iop->_ptr = iop->_base;
1160Sstevel@tonic-gate if (flag & _IONBF)
1170Sstevel@tonic-gate nbyte = 1;
1180Sstevel@tonic-gate else
1190Sstevel@tonic-gate nbyte = endbuf - iop->_base;
1201846Scraigm if ((res = read(GET_FD(iop), (char *)iop->_base, nbyte)) > 0) {
1210Sstevel@tonic-gate iop->_cnt = res - 1;
1220Sstevel@tonic-gate return (*iop->_ptr++);
1230Sstevel@tonic-gate }
1245891Sraf
1255891Sraf iop->_cnt = 0;
1265891Sraf if (res == 0)
1275891Sraf iop->_flag |= _IOEOF;
1285891Sraf else if (!cancel_active())
1295891Sraf iop->_flag |= _IOERR;
1305891Sraf return (EOF);
1310Sstevel@tonic-gate }
132