xref: /titanic_52/usr/src/lib/libbc/libc/stdio/4.2/filbuf.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1986 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*      Copyright (c) 1984 AT&T */
28 /*        All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"  /* from S5R2 2.1 */
31 
32 
33 /*LINTLIBRARY*/
34 #include <stdio.h>
35 
36 extern _findbuf();
37 extern int read();
38 extern int fflush();
39 
40 int
41 __filbuf(iop)
42 register FILE *iop;
43 {
44 	return (_filbuf(iop));
45 }
46 
47 int
48 _filbuf(iop)
49 register FILE *iop;
50 {
51 
52 	if ( !(iop->_flag & _IOREAD) )
53 		if (iop->_flag & _IORW)
54 			iop->_flag |= _IOREAD;
55 		else
56 			return(EOF);
57 
58 	if (iop->_flag&(_IOSTRG|_IOEOF))
59 		return(EOF);
60 
61 	if (iop->_base == NULL)  /* get buffer if we don't have one */
62 		_findbuf(iop);
63 
64 	if (iop == stdin) {
65 		if (stdout->_flag&_IOLBF)
66 			(void) fflush(stdout);
67 		if (stderr->_flag&_IOLBF)
68 			(void) fflush(stderr);
69 	}
70 
71 	iop->_ptr = iop->_base;
72 	iop->_cnt = read(fileno(iop), (char *)iop->_base,
73 	    (unsigned)((iop->_flag & _IONBF) ? 1 : iop->_bufsiz ));
74 	if (--iop->_cnt >= 0)		/* success */
75 		return (*iop->_ptr++);
76 	if (iop->_cnt != -1)		/* error */
77 		iop->_flag |= _IOERR;
78 	else {				/* end-of-file */
79 		iop->_flag |= _IOEOF;
80 		if (iop->_flag & _IORW)
81 			iop->_flag &= ~_IOREAD;
82 	}
83 	iop->_cnt = 0;
84 	return (EOF);
85 }
86