Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / stdio / fgets.c
1 /*
2  * fgets.c - get a string from a file
3  */
4 /* $Id: fgets.c,v 1.4 1994/06/24 11:49:05 ceriel Exp $ */
5
6 #include        <stdio.h>
7
8 char *
9 fgets(char *s, register int n, register FILE *stream)
10 {
11         register int ch;
12         register char *ptr;
13
14         ptr = s;
15         while (--n > 0 && (ch = getc(stream)) != EOF) {
16                 *ptr++ = ch;
17                 if ( ch == '\n')
18                         break;
19         }
20         if (ch == EOF) {
21                 if (feof(stream)) {
22                         if (ptr == s) return NULL;
23                 } else return NULL;
24         }
25         *ptr = '\0';
26         return s;
27 }