BSD 1 development
[unix-history] / portlib / cgetc.c
CommitLineData
520d5775
EA
1# include "iodec.h"
2
3/**
4 ** get a single character
5 **/
6
7int __backup 0;
8
9cgetc(xfn)
10int xfn;
11{
12 register struct fileps *fp;
13 register int i;
14 extern int f_log;
15 extern int cin;
16 register int fn;
17
18 fn = xfn;
19 if (fn < 0 || fn >= MAXFILES)
20 __error("cgetc: illegal file number %d", fn);
21 fp = &__filehdr[fn];
22 if (fp->wrflag == 2)
23 __error("cgetc: file %d not open to read", fn);
24 if (fp->wrflag == 0)
25 __makbuf(fn, 0);
26 if (fp->eoferr)
27 return ('\0');
28 if (fp->nchars == 0)
29 {
30 i = read(fn, fp->bptr = fp->buff, fp->bsize);
31 if (i < 0)
32 __error("cgetc: read error on %d", fn);
33 if (i == 0)
34 {
35 fp->eoferr = 2;
36 return ('\0');
37 }
38 fp->nchars = i;
39 }
40 fp->nchars--;
41 if (f_log > 0 && fn == cin)
42 if (__backup)
43 __backup--;
44 else
45 cputc(*fp->bptr, f_log);
46 return(*fp->bptr++);
47}