]> git.sur5r.net Git - cc65/blob - libsrc/common/fgets.c
Renamed the defines in symdefs.h to something more meaningful. They were named
[cc65] / libsrc / common / fgets.c
1 /*
2  * Ullrich von Bassewitz, 11.08.1998
3  *
4  * char* fgets (char* s, int size, FILE* f);
5  */
6
7
8
9 #include <stdio.h>
10 #include <errno.h>
11 #include "_file.h"
12
13
14
15 /*****************************************************************************/
16 /*                                   Code                                    */
17 /*****************************************************************************/
18
19
20
21 char* __fastcall__ fgets (char* s, unsigned size, FILE* f)
22 {
23     unsigned i;
24     int c;
25
26     if (size == 0) {
27         /* Invalid size */
28         return (char*) _seterrno (EINVAL);
29     }
30
31     /* Read input */
32     i = 0;
33     while (--size) {
34
35         /* Get next character */
36         if ((c = fgetc (f)) == EOF) {
37             s[i] = '\0';
38             /* Error or EOF */
39             if ((f->f_flags & _FERROR) != 0 || i == 0) {
40                 /* ERROR or EOF on first char */
41                 return 0;
42             } else {
43                 /* EOF with data already read */
44                 break;
45             }
46         }
47
48         /* One char more */
49         s[i++] = c;
50
51         /* Stop at end of line */
52         if (c == '\n') {
53             break;
54         }
55     }
56
57     /* Terminate the string */
58     s[i] = '\0';
59
60     /* Done */
61     return s;
62 }
63
64
65