shithub: scc

ref: 0abc10c55a5366e6fdad4c775e5f18b39c14eec6
dir: /src/libc/stdio/fgets.c/

View raw version
#include <stdio.h>

#undef fgets

char *
fgets(char *restrict s, int n, FILE *restrict fp)
{
	int ch = 0;
	char *t = s;

	for (--n; n > 0; --n) {
		if ((ch = getc(fp)) == EOF)
			break;
		if  ((*t++ = ch) == '\n')
			break;
	}
	if (ch == EOF && s == t)
		return NULL;
	*t = '\0';

	return s;
}