home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / FREAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  2.2 KB  |  100 lines

  1. /* fread.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <stdio.h>
  5. #include <io.h>
  6. #include <errno.h>
  7. #include <string.h>
  8.  
  9. #define min(a,b) (((a) < (b)) ? (a) : (b))
  10.  
  11. size_t fread (void *buffer, size_t size, size_t count, FILE *stream)
  12. {
  13.   size_t total, left, n;
  14.   int r;   /* signed! */
  15.   char *dst;
  16.   int fh, c;
  17.  
  18.   if (size == 0 || count == 0)
  19.     return (0);
  20.   total = size * count;
  21.   if (total / count != size)
  22.     {
  23.       errno = ERANGE;
  24.       return (0);
  25.     }
  26.   if (nbuf (stream))
  27.     _fbuf (stream);
  28.   left = total;
  29.   dst = buffer;
  30.   fh = fileno (stream);
  31.   stream->flags |= _IOREAD;
  32.   if (bbuf (stream))
  33.     while (left != 0)
  34.       {
  35.         if (stream->rcount != 0)
  36.           {
  37.             n = min ((size_t)stream->rcount, left);
  38.             memcpy (dst, stream->ptr, n);
  39.             stream->ptr += n;
  40.             stream->rcount -= n;
  41.             dst += n;
  42.             left -= n;
  43.           }
  44.         else if (left > BUFSIZ)
  45.           {
  46.             n = (left / BUFSIZ) * BUFSIZ;      /* Number of buffers */
  47.             r = read (fh, dst, n);
  48.             if (r < 0)
  49.               {
  50.                 stream->flags |= _IOERR;
  51.                 break;
  52.               }
  53.             if (r == 0)
  54.               {
  55.                 stream->flags |= _IOEOF;
  56.                 break;
  57.               }
  58.             left -= r;
  59.             dst += r;
  60.           }
  61.         else
  62.           {
  63.             c = _fill (stream);
  64.             if (c == EOF)
  65.               break;
  66.             *dst++ = (char)c;
  67.             --left;
  68.           }
  69.       }
  70.   else
  71.     {
  72.       if (stream->rcount != 0)
  73.         {
  74.           n = min ((size_t)stream->rcount, left);
  75.           memcpy (dst, stream->ptr, n);
  76.           stream->ptr += n;
  77.           stream->rcount -= n;
  78.           dst += n;
  79.           left -= n;
  80.         }
  81.       while (left != 0)
  82.         {
  83.           r = read (fh, dst, left);
  84.           if (r < 0)
  85.             {
  86.               stream->flags |= _IOERR;
  87.               break;
  88.             }
  89.           if (r == 0)
  90.             {
  91.               stream->flags |= _IOEOF;
  92.               break;
  93.             }
  94.           dst += r;
  95.           left -= r;
  96.         }
  97.     }
  98.   return ((total - left) / size);
  99. }
  100.