home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / mdcopy / part02 / mdwrite.c < prev   
Encoding:
C/C++ Source or Header  |  1989-11-02  |  926 b   |  46 lines

  1. /*
  2.  * Created 1989 by greg yachuk.  Placed in the public domain.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "mdiskio.h"
  7.  
  8. /*
  9.  * mdwrite -    write `nitems' of `size' bytes from a buffer to a file.
  10.  *        return the number of items completely written.
  11.  */
  12. mdwrite(ptr, size, nitems, mdp)
  13. char   *ptr;
  14. int    size;
  15. int    nitems;
  16. MDFILE *mdp;
  17. {
  18.     int    len;            /* number of bytes to transfer */
  19.     long    lsize;            /* size left to transfer */
  20.  
  21.     lsize = nitems * size;        /* total transfer size */
  22.  
  23.     while (lsize > 0)
  24.     {
  25.         /* force a flush, if necessary */
  26.         mdputc(0, mdp);
  27.         if (mdeof(mdp))
  28.             return (nitems - (lsize / size));
  29.         --mdp->bufptr;
  30.         ++mdp->bufcnt;
  31.  
  32.         /* don't put more in the buffer than it can hold */
  33.         len = (lsize < mdp->bufcnt) ? lsize : mdp->bufcnt;
  34.         memcpy(mdp->bufptr, ptr, len);
  35.  
  36.         /* adjust all the counts */
  37.         lsize -= len;
  38.         ptr += len;
  39.         mdp->bufptr += len;
  40.         mdp->bufcnt -= len;
  41.     }
  42.  
  43.     /* all items got written */
  44.     return (nitems);
  45. }
  46.