1 | /* |
---|
2 | * Copyright (c) 1990 The Regents of the University of California. |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms are permitted |
---|
6 | * provided that the above copyright notice and this paragraph are |
---|
7 | * duplicated in all such forms and that any documentation, |
---|
8 | * advertising materials, and other materials related to such |
---|
9 | * distribution and use acknowledge that the software was developed |
---|
10 | * by the University of California, Berkeley. The name of the |
---|
11 | * University may not be used to endorse or promote products derived |
---|
12 | * from this software without specific prior written permission. |
---|
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
---|
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
---|
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
16 | */ |
---|
17 | /* No user fns here. Pesch 15apr92. */ |
---|
18 | |
---|
19 | #include <_ansi.h> |
---|
20 | #include <stdio.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <errno.h> |
---|
23 | #include "local.h" |
---|
24 | |
---|
25 | static int |
---|
26 | lflush (FILE *fp) |
---|
27 | { |
---|
28 | if ((fp->_flags & (__SLBF | __SWR)) == (__SLBF | __SWR)) |
---|
29 | return fflush (fp); |
---|
30 | return 0; |
---|
31 | } |
---|
32 | |
---|
33 | /* |
---|
34 | * Refill a stdio buffer. |
---|
35 | * Return EOF on eof or error, 0 otherwise. |
---|
36 | */ |
---|
37 | |
---|
38 | int |
---|
39 | __srefill_r (struct _reent * ptr, |
---|
40 | register FILE * fp) |
---|
41 | { |
---|
42 | /* make sure stdio is set up */ |
---|
43 | |
---|
44 | CHECK_INIT (ptr, fp); |
---|
45 | |
---|
46 | ORIENT (fp, -1); |
---|
47 | |
---|
48 | fp->_r = 0; /* largely a convenience for callers */ |
---|
49 | |
---|
50 | #ifndef __CYGWIN__ |
---|
51 | /* SysV does not make this test; take it out for compatibility */ |
---|
52 | if (fp->_flags & __SEOF) |
---|
53 | return EOF; |
---|
54 | #endif |
---|
55 | |
---|
56 | /* if not already reading, have to be reading and writing */ |
---|
57 | if ((fp->_flags & __SRD) == 0) |
---|
58 | { |
---|
59 | if ((fp->_flags & __SRW) == 0) |
---|
60 | { |
---|
61 | ptr->_errno = EBADF; |
---|
62 | fp->_flags |= __SERR; |
---|
63 | return EOF; |
---|
64 | } |
---|
65 | /* switch to reading */ |
---|
66 | if (fp->_flags & __SWR) |
---|
67 | { |
---|
68 | if (_fflush_r (ptr, fp)) |
---|
69 | return EOF; |
---|
70 | fp->_flags &= ~__SWR; |
---|
71 | fp->_w = 0; |
---|
72 | fp->_lbfsize = 0; |
---|
73 | } |
---|
74 | fp->_flags |= __SRD; |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | /* |
---|
79 | * We were reading. If there is an ungetc buffer, |
---|
80 | * we must have been reading from that. Drop it, |
---|
81 | * restoring the previous buffer (if any). If there |
---|
82 | * is anything in that buffer, return. |
---|
83 | */ |
---|
84 | if (HASUB (fp)) |
---|
85 | { |
---|
86 | FREEUB (ptr, fp); |
---|
87 | if ((fp->_r = fp->_ur) != 0) |
---|
88 | { |
---|
89 | fp->_p = fp->_up; |
---|
90 | return 0; |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | if (fp->_bf._base == NULL) |
---|
96 | __smakebuf_r (ptr, fp); |
---|
97 | |
---|
98 | /* |
---|
99 | * Before reading from a line buffered or unbuffered file, |
---|
100 | * flush all line buffered output files, per the ANSI C |
---|
101 | * standard. |
---|
102 | */ |
---|
103 | if (fp->_flags & (__SLBF | __SNBF)) |
---|
104 | { |
---|
105 | /* Ignore this file in _fwalk to avoid potential deadlock. */ |
---|
106 | short orig_flags = fp->_flags; |
---|
107 | fp->_flags = 1; |
---|
108 | (void) _fwalk (_GLOBAL_REENT, lflush); |
---|
109 | fp->_flags = orig_flags; |
---|
110 | |
---|
111 | /* Now flush this file without locking it. */ |
---|
112 | if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR)) |
---|
113 | __sflush_r (ptr, fp); |
---|
114 | } |
---|
115 | |
---|
116 | fp->_p = fp->_bf._base; |
---|
117 | fp->_r = fp->_read (ptr, fp->_cookie, (char *) fp->_p, fp->_bf._size); |
---|
118 | #ifndef __CYGWIN__ |
---|
119 | if (fp->_r <= 0) |
---|
120 | #else |
---|
121 | if (fp->_r > 0) |
---|
122 | fp->_flags &= ~__SEOF; |
---|
123 | else |
---|
124 | #endif |
---|
125 | { |
---|
126 | if (fp->_r == 0) |
---|
127 | fp->_flags |= __SEOF; |
---|
128 | else |
---|
129 | { |
---|
130 | fp->_r = 0; |
---|
131 | fp->_flags |= __SERR; |
---|
132 | } |
---|
133 | return EOF; |
---|
134 | } |
---|
135 | return 0; |
---|
136 | } |
---|