]> git.sur5r.net Git - cc65/blob - libsrc/common/fgetc.c
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / libsrc / common / fgetc.c
1 /*
2  * Ullrich von Bassewitz, 11.08.1998
3  *
4  * int fgetc (FILE* f);
5  */
6
7
8
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include "_file.h"
13
14
15
16 int fgetc (FILE* f)
17 {
18     char c;
19
20     /* Check if the file is open or if there is an error condition */
21     if ((f->f_flags & _FOPEN) == 0 || (f->f_flags & (_FERROR | _FEOF)) != 0) {
22         return -1;
23     }
24
25     /* Read the byte */
26     switch (read (f->f_fd, &c, 1)) {
27
28         case -1:
29             /* Error */
30             f->f_flags |= _FERROR;
31             return -1;
32
33         case 0:
34             /* EOF */
35             f->f_flags |= _FEOF;
36             return -1;
37
38         default:
39             /* Char read */
40             return ((int) c) & 0xFF;
41
42     }
43 }
44
45
46