home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / fread.c < prev    next >
C/C++ Source or Header  |  1992-12-12  |  3KB  |  142 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. extern short  __FRW_BIN__;
  14.  
  15. size_t    fread(_data, size, count, fp)
  16.     void *_data;
  17.     size_t size;
  18.     size_t count;
  19.     register FILE *fp;
  20.     {
  21.     register size_t n;
  22.     register long l, cnt;
  23.     register unsigned int f;
  24.     char *data=_data;
  25.     char *ptr;
  26.  
  27.     assert((data != NULL));
  28.     assert((size != 0));
  29.     
  30.     f = fp->_flag;
  31.     if(f & _IORW) f = (fp->_flag |= _IOREAD);
  32.     if(!(f & _IOREAD) || (f & (_IOERR | _IOEOF)))
  33.         return(0);
  34.  
  35.     l = 0;
  36.     n = count * size;
  37. #if 0
  38.     if(fflush(fp))            /* re-sync file pointers */
  39.         return 0;
  40. #endif
  41.     assert((n <= (size_t)LONG_MAX));
  42.     if( (f&_IOBIN) || __FRW_BIN__ ) {
  43.     again:    
  44.     if((cnt = fp->_cnt) > 0)
  45.     {
  46.         cnt = (cnt < n) ? cnt : n;
  47.         bcopy(fp->_ptr, data, cnt);
  48.         fp->_cnt -= cnt;
  49.         fp->_ptr += cnt;
  50.         l += cnt;
  51.         data = data + cnt;
  52.         n -= cnt;
  53.     }
  54.     /* n == how much more */
  55.     if(n > 0)
  56.     {
  57.         if(n < fp->_bsiz)
  58.         { /* read in fp->_bsiz bytes into fp->_base and do it again */
  59.         fp->_ptr = fp->_base;
  60.         if((cnt = _read(fp->_file, fp->_base, (unsigned long)fp->_bsiz)) <= 0)
  61.         {   /* EOF or error */
  62.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  63.             goto ret;
  64.         }
  65.         fp->_cnt = cnt;
  66.         goto again;
  67.         }
  68.         else
  69.         while (n > 0)
  70.         { /* read in n bytes into data */
  71.         if((cnt = _read(fp->_file, data, (unsigned long)n)) <= 0)
  72.         {   /* EOF or error */
  73.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  74.             goto ret;
  75.         }
  76.         l += cnt;
  77.         data = data + cnt;
  78.         n -= cnt;
  79.         }
  80.     }
  81.     } else {
  82.     while( n>0 ) {
  83.         if( (cnt=fp->_cnt)>0 ) {
  84.         ptr=(char*)fp->_ptr;
  85.         while( n>0 && cnt>0 ) {
  86.             if( *ptr!='\r' ) {
  87.             *data++=*ptr++;
  88.             cnt--;
  89.             n--;
  90.             l++;
  91.             } else {
  92.             ptr++;
  93.             cnt--;
  94.             }
  95.         }
  96.         fp->_cnt=cnt;
  97.         fp->_ptr=(unsigned char*)ptr;
  98.         }
  99.         if( n==0 ) break; /* done */
  100.         /* wanna have n more bytes */
  101.         if( n<fp->_bsiz ) {
  102.         /* read in fp->_bsiz bytes into fp->_base and do it again */
  103.         fp->_ptr = fp->_base;
  104.         if((cnt = _read(fp->_file, fp->_base, (unsigned long)fp->_bsiz)) <= 0)
  105.         {   /* EOF or error */
  106.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  107.             goto ret;
  108.         }
  109.         fp->_cnt = cnt;
  110.         } else {
  111.         /* read in n bytes into data */
  112.         if((cnt = _read(fp->_file, data, (unsigned long)n)) <= 0)
  113.         {   /* EOF or error */
  114.             fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
  115.             goto ret;
  116.         }
  117.         /* Quickly move to first CR */
  118.         ptr = memchr (data, '\r', cnt);
  119.         if (ptr == NULL)
  120.           ptr = data + cnt;
  121.         cnt -= ptr - data;
  122.         n -= ptr - data;
  123.         data = ptr;
  124.         while( cnt>0 ) {
  125.             if( *ptr!='\r' ) {
  126.             *data++=*ptr++;
  127.             cnt--;
  128.             n--;
  129.             l++;
  130.             } else {
  131.             ptr++;
  132.             cnt--;
  133.             }
  134.         }
  135.         }
  136.     }
  137.     }
  138.  
  139.     ret:
  140.     return((l > 0) ? ((size_t)l / size) : 0);
  141.     }
  142.