Files
rosetta/lib/libc/io/stdio/fgets.c
T

41 lines
470 B
C
Raw Normal View History

#include "file.h"
#include <stdio.h>
char *fgets(
char *restrict str,
int count,
struct __opaque_file *restrict stream)
{
if (stream->f_flags & (FILE_EOF | FILE_ERR)) {
return NULL;
}
size_t i = 0;
while (1) {
int c = fgetc(stream);
if (c == EOF) {
str[i] = 0;
break;
}
str[i++] = c;
if (c == '\n') {
str[i] = 0;
break;
}
}
if (ferr(stream)) {
return NULL;
}
if (feof(stream) && i == 0) {
return NULL;
}
return str;
}