Subversion Repositories Kolibri OS

Rev

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

  1. /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <libc/file.h>
  5.  
  6. size_t fread(void *vptr, size_t size, size_t count, FILE *iop)
  7. {
  8.   char *ptr = (char *)vptr;
  9.   int s;
  10.   int c;
  11.  
  12. //  __libclog_printf("fread(%x,%u,%u,%x)\n",vptr,size,count,iop);
  13.  
  14.   s = size * count;
  15.   while (s > 0) {
  16.     if (iop->_cnt < s) {
  17.       if (iop->_cnt > 0) {
  18.         memcpy(ptr, iop->_ptr, iop->_cnt);
  19.         ptr += iop->_cnt;
  20.         s -= iop->_cnt;
  21.       }
  22.       /*
  23.        * filbuf clobbers _cnt & _ptr,
  24.        * so don't waste time setting them.
  25.        */
  26.       if ((c = _filbuf(iop)) == EOF)
  27.         break;
  28.       *ptr++ = c;
  29.       s--;
  30.     }
  31.     if (iop->_cnt >= s) {
  32.       memcpy(ptr, iop->_ptr, s);
  33.       iop->_ptr += s;
  34.       iop->_cnt -= s;
  35.       return count;
  36.     }
  37.   }
  38.   return size != 0 ? count - ((s + size - 1) / size) : 0;
  39. }
  40.