home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / fread.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  3KB  |  163 lines

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