Subversion Repositories Kolibri OS

Rev

Rev 9165 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include "conio.h"
  4. #include "sys/ksys.h"
  5.  
  6. size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict stream) {
  7.         unsigned bytes_read = 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==stdin){
  22.                 con_init();
  23.                 con_gets((char*)ptr, bytes_count+1);
  24.                 return nmemb;
  25.         }
  26.  
  27.     if(stream->mode != _FILEMODE_W && stream->mode != _FILEMODE_A){
  28.         if(!stream->__ungetc_emu_buff){
  29.                         ((char*) ptr)[0]=(char)stream->__ungetc_emu_buff;
  30.                         //debug_printf("Ungetc: %x\n", ((char*) ptr)[0]);
  31.                 }
  32.                 unsigned status = _ksys_file_read_file(stream->name, stream->position, bytes_count, ptr , &bytes_read);
  33.                 if (status != KSYS_FS_ERR_SUCCESS) {
  34.                         if(status == KSYS_FS_ERR_EOF){
  35.                                 stream->eof=1;
  36.                         }else{
  37.                                 errno = EIO;
  38.                                 stream->error = errno;
  39.                                 return 0;
  40.                         }
  41.                 }
  42.                 stream->position+=bytes_read;
  43.         }
  44.         return bytes_read/size;
  45. }
  46.