home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / mdcopy / part02 / mdfill.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-02  |  2.2 KB  |  97 lines

  1. /*
  2.  * Created 1989 by greg yachuk.  Placed in the public domain.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <malloc.h>
  7. #include "mdiskio.h"
  8.  
  9. /*
  10.  * _mdfill -    fill an mdiskbuf from the diskette.  read as many
  11.  *        bytes as possible
  12.  */
  13. _mdfill(mdp)
  14. MDFILE *mdp;
  15. {
  16.     int    blen;        /* length of buffer */
  17.     int    len;        /* number of chars yet to read */
  18.     int    cumm;        /* total number of chars read so far */
  19.     int    ret;        /* number of chars most recently read */
  20.  
  21.     mdp->bufcnt = 0;        /* in case we forget, later */
  22.  
  23.     if (mdp->flags & (_MDERR|_MDWRITE))
  24.     {
  25.         mdp->flags |= _MDERR;    /* can't read into a write stream */
  26.         return (EOF);
  27.     }
  28.  
  29.     if (mdeof(mdp))
  30.         return (EOF);        /* go no business even being here */
  31.  
  32.     /* calculate the size of the buffer */
  33.  
  34.     blen = mdp->bufptr - mdp->buffer;
  35.  
  36.     /* first fill when `get' first char, so allocate buffer */
  37.  
  38.     if (mdp->buffer == NULL)
  39.         blen = _mdgetbuf(mdp, blen);
  40.  
  41.     /*
  42.      * try to read an entire buffer.  if the file ends, but
  43.      * continues on another disk, calculate the remaining number
  44.      * of bytes to fill the buffer, and try to read them in.
  45.      */
  46.     for (cumm = 0; cumm < blen; cumm += ret)
  47.     {
  48.         len = blen - cumm;    /* number remaining to be read */
  49.         ret = read(mdp->fid, &mdp->buffer[cumm], len);
  50.         if (ret == -1)
  51.             return (EOF);    /* signal error right away */
  52.  
  53.         /*
  54.          * if we got some characters, check if we got enough.
  55.          * there might still be more to read, due to the way
  56.          * that microsoft does CR/LF translation.
  57.          */
  58.  
  59.         if (ret != 0)
  60.             continue;
  61.  
  62.         /*
  63.          * didn't get any this read.  if we managed to get some
  64.          * off this file (diskette), just cut our losses.
  65.          */
  66.  
  67.         if (cumm != 0)
  68.             break;
  69.  
  70.         /* otherwise, try the next diskette */
  71.  
  72.         if (mdp->ateof(mdp, _MDREAD))
  73.         {
  74.             /* real-final EOF, so don't close the file */
  75.             mdp->flags |= _MDEOF;
  76.             mdp->bufptr = mdp->buffer + blen;
  77.             return (EOF);
  78.         }
  79.  
  80.         _mdclose(mdp);
  81.         mdp->prompt(mdp);
  82.         if (_mdopen(mdp) == -1)
  83.             break;        /* something went wrong */
  84.     }
  85.  
  86.     mdp->bufptr = mdp->buffer + (blen - cumm);
  87.     mdp->bufcnt = cumm;        /* record actual number of bytes */
  88.  
  89.     if (cumm < blen)
  90.         memmove(mdp->bufptr, mdp->buffer, cumm);
  91.  
  92.     /* mark buffer as having a read done on it (in case of RDWR) */
  93.     mdp->flags |= _MDREAD;
  94.     mdp->flags &= ~_MDEMPTY;    /* no longer empty */
  95.     return (mdgetc(mdp));
  96. }
  97.