scc

A fork of https://git.simple-cc.org/scc/ for Qute

git clone https://git.8pit.net/scc.git

 1#include <errno.h>
 2#include <stdio.h>
 3#include <stdlib.h>
 4
 5#include "../libc.h"
 6#include "../syscall.h"
 7
 8int
 9__getc(FILE *fp)
10{
11	int cnt;
12
13	if (fp->flags & (_IOEOF | _IOERR))
14		return EOF;
15
16	if ((fp->flags & (_IOREAD | _IORW)) == 0) {
17		fp->flags |= _IOERR;
18		errno = EBADF;
19		return EOF;
20	}
21
22	if (fp->flags & _IOSTRG) {
23		fp->flags |= _IOEOF;
24		return EOF;
25	}
26
27	if (fp->buf == NULL && _allocbuf(fp))
28		return EOF;
29
30	if ((cnt = _read(fp->fd, fp->buf, fp->len)) <= 0) {
31		fp->flags |= (cnt == 0) ? _IOEOF : _IOERR;
32		return EOF;
33	}
34
35	fp->flags |= _IOREAD;
36	fp->rp = fp->buf;
37	fp->wp = fp->buf + cnt;
38
39	return *fp->rp++;
40}