xref: /illumos-gate/usr/src/lib/libc/port/stdio/_filbuf.c (revision cd62a92d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a5f69788Scraigm  * Common Development and Distribution License (the "License").
6a5f69788Scraigm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a5f69788Scraigm 
227c478bd9Sstevel@tonic-gate /*
23a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*cd62a92dSRobert Mustacchi /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307257d1b4Sraf #pragma weak __filbuf = _filbuf
317c478bd9Sstevel@tonic-gate 
327257d1b4Sraf #include "lint.h"
337c478bd9Sstevel@tonic-gate #include "file64.h"
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include "stdiom.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static int
_xpg4_check(void)427c478bd9Sstevel@tonic-gate _xpg4_check(void)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	extern int	__xpg4;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	return (__xpg4);
477c478bd9Sstevel@tonic-gate }
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* fill buffer, return first character or EOF */
507c478bd9Sstevel@tonic-gate int
_filbuf(FILE * iop)517c478bd9Sstevel@tonic-gate _filbuf(FILE *iop)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	ssize_t res;
547c478bd9Sstevel@tonic-gate 	size_t nbyte;
557c478bd9Sstevel@tonic-gate 	Uchar *endbuf;
567c478bd9Sstevel@tonic-gate #ifdef	_LP64
577c478bd9Sstevel@tonic-gate 	unsigned int	flag;
587c478bd9Sstevel@tonic-gate #else
597c478bd9Sstevel@tonic-gate 	unsigned char	flag;
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate 
62*cd62a92dSRobert Mustacchi 	if (!(iop->_flag & _IOREAD)) {	/* check, correct permissions */
63*cd62a92dSRobert Mustacchi 		if (iop->_flag & _IORW) {
64*cd62a92dSRobert Mustacchi 			iop->_flag |= _IOREAD;  /* change direction */
657c478bd9Sstevel@tonic-gate 						/* to read - fseek */
66*cd62a92dSRobert Mustacchi 		} else {
677c478bd9Sstevel@tonic-gate 			errno = EBADF;
687c478bd9Sstevel@tonic-gate 			return (EOF);
697c478bd9Sstevel@tonic-gate 		}
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (iop->_base == 0) {
73*cd62a92dSRobert Mustacchi 		/* Get the buffer and end of buffer */
74*cd62a92dSRobert Mustacchi 		if ((endbuf = _findbuf(iop)) == 0) {
757c478bd9Sstevel@tonic-gate 			return (EOF);
76*cd62a92dSRobert Mustacchi 		}
77*cd62a92dSRobert Mustacchi 	} else {
787c478bd9Sstevel@tonic-gate 		endbuf = _bufend(iop);
79*cd62a92dSRobert Mustacchi 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	/*
827c478bd9Sstevel@tonic-gate 	 * Flush all line-buffered streams before we
837c478bd9Sstevel@tonic-gate 	 * read no-buffered or line-buffered input.
847c478bd9Sstevel@tonic-gate 	 */
857c478bd9Sstevel@tonic-gate 	if (iop->_flag & (_IONBF | _IOLBF))
867c478bd9Sstevel@tonic-gate 		_flushlbf();
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * Changed the get family fns in Solaris 10 to comply with the
897c478bd9Sstevel@tonic-gate 	 * 1990 C Standard and standards based upon it.  If the
907c478bd9Sstevel@tonic-gate 	 * end-of-file indicator for the stream is set, or if the stream
917c478bd9Sstevel@tonic-gate 	 * is at end-of-file, the function will return EOF, and the file
927c478bd9Sstevel@tonic-gate 	 * position indicator for the stream will not be advanced.
937c478bd9Sstevel@tonic-gate 	 * Additional bytes appended to the file do not clear the EOF
947c478bd9Sstevel@tonic-gate 	 * indicator.
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	if ((flag = iop->_flag) & _IOEOF) {
977c478bd9Sstevel@tonic-gate 		if (_xpg4_check()) {
987c478bd9Sstevel@tonic-gate 			/*
997c478bd9Sstevel@tonic-gate 			 * A previous read() has returned 0 (below),
1007c478bd9Sstevel@tonic-gate 			 * therefore iop->_cnt was set to 0, and the EOF
1017c478bd9Sstevel@tonic-gate 			 * indicator was set before returning EOF.  Reset
1027c478bd9Sstevel@tonic-gate 			 * iop->_cnt to 0; it has likely been changed by
1037c478bd9Sstevel@tonic-gate 			 * a function such as getc().
1047c478bd9Sstevel@tonic-gate 			 */
1057c478bd9Sstevel@tonic-gate 			iop->_cnt = 0;
1067c478bd9Sstevel@tonic-gate 			return (EOF);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 	}
109*cd62a92dSRobert Mustacchi 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * Fill buffer or read 1 byte for unbuffered, handling any errors.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	iop->_ptr = iop->_base;
1147c478bd9Sstevel@tonic-gate 	if (flag & _IONBF)
1157c478bd9Sstevel@tonic-gate 		nbyte = 1;
1167c478bd9Sstevel@tonic-gate 	else
1177c478bd9Sstevel@tonic-gate 		nbyte = endbuf - iop->_base;
118*cd62a92dSRobert Mustacchi 	if ((res = _xread(iop, (char *)iop->_base, nbyte)) > 0) {
1197c478bd9Sstevel@tonic-gate 		iop->_cnt = res - 1;
1207c478bd9Sstevel@tonic-gate 		return (*iop->_ptr++);
1217c478bd9Sstevel@tonic-gate 	}
122a574db85Sraf 
123a574db85Sraf 	iop->_cnt = 0;
124a574db85Sraf 	if (res == 0)
125a574db85Sraf 		iop->_flag |= _IOEOF;
126a574db85Sraf 	else if (!cancel_active())
127a574db85Sraf 		iop->_flag |= _IOERR;
128a574db85Sraf 	return (EOF);
1297c478bd9Sstevel@tonic-gate }
130