lib: c: io: implement standard FILE and DIR interfaces
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user