xref: /titanic_41/usr/src/lib/libbc/libc/stdio/4.2/filbuf.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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"
31 
32 
33 /*LINTLIBRARY*/
34 #include <stdio.h>
35 #include <unistd.h>
36 
37 extern void	_findbuf();
38 extern int	fflush();
39 
40 int
41 __filbuf(FILE *iop)
42 {
43 	return (_filbuf(iop));
44 }
45 
46 int
47 _filbuf(FILE *iop)
48 {
49 
50 	if ( !(iop->_flag & _IOREAD) )
51 		if (iop->_flag & _IORW)
52 			iop->_flag |= _IOREAD;
53 		else
54 			return(EOF);
55 
56 	if (iop->_flag&(_IOSTRG|_IOEOF))
57 		return(EOF);
58 
59 	if (iop->_base == NULL)  /* get buffer if we don't have one */
60 		_findbuf(iop);
61 
62 	if (iop == stdin) {
63 		if (stdout->_flag&_IOLBF)
64 			(void) fflush(stdout);
65 		if (stderr->_flag&_IOLBF)
66 			(void) fflush(stderr);
67 	}
68 
69 	iop->_ptr = iop->_base;
70 	iop->_cnt = read(fileno(iop), (char *)iop->_base,
71 	    (unsigned)((iop->_flag & _IONBF) ? 1 : iop->_bufsiz ));
72 	if (--iop->_cnt >= 0)		/* success */
73 		return (*iop->_ptr++);
74 	if (iop->_cnt != -1)		/* error */
75 		iop->_flag |= _IOERR;
76 	else {				/* end-of-file */
77 		iop->_flag |= _IOEOF;
78 		if (iop->_flag & _IORW)
79 			iop->_flag &= ~_IOREAD;
80 	}
81 	iop->_cnt = 0;
82 	return (EOF);
83 }
84