Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include "conio.h"
  3. #include <sys/ksys.h>
  4. #include <errno.h>
  5.  
  6.  
  7. size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream) {
  8.         unsigned bytes_written = 0;
  9.         unsigned bytes_count = size * nmemb;
  10.        
  11.         if(!stream){
  12.                 errno = EINVAL;
  13.                 return 0;
  14.         }
  15.        
  16.         if(stream==stdout){
  17.                 __con_init();
  18.                 __con_write_string((char*)ptr, bytes_count);
  19.                 return nmemb;
  20.         }
  21.         else if(stream==stderr){
  22.                 for (size_t i = 0; i < bytes_count; i++) {
  23.                         char c = *(char*)(ptr+i);
  24.                         _ksys_debug_putc(c);
  25.                 }
  26.         }
  27.         else{
  28.                 if(stream->mode != _STDIO_F_R){
  29.                         unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written);
  30.                         if (status != KSYS_FS_ERR_SUCCESS) {
  31.                 errno = EIO;
  32.                 stream->error = errno;
  33.                                 return 0;
  34.                 }else {
  35.                         stream->position+=bytes_written;
  36.                         }
  37.                 }
  38.         }
  39.         return bytes_written;
  40. }