char *fgets(char *s, int n, FILE *stream) { size_t index = 0; while (index < (n - 1)) { int character = fgetc(stream); // EOF or error and nothing read? if (character == EOF && index == 0)return NULL; // EOF or error? else if (character == EOF)goto success; s[index] = (char)character; ++index; // newline? if ((char)character == '\n')goto success; } success: s[index] = '\0'; return s; }