]> git.sur5r.net Git - cc65/blob - libsrc/common/gets.c
Invalid error codes will set errno
[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 /*****************************************************************************/
15 /*                                   Code                                    */
16 /*****************************************************************************/
17
18
19
20 char* __fastcall__ gets (char* s)
21 {
22     int c;
23     int i = 0;
24
25     do {
26
27         /* Get next character */
28         if ((c = fgetc (stdin)) == EOF) {
29             /* Error or EOF */
30             s [i] = 0;
31             if (stdin->f_flags & _FERROR) {
32                 /* ERROR */
33                 return 0;
34             } else {
35                 /* EOF */
36                 if (i) {
37                     return s;
38                 } else {
39                     return 0;
40                 }
41             }
42         }
43
44         /* One char more */
45         s [i++] = c;
46
47     } while (c != '\n');
48
49     /* Replace newline by NUL */
50     s [i-1] = '\0';
51
52     /* Done */
53     return s;
54 }
55
56
57
58