Subversion Repositories Kolibri OS

Rev

Rev 8793 | Blame | Compare with Previous | 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. size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream) {
  7.         unsigned bytes_written = 0;
  8.         unsigned bytes_count = size * nmemb;
  9.        
  10.         if(!stream){
  11.                 errno = EBADF;
  12.                 return 0;
  13.         }
  14.        
  15.         if(size<=0 || nmemb<=0){
  16.                 errno = EINVAL;
  17.                 stream->error=errno;
  18.                 return 0;
  19.         }
  20.        
  21.         if(stream==stdout){
  22.                 con_init();
  23.                 con_write_string((char*)ptr, bytes_count);
  24.                 return nmemb;
  25.         }
  26.        
  27.         if(stream==stderr){
  28.                 for (size_t i = 0; i < bytes_count; i++) {
  29.                         char c = *(char*)(ptr+i);
  30.                         _ksys_debug_putc(c);
  31.                 }
  32.                 return nmemb;
  33.         }
  34.        
  35.         if(stream->mode != _FILEMODE_R){
  36.                 unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written);
  37.                 if (status != KSYS_FS_ERR_SUCCESS) {
  38.                         errno = EIO;
  39.                         stream->error = errno;
  40.                         return 0;
  41.                 }
  42.                 stream->position+=bytes_written;
  43.         }
  44.         return bytes_written/size;
  45. }
  46.