]> git.sur5r.net Git - cc65/blob - libsrc/common/fgets.c
This commit was generated by cvs2svn to compensate for changes in r2,
[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 char* fgets (char* s, unsigned size, FILE* f)
16 {
17     int i, c;
18
19     /* We do not handle the case "size == 0" here */
20     i = 0; --size;
21     while (i < size) {
22
23         /* Get next character */
24         c = fgetc (f);
25         if (c == -1) {
26             s [i] = 0;
27             /* Error or EOF */
28             if (f->f_flags & _FERROR) {
29                 /* ERROR */
30                 return 0;
31             } else {
32                 /* EOF */
33                 if (i) {
34                     return s;
35                 } else {
36                     return 0;
37                 }
38             }
39         }
40
41         /* One char more */
42         s [i++] = c;
43
44         /* Stop at end of line */
45         if (c == '\n') {
46             break;
47         }
48     }
49
50     /* Replace newline by NUL */
51     s [i-1] = '\0';
52
53     /* Done */
54     return s;
55 }
56
57
58
59