fgets.c (293B)
1 #include <stdio.h> 2 3 #undef fgets 4 5 char * 6 fgets(char *restrict s, int n, FILE *restrict fp) 7 { 8 int ch = 0; 9 char *t = s; 10 11 for (--n; n > 0; --n) { 12 if ((ch = getc(fp)) == EOF) 13 break; 14 if ((*t++ = ch) == '\n') 15 break; 16 } 17 if (ch == EOF && s == t) 18 return NULL; 19 *t = '\0'; 20 21 return s; 22 }