]> git.sur5r.net Git - cc65/blob - libsrc/common/fgetc.c
Fixed problems that were introduced with r4287.
[cc65] / libsrc / common / fgetc.c
1 /*
2  * fgetc.c
3  *
4  * (C) Copyright 1998, 2002 Ullrich von Bassewitz (uz@cc65.org)
5  *
6  */
7
8
9
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include "_file.h"
14
15
16
17 /*****************************************************************************/
18 /*                                   Code                                    */
19 /*****************************************************************************/
20
21
22
23 int __fastcall__ fgetc (register FILE* f)
24 {
25     unsigned char c;
26
27     /* Check if the file is open or if there is an error condition */
28     if ((f->f_flags & _FOPEN) == 0 || (f->f_flags & (_FERROR | _FEOF)) != 0) {
29         return EOF;
30     }
31
32     /* If we have a pushed back character, return it */
33     if (f->f_flags & _FPUSHBACK) {
34         f->f_flags &= ~_FPUSHBACK;
35         return f->f_pushback;
36     }
37
38     /* Read one byte */
39     switch (read (f->f_fd, &c, 1)) {
40
41         case -1:
42             /* Error */
43             f->f_flags |= _FERROR;
44             return EOF;
45
46         case 0:
47             /* EOF */
48             f->f_flags |= _FEOF;
49             return EOF;
50
51         default:
52             /* Char read */
53             return c;
54
55     }
56 }
57
58
59