home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / amiga / libnix-0.7-src.lha / libnix-0.7 / sources / nix / stdio / fread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  941 b   |  45 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. extern void __chkabort(void);
  7. extern int __fflush(FILE *stream);
  8.  
  9. size_t fread(void *ptr,size_t size,size_t nmemb,FILE *stream)
  10. {
  11.   signed long subsize;
  12.   unsigned long total;
  13.   unsigned char *b;
  14.   __chkabort();
  15.   if(stream->flags&64) /* Error on stream */
  16.   { errno=EPERM;
  17.     return 0; }
  18.   if (!(total=size*nmemb))
  19.     return 0;
  20.   b=(unsigned char *)ptr;
  21.   if(stream->flags&8)
  22.   { if(__fflush(stream))
  23.       return 0;
  24.   }else
  25.     while(total&&(stream->incount||stream->tmpp!=NULL))
  26.     { total--;
  27.       *b++=fgetc(stream); } /* read the buffer */
  28.   while(total)
  29.   {
  30.     subsize=read(stream->file,b,total);
  31.     if(subsize==0)
  32.     {
  33.       stream->flags|=32;
  34.       break;
  35.     }else if(subsize<0) /* Error */
  36.     {
  37.       stream->flags|=64; /* error flag */
  38.       break;
  39.     }
  40.     total=total-subsize;
  41.     b=b+subsize;
  42.   }
  43.   return (b-(unsigned char *)ptr)/size;
  44. }
  45.