home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / stdio / fread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  1.3 KB  |  75 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <libc/file.h>
  6.  
  7. size_t
  8. fread(void *vptr, size_t size, size_t count, FILE *iop)
  9. {
  10.   char *ptr = (char *)vptr;
  11.   int s;
  12.   int c;
  13.  
  14.   s = size * count;
  15.   if(!__is_text_file(iop))
  16.   {
  17.     while (s > 0) {
  18.       if (iop->_cnt < s) {
  19.     if (iop->_cnt > 0) {
  20.       memcpy(ptr, iop->_ptr, iop->_cnt);
  21.       ptr += iop->_cnt;
  22.       s -= iop->_cnt;
  23.     }
  24.     /*
  25.      * filbuf clobbers _cnt & _ptr,
  26.      * so don't waste time setting them.
  27.      */
  28.     if ((c = _filbuf(iop)) == EOF)
  29.       break;
  30.     *ptr++ = c;
  31.     s--;
  32.       }
  33.       if (iop->_cnt >= s) {
  34.     memcpy(ptr, iop->_ptr, s);
  35.     iop->_ptr += s;
  36.     iop->_cnt -= s;
  37.     return count;
  38.       }
  39.     }
  40.   }
  41.   else
  42.   {
  43.     while (s > 0) {
  44.       if (iop->_cnt < s) {
  45.     while (iop->_cnt > 0) {
  46.       if ((c = *iop->_ptr++) != '\r')
  47.       {
  48.         *ptr++ = c;
  49.         s--;
  50.       }
  51.       iop->_cnt--;
  52.     }
  53.     if ((c = _filbuf(iop)) == EOF)
  54.       break;
  55.     if (c != '\r')
  56.     {
  57.       *ptr++ = c;
  58.       s--;
  59.     }
  60.       }
  61.       if (iop->_cnt >= s) {
  62.     while (s > 0 && iop->_cnt > 0) {
  63.       if ((c = *iop->_ptr++) != '\r')
  64.       {
  65.         *ptr++ = c;
  66.         s--;
  67.       }
  68.       iop->_cnt--;
  69.     }
  70.       }
  71.     } /* end while */
  72.   }
  73.   return size != 0 ? count - ((s + size - 1) / size) : 0;
  74. }
  75.