match glibc/lsb cancellation abi on i386
[musl] / src / stdio / __underflow.c
1 #include "stdio_impl.h"
2
3 int __underflow(FILE *f)
4 {
5         ssize_t cnt;
6
7         /* Read from buffer (Do we ever get called when this is true??) */
8         if (f->rpos < f->rstop) return *f->rpos;
9
10         /* Initialize if we're not already reading */
11         if (!f->rstop) {
12                 /* Fail immediately if unreadable, eof, or error state. */
13                 if (f->flags & (F_EOF|F_ERR|F_NORD)) return EOF;
14
15                 /* Set byte orientation -1,0=>-1; 1=>1 */
16                 f->mode |= f->mode-1;
17
18                 /* Flush any unwritten output; fail on error. */
19                 if (f->wpos > f->buf && __oflow(f)) return EOF;
20
21                 /* Disallow writes to buffer. */
22                 f->wstop = 0;
23         }
24
25         /* Perform the underlying read operation */
26         if ((cnt=f->read(f, f->buf, f->buf_size)) + 1 <= 1) {
27                 /* Set flags and leave read mode */
28                 f->flags |= F_EOF | (cnt & F_ERR);
29                 f->rpos = f->rend = f->rstop = 0;
30                 return EOF;
31         }
32
33         /* Setup buffer pointers for reading from buffer */
34         f->rpos = f->buf;
35         f->rend = f->rstop = f->buf + cnt;
36
37         return *f->rpos;
38 }