#include <stdio.h>

char *fgets(char *buf, int num, FILE *stream)
{
	int i = 0,rv;
	while (1) {
		rv = getc(stream);
		if (rv == EOF)
			break;
		if (--num) {
			buf[i++] = (char)rv;
			if (rv == '\n') 
				break;
		}
		else
			break;
	}
	buf[i] = 0;
	if (rv == EOF)
		return 0;
	else return buf;
}