]> git.sur5r.net Git - cc65/blob - libsrc/common/gets.c
Fixed a bug
[cc65] / libsrc / common / gets.c
1 /*
2  * gets.c
3  *
4  * Ullrich von Bassewitz, 11.08.1998
5  */
6
7
8
9 #include <stdio.h>
10 #include "_file.h"
11
12
13
14 char* gets (char* s)
15 {
16     int i, c;
17
18     i = 0;
19     do {
20
21         /* Get next character */
22         c = fgetc (stdin);
23         if (c == -1) {
24             /* Error or EOF */
25             s [i] = 0;
26             if (stdin->f_flags & _FERROR) {
27                 /* ERROR */
28                 return 0;
29             } else {
30                 /* EOF */
31                 if (i) {
32                     return s;
33                 } else {
34                     return 0;
35                 }
36             }
37         }
38
39         /* One char more */
40         s [i++] = c;
41
42     } while (c != '\n');
43
44     /* Replace newline by NUL */
45     s [i-1] = '\0';
46
47     /* Done */
48     return s;
49 }
50
51
52