home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / FREAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  3.9 KB  |  149 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - fread.c
  3.  *
  4.  * function(s)
  5.  *        fread  - reads data from a stream
  6.  *        _fgetn - reads data from a stream
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*[]------------------------------------------------------------[]*/
  10. /*|                                                              |*/
  11. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  12. /*|                                                              |*/
  13. /*|                                                              |*/
  14. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  15. /*|     All Rights Reserved.                                     |*/
  16. /*|                                                              |*/
  17. /*[]------------------------------------------------------------[]*/
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <io.h>
  23. #include <asmrules.h>
  24.  
  25. /*---------------------------------------------------------------------*
  26.  
  27. Name        _fgetn - reads data from a stream
  28.  
  29. Usage        static unsigned pascal near
  30.              _fgetn (register void *ptr, size_t n, FILE *fp);
  31.  
  32. Prototype in    local to this module
  33.  
  34. Description    _fgetn reads n bytes of data from the named input stream
  35.                 into a block pointed to by ptr.
  36.  
  37. Return value    success : 0
  38.         else    : non-zero
  39.  
  40. *---------------------------------------------------------------------*/
  41. static unsigned pascal near _fgetn( register void *ptr, size_t n, FILE *fp )
  42.   {
  43.   register int Byte, Temp;
  44.  
  45.   while( n )
  46.     {
  47.     n++;
  48.  
  49.     Temp = min( n, fp->bsize );
  50.  
  51.     if( (fp->flags & _F_BIN) && fp->bsize && (n > fp->bsize) && (fp->level == 0))
  52.       {
  53.       n--;
  54.       Temp = 0;
  55.  
  56.       while( n >= fp->bsize )
  57.     {
  58.     Temp += fp->bsize;
  59.     n -= fp->bsize;
  60.     }
  61.  
  62.       Byte = _read(fp->fd, ptr, Temp);
  63.  
  64.       (char *) ptr += Byte;
  65.  
  66.       if( Byte != Temp )
  67.     {
  68.     n += (Temp - Byte);
  69.         fp->flags |= _F_EOF;
  70.     break;
  71.           }
  72.       }
  73.     else
  74.       {
  75.       while (--n  && --Temp && ( Byte = getc (fp)) != EOF )
  76.         {
  77.         *((char *) ptr)++ = Byte;
  78.         }
  79.  
  80.       if( Byte == EOF )
  81.         {
  82.         fp->flags |= _F_EOF;
  83.         break;
  84.         }
  85.       }
  86.     }
  87.  
  88.   return n;
  89.   }
  90.  
  91.  
  92. /*---------------------------------------------------------------------*
  93.  
  94. Name        fread - reads data from a stream
  95.  
  96. Usage        #include <stdio.h>
  97.         size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
  98.  
  99. Related
  100. functions usage    size_t fwrite(void *ptr, size_t size, size_t nitems, FILE *stream);
  101.  
  102. Prototype in    stdio.h
  103.  
  104. Description    fread reads nitems of data, each of length size
  105.         bytes, from the named input stream into a block pointed to by
  106.         ptr.
  107.  
  108.         fwrite appends nitems of data, each of length size bytes, to
  109.         the named output stream. The data appended begins at ptr.
  110.  
  111.         For both functions, the total number of bytes read is (nitems
  112.         * size).
  113.  
  114.         ptr in the declarations is a pointer to any object.
  115.         size is the size of the object ptr points to. The
  116.         expression sizeof *ptr will produce the proper value.
  117.  
  118. Return value    On successful completion, each function returns the
  119.         number of items (not bytes) actually read or written. fread
  120.         returns a short count (possibly 0) on end-of-file or error.
  121.         fwrite returns a short count on error.
  122.  
  123. *---------------------------------------------------------------------*/
  124. size_t fread( void *ptr, register size_t psize, size_t nitems, FILE *fp )
  125.   {
  126. #if (LDATA)
  127.   register size_t n;
  128. #endif
  129.   unsigned long temp;
  130.  
  131.   if( !psize )  return( 0 );
  132.  
  133.   if( (temp = (unsigned long)psize * (unsigned long)nitems) < 0x10000L)
  134.         return( ((unsigned)temp - _fgetn( ptr, (unsigned)temp, fp )) / psize );
  135.  
  136. #if (!LDATA)
  137.   return 0;
  138. #else
  139.   n = nitems+1;
  140.  
  141.   while( --n && 0 == _fgetn( ptr, psize, fp ))
  142.     {
  143.     ptr = (void *) (((char huge *) ptr) + psize);
  144.     }
  145.  
  146.   return( nitems - n );
  147. #endif
  148.   }
  149.