2021-04-27 18:33:31 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "conio.h"
|
|
|
|
#include <sys/ksys.h>
|
|
|
|
#include <errno.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){
|
2021-05-23 17:28:07 +02:00
|
|
|
errno = EBADF;
|
2021-04-27 18:33:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-26 22:35:11 +02:00
|
|
|
if(size<=0 || nmemb<=0){
|
|
|
|
errno = EINVAL;
|
|
|
|
stream->error=errno;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-27 18:33:31 +02:00
|
|
|
if(stream==stdout){
|
2021-05-25 17:38:14 +02:00
|
|
|
con_init();
|
|
|
|
con_write_string((char*)ptr, bytes_count);
|
2021-04-27 18:33:31 +02:00
|
|
|
return nmemb;
|
|
|
|
}
|
2021-08-26 22:35:11 +02:00
|
|
|
|
|
|
|
if(stream==stderr){
|
2021-04-27 18:33:31 +02:00
|
|
|
for (size_t i = 0; i < bytes_count; i++) {
|
|
|
|
char c = *(char*)(ptr+i);
|
|
|
|
_ksys_debug_putc(c);
|
|
|
|
}
|
2021-08-26 22:35:11 +02:00
|
|
|
return nmemb;
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
2021-08-26 22:35:11 +02:00
|
|
|
|
|
|
|
if(stream->mode != _FILEMODE_R){
|
|
|
|
unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written);
|
|
|
|
if (status != KSYS_FS_ERR_SUCCESS) {
|
|
|
|
errno = EIO;
|
|
|
|
stream->error = errno;
|
|
|
|
return 0;
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
2021-08-26 22:35:11 +02:00
|
|
|
stream->position+=bytes_written;
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
2021-08-26 22:35:11 +02:00
|
|
|
return bytes_written/size;
|
|
|
|
}
|