home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / mntlib16.lzh / MNTLIB16 / FREAD.C < prev    next >
C/C++ Source or Header  |  1993-07-29  |  2KB  |  75 lines

  1. /* nothing like the original from
  2.  * Dale Schumacher's dLibs
  3.  */
  4.  
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <limits.h>
  8. #include <assert.h>
  9. #include <string.h>
  10. #include "lib.h"
  11.  
  12. size_t  fread(data, size, count, fp)
  13.     register void *data;
  14.     size_t size;
  15.     size_t count;
  16.     register FILE *fp;
  17. {
  18.     register size_t n;
  19.     register long l, cnt;
  20.     register unsigned int f;
  21.  
  22.     assert((data != NULL));
  23.     assert((size != 0));
  24.     
  25.     f = fp->_flag;
  26.     if(f & _IORW) f = (fp->_flag |= _IOREAD);
  27.     if(!(f & _IOREAD) || (f & (_IOERR | _IOEOF)))
  28.         return(0);
  29.  
  30.     l = 0;
  31.     n = count * size;
  32. #if 0
  33.     if(fflush(fp))            /* re-sync file pointers */
  34.         return 0;
  35. #endif
  36.     assert((n <= (size_t)LONG_MAX));
  37.     again:    
  38.     if((cnt = fp->_cnt) > 0)
  39.     {
  40.         cnt = (cnt < n) ? cnt : n;
  41.         bcopy(fp->_ptr, data, cnt);
  42.         fp->_cnt -= cnt;
  43.         fp->_ptr += cnt;
  44.         l += cnt;
  45.         data += cnt;
  46.         n -= cnt;
  47.     }
  48.     /* n == how much more */
  49.     if(n > 0)
  50.     {
  51.         if(n < fp->_bsiz)
  52.         { /* read in fp->_bsiz bytes into fp->_base and do it again */
  53.         fp->_ptr = fp->_base;
  54.         if((cnt = _read(fp->_file, fp->_base, (long)fp->_bsiz)) <= 0)
  55.         {   /* EOF or error */
  56.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  57.             goto ret;
  58.         }
  59.         fp->_cnt = cnt;
  60.         goto again;
  61.         }
  62.         else
  63.         { /* read in n bytes into data */
  64.         cnt = _read(fp->_file, data, (long)n);
  65.         if (cnt <= 0) {
  66.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  67.         } else {
  68.             l += cnt;
  69.         }
  70.         }
  71.     }
  72.     ret:
  73.     return((l > 0) ? ((size_t)l / size) : 0);
  74. }
  75.