1 /* 2 * fgets.c - get a string from a file 3 */ 4 /* $Header$ */ 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 } 28