LibC: Fix some bugs

This commit is contained in:
Bananymous 2023-04-25 13:22:33 +03:00
parent 175f07cd2f
commit 0718bea5a1
1 changed files with 4 additions and 2 deletions

View File

@ -216,6 +216,7 @@ int fprintf(FILE* file, const char* format, ...)
int fputc(int c, FILE* file) int fputc(int c, FILE* file)
{ {
file->buffer[file->buffer_index++] = c; file->buffer[file->buffer_index++] = c;
file->offset++;
if (c == '\n' || file->buffer_index == sizeof(file->buffer)) if (c == '\n' || file->buffer_index == sizeof(file->buffer))
if (fflush(file) == EOF) if (fflush(file) == EOF)
return EOF; return EOF;
@ -235,7 +236,7 @@ int fputs(const char* str, FILE* file)
size_t fread(void* buffer, size_t size, size_t nitems, FILE* file) size_t fread(void* buffer, size_t size, size_t nitems, FILE* file)
{ {
if (file->eof) if (file->eof || nitems * size == 0)
return 0; return 0;
long ret = syscall(SYS_READ, file->fd, buffer, size * nitems); long ret = syscall(SYS_READ, file->fd, buffer, size * nitems);
if (ret < 0) if (ret < 0)
@ -246,6 +247,7 @@ size_t fread(void* buffer, size_t size, size_t nitems, FILE* file)
} }
if (ret < size * nitems) if (ret < size * nitems)
file->eof = true; file->eof = true;
file->offset += ret;
return ret / size; return ret / size;
} }
@ -376,7 +378,7 @@ void perror(const char* string)
fputs(": ", stderr); fputs(": ", stderr);
} }
fputs(strerror(errno), stderr); fputs(strerror(errno), stderr);
fflush(stderr); fputc('\n', stderr);
stderr->error = true; stderr->error = true;
} }