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

46 lines
618 B
C
Raw Normal View History

#include "file.h"
#include <stdio.h>
char *fgets(
char *restrict str,
int count,
struct __opaque_file *restrict stream)
{
__libc_file_lock(stream);
if (stream->f_flags & (FILE_EOF | FILE_ERR)) {
__libc_file_unlock(stream);
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 (ferror(stream)) {
__libc_file_unlock(stream);
return NULL;
}
if (feof(stream) && i == 0) {
__libc_file_unlock(stream);
return NULL;
}
__libc_file_unlock(stream);
return str;
}