home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mntlib24 / fread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  2.8 KB  |  134 lines

  1. /* nothing like the original from
  2.  * from Dale Schumacher's dLibs
  3.  */
  4.  
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <limits.h>
  9. #include <assert.h>
  10. #include <string.h>
  11. #include "lib.h"
  12.  
  13. size_t    fread(_data, size, count, fp)
  14.     void *_data;
  15.     size_t size;
  16.     size_t count;
  17.     register FILE *fp;
  18.     {
  19.     register size_t n;
  20.     register long l, cnt;
  21.     register unsigned int f;
  22.     char *data=_data;
  23.     char *ptr;
  24.  
  25.     assert((data != NULL));
  26.     assert((size != 0));
  27.     
  28.     f = fp->_flag;
  29.     if(f & _IORW) f = (fp->_flag |= _IOREAD);
  30.     if(!(f & _IOREAD) || (f & (_IOERR | _IOEOF)))
  31.         return(0);
  32.  
  33.     l = 0;
  34.     n = count * size;
  35. #if 0
  36.     if(fflush(fp))            /* re-sync file pointers */
  37.         return 0;
  38. #endif
  39.     assert((n <= (size_t)LONG_MAX));
  40.     if( f&_IOBIN ) {
  41.     again:    
  42.     if((cnt = fp->_cnt) > 0)
  43.     {
  44.         cnt = (cnt < n) ? cnt : n;
  45.         bcopy(fp->_ptr, data, cnt);
  46.         fp->_cnt -= cnt;
  47.         fp->_ptr += cnt;
  48.         l += cnt;
  49.         data = data + cnt;
  50.         n -= cnt;
  51.     }
  52.     /* n == how much more */
  53.     if(n > 0)
  54.     {
  55.         if(n < fp->_bsiz)
  56.         { /* read in fp->_bsiz bytes into fp->_base and do it again */
  57.         fp->_ptr = fp->_base;
  58.         if((cnt = _read(fp->_file, fp->_base, (unsigned long)fp->_bsiz)) <= 0)
  59.         {   /* EOF or error */
  60.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  61.             goto ret;
  62.         }
  63.         fp->_cnt = cnt;
  64.         goto again;
  65.         }
  66.         else
  67.         { /* read in n bytes into data */
  68.         if((cnt = _read(fp->_file, data, (unsigned long)n)) != n)
  69.         {   /* EOF or error */
  70.             fp->_flag |= ((cnt >= 0) ? _IOEOF : _IOERR);
  71.         }
  72.         if( cnt>0 )
  73.             l += cnt;
  74.         }
  75.     }
  76.     } else {
  77.     while( n>0 ) {
  78.         if( (cnt=fp->_cnt)>0 ) {
  79.         ptr=(char*)fp->_ptr;
  80.         while( n>0 && cnt>0 ) {
  81.             if( *ptr!='\r' ) {
  82.             *data++=*ptr++;
  83.             cnt--;
  84.             n--;
  85.             l++;
  86.             } else {
  87.             ptr++;
  88.             cnt--;
  89.             }
  90.         }
  91.         fp->_cnt=cnt;
  92.         fp->_ptr=(unsigned char*)ptr;
  93.         }
  94.         if( n==0 ) break; /* done */
  95.         /* wanna have n more bytes */
  96.         if( n<fp->_bsiz ) {
  97.         /* read in fp->_bsiz bytes into fp->_base and do it again */
  98.         fp->_ptr = fp->_base;
  99.         if((cnt = _read(fp->_file, fp->_base, (unsigned long)fp->_bsiz)) <= 0)
  100.         {   /* EOF or error */
  101.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  102.             goto ret;
  103.         }
  104.         fp->_cnt = cnt;
  105.         } else {
  106.         /* read in n bytes into data */
  107.         if((cnt = _read(fp->_file, data, (unsigned long)n)) != n)
  108.         {   /* EOF or error */
  109.             fp->_flag |= ((cnt >= 0) ? _IOEOF : _IOERR);
  110.             /* if EOF but read bytes still convert CRLF */
  111.             if( cnt<=0 ) goto ret;
  112.         }
  113.         ptr=data;
  114.         while( cnt>0 ) {
  115.             if( *ptr!='\r' ) {
  116.             *data++=*ptr++;
  117.             cnt--;
  118.             n--;
  119.             l++;
  120.             } else {
  121.             ptr++;
  122.             cnt--;
  123.             }
  124.         }
  125.         if( fp->_flag&_IOEOF )
  126.             goto ret;
  127.         }
  128.     }
  129.     }
  130.  
  131.     ret:
  132.     return((l > 0) ? ((size_t)l / size) : 0);
  133.     }
  134.