2026-03-25 20:21:12 +00:00
|
|
|
#include "file.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
char *fgets(
|
|
|
|
|
char *restrict str,
|
|
|
|
|
int count,
|
|
|
|
|
struct __opaque_file *restrict stream)
|
|
|
|
|
{
|
2026-04-01 18:51:13 +01:00
|
|
|
__libc_file_lock(stream);
|
2026-03-25 20:21:12 +00:00
|
|
|
if (stream->f_flags & (FILE_EOF | FILE_ERR)) {
|
2026-04-01 18:51:13 +01:00
|
|
|
__libc_file_unlock(stream);
|
2026-03-25 20:21:12 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 18:51:13 +01:00
|
|
|
if (ferror(stream)) {
|
|
|
|
|
__libc_file_unlock(stream);
|
2026-03-25 20:21:12 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (feof(stream) && i == 0) {
|
2026-04-01 18:51:13 +01:00
|
|
|
__libc_file_unlock(stream);
|
2026-03-25 20:21:12 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 18:51:13 +01:00
|
|
|
__libc_file_unlock(stream);
|
2026-03-25 20:21:12 +00:00
|
|
|
return str;
|
|
|
|
|
}
|