summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/stdio/_filbuf.c
blob: 2accbe11583c601982564e874ec9b4766f22004e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved	*/

#pragma weak __filbuf = _filbuf

#include "lint.h"
#include "file64.h"
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include "stdiom.h"


static int
_xpg4_check(void)
{
	extern int	__xpg4;

	return (__xpg4);
}

/* fill buffer, return first character or EOF */
int
_filbuf(FILE *iop)
{
	ssize_t res;
	size_t nbyte;
	Uchar *endbuf;
#ifdef	_LP64
	unsigned int	flag;
#else
	unsigned char	flag;
#endif

	if (!(iop->_flag & _IOREAD)) {	/* check, correct permissions */
		if (iop->_flag & _IORW) {
			iop->_flag |= _IOREAD;  /* change direction */
						/* to read - fseek */
		} else {
			errno = EBADF;
			return (EOF);
		}
	}

	if (iop->_base == 0) {
		/* Get the buffer and end of buffer */
		if ((endbuf = _findbuf(iop)) == 0) {
			return (EOF);
		}
	} else {
		endbuf = _bufend(iop);
	}

	/*
	 * Flush all line-buffered streams before we
	 * read no-buffered or line-buffered input.
	 */
	if (iop->_flag & (_IONBF | _IOLBF))
		_flushlbf();
	/*
	 * Changed the get family fns in Solaris 10 to comply with the
	 * 1990 C Standard and standards based upon it.  If the
	 * end-of-file indicator for the stream is set, or if the stream
	 * is at end-of-file, the function will return EOF, and the file
	 * position indicator for the stream will not be advanced.
	 * Additional bytes appended to the file do not clear the EOF
	 * indicator.
	 */
	if ((flag = iop->_flag) & _IOEOF) {
		if (_xpg4_check()) {
			/*
			 * A previous read() has returned 0 (below),
			 * therefore iop->_cnt was set to 0, and the EOF
			 * indicator was set before returning EOF.  Reset
			 * iop->_cnt to 0; it has likely been changed by
			 * a function such as getc().
			 */
			iop->_cnt = 0;
			return (EOF);
		}
	}

	/*
	 * Fill buffer or read 1 byte for unbuffered, handling any errors.
	 */
	iop->_ptr = iop->_base;
	if (flag & _IONBF)
		nbyte = 1;
	else
		nbyte = endbuf - iop->_base;
	if ((res = _xread(iop, (char *)iop->_base, nbyte)) > 0) {
		iop->_cnt = res - 1;
		return (*iop->_ptr++);
	}

	iop->_cnt = 0;
	if (res == 0)
		iop->_flag |= _IOEOF;
	else if (!cancel_active())
		iop->_flag |= _IOERR;
	return (EOF);
}