41 lines
470 B
C
41 lines
470 B
C
|
|
#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;
|
||
|
|
}
|