xref: /openbsd-src/lib/libc/stdio/refill.c (revision 33b4f39fbeffad07bc3206f173cff9f3c9901cd1)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Torek.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char rcsid[] = "$OpenBSD: refill.c,v 1.6 2003/06/02 20:18:37 millert Exp $";
35 #endif /* LIBC_SCCS and not lint */
36 
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "local.h"
41 
42 static int
43 lflush(fp)
44 	FILE *fp;
45 {
46 
47 	if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR))
48 		return (__sflush(fp));
49 	return (0);
50 }
51 
52 /*
53  * Refill a stdio buffer.
54  * Return EOF on eof or error, 0 otherwise.
55  */
56 int
57 __srefill(fp)
58 	register FILE *fp;
59 {
60 
61 	/* make sure stdio is set up */
62 	if (!__sdidinit)
63 		__sinit();
64 
65 	fp->_r = 0;		/* largely a convenience for callers */
66 
67 	/* SysV does not make this test; take it out for compatibility */
68 	if (fp->_flags & __SEOF)
69 		return (EOF);
70 
71 	/* if not already reading, have to be reading and writing */
72 	if ((fp->_flags & __SRD) == 0) {
73 		if ((fp->_flags & __SRW) == 0) {
74 			errno = EBADF;
75 			fp->_flags |= __SERR;
76 			return (EOF);
77 		}
78 		/* switch to reading */
79 		if (fp->_flags & __SWR) {
80 			if (__sflush(fp))
81 				return (EOF);
82 			fp->_flags &= ~__SWR;
83 			fp->_w = 0;
84 			fp->_lbfsize = 0;
85 		}
86 		fp->_flags |= __SRD;
87 	} else {
88 		/*
89 		 * We were reading.  If there is an ungetc buffer,
90 		 * we must have been reading from that.  Drop it,
91 		 * restoring the previous buffer (if any).  If there
92 		 * is anything in that buffer, return.
93 		 */
94 		if (HASUB(fp)) {
95 			FREEUB(fp);
96 			if ((fp->_r = fp->_ur) != 0) {
97 				fp->_p = fp->_up;
98 				return (0);
99 			}
100 		}
101 	}
102 
103 	if (fp->_bf._base == NULL)
104 		__smakebuf(fp);
105 
106 	/*
107 	 * Before reading from a line buffered or unbuffered file,
108 	 * flush all line buffered output files, per the ANSI C
109 	 * standard.
110 	 */
111 	if (fp->_flags & (__SLBF|__SNBF))
112 		(void) _fwalk(lflush);
113 	fp->_p = fp->_bf._base;
114 	fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size);
115 	fp->_flags &= ~__SMOD;	/* buffer contents are again pristine */
116 	if (fp->_r <= 0) {
117 		if (fp->_r == 0)
118 			fp->_flags |= __SEOF;
119 		else {
120 			fp->_r = 0;
121 			fp->_flags |= __SERR;
122 		}
123 		return (EOF);
124 	}
125 	return (0);
126 }
127