Files
kolibrios/programs/develop/ktcc/libc.obj/source/stdio/fwrite.c
T
Egor00f c121db7a88
Build system / Build (es_ES) (push) Successful in 3m18s
Build system / Build (en_US) (push) Successful in 3m23s
Build system / Build (ru_RU) (push) Successful in 3m25s
Build system / Publish Images (push) Successful in 4m52s
libc.obj: use bitfield flags in file instead of int variables && rewrite freopen, fopen (#691)
Use bitfield `flags` flags in file instead of `int` variables `mode`, `error`, `eof`.

And rewrite weird logic opening/close files

Reviewed-on: #691
Reviewed-by: Ivan B <1+dunkaist@noreply.localhost>
Reviewed-by: Kiril Lipatov <lipatov.kiril@gmail.com>
Co-authored-by: Egor00f <y.yarin@inbox.ru>
2026-09-22 12:39:50 +00:00

47 lines
1.2 KiB
C

#include <stdio.h>
#include "../sys/_conio.h"
#include <sys/ksys.h>
#include <errno.h>
#include <shell_api.h>
size_t fwrite(const void* restrict ptr, size_t size, size_t nmemb, FILE* restrict stream)
{
unsigned bytes_written = 0;
unsigned bytes_count = size * nmemb;
if (!stream) {
errno = EBADF;
return 0;
}
if (size <= 0 || nmemb <= 0) {
errno = EINVAL;
stream->flags.error = true;
return 0;
}
if (stream == stdout) {
console_write((char*)ptr, bytes_count);
return nmemb;
}
if (stream == stderr) {
for (size_t i = 0; i < bytes_count; i++) {
char c = *(char*)(ptr + i);
_ksys_debug_putc(c);
}
return nmemb;
}
if (stream->flags.write) {
unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written);
if (status != KSYS_FS_ERR_SUCCESS) {
errno = EIO;
stream->flags.error = true;
return 0;
}
stream->position += bytes_written;
}
return bytes_written / size;
}