home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1997 #3 / Amiga Plus CD - 1997 - No. 03.iso / pd / programmierung / vbcc / machines / amiga68k / libsrc / stdio / fread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  794 b   |  32 lines

  1. #include <stdio.h>
  2.  
  3. size_t fread(void *ptr,size_t size,size_t nmemb,FILE *f)
  4. {
  5.     size_t cnt,total=size*nmemb;
  6.     char *p=ptr;
  7.     long result;
  8.     if(!f||!total) return(0);
  9.     if((f->flags&(_READABLE|_WRITE|_ERR|_EOF))!=_READABLE) return(0);
  10.     f->flags|=_READ;
  11.     if(cnt=f->count){
  12.     /*  Buffer lesen    */
  13.         if(total<=cnt){
  14.             memcpy(p,f->pointer,total);
  15.             f->pointer+=total;f->count-=total;
  16.             return(nmemb);
  17.         }else{
  18.             memcpy(p,f->pointer,cnt);
  19.             total-=cnt;p+=cnt;
  20.             f->count=0;
  21.         }
  22.     }
  23.     result=Read(f->filehandle,p,total);
  24.     if(result==-1){f->flags|=_ERR;return(cnt/size);}
  25.     if(result<total){
  26.         f->flags|=_EOF;
  27.         return((cnt+result)/size);
  28.     }else{
  29.         return(nmemb);
  30.     }
  31. }
  32.