home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / BLKIO10.ZIP / BSYNC.C < prev    next >
Text File  |  1989-08-30  |  2KB  |  109 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bsync.c    1.1 - 89/07/03" */
  5.  
  6. #include <errno.h>
  7. #include "blkio_.h"
  8.  
  9. /*man---------------------------------------------------------------------------
  10. NAME
  11.      bsync - synchronize a block file
  12.  
  13. SYNOPSIS
  14.      #include <blkio.h>
  15.  
  16.      int bsync(bp)
  17.      BLKFILE *bp;
  18.  
  19. DESCRIPTION
  20.      The bsync function causes the block file associated with BLKFILE
  21.      pointer bp to be synchronized with its buffers; any buffered data which
  22.      has been written to the buffers is written to the file.  The header, if
  23.      it has been modified, is written out last.  The block file remains open
  24.      and the buffers retain their contents.  If bp is open read-only or is
  25.      not buffered, there will be no data to synchronize and bsync will return
  26.      a value of 0 immediately.
  27.  
  28.      bsync will fail if one or more of the following is true:
  29.  
  30.      [EINVAL]       bp is not a valid BLKFILE pointer.
  31.      [BENOPEN]      bp is not open.
  32.  
  33. SEE ALSO
  34.      bexit, bflush, bputb.
  35.  
  36. DIAGNOSTICS
  37.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  38.      value of -1 is returned, and errno set to indicate the error.
  39.  
  40. ------------------------------------------------------------------------------*/
  41. int bsync(bp)
  42. BLKFILE *bp;
  43. {
  44.     int    rs    = 0;
  45.     int    i    = 0;
  46. #ifdef DEBUG
  47.     bpos_t    endblk    = 0;
  48. #endif
  49.  
  50.     errno = 0;
  51.  
  52.     /* validate arguments */
  53.     if (!b_valid(bp)) {
  54.         errno = EINVAL;
  55.         return -1;
  56.     }
  57.  
  58.     /* check if not open */
  59.     if (!(bp->flags & BIOOPEN)) {
  60.         errno = BENOPEN;
  61.         return -1;
  62.     }
  63.  
  64.     /* check if not open for writing */
  65.     if (!(bp->flags & BIOWRITE)) {
  66.         errno = 0;
  67.         return 0;
  68.     }
  69.  
  70.     /* check if not buffered */
  71.     if (bp->bufcnt == 0) {
  72.         errno = 0;
  73.         return 0;
  74.     }
  75.  
  76.     /* sync all blocks */
  77.     for (i = 1; i <= bp->bufcnt; i++) {
  78.         rs = b_put(bp, (size_t)i);
  79.         if (rs == -1) {
  80.             BEPRINT;
  81.             return -1;
  82.         }
  83.     }
  84.  
  85.     /* sync header */
  86.     rs = b_put(bp, (size_t)0);
  87.     if (rs == -1) {
  88.         BEPRINT;
  89.         return -1;
  90.     }
  91.  
  92. #ifdef DEBUG
  93.     /* check block count */
  94.     rs = b_uendblk(bp, &endblk);
  95.     if (rs == -1) {
  96.         BEPRINT;
  97.         return -1;
  98.     }
  99.     if (bp->endblk != endblk) {
  100.         BEPRINT;
  101.         errno = BEPANIC;
  102.         return -1;
  103.     }
  104. #endif
  105.  
  106.     errno = 0;
  107.     return 0;
  108. }
  109.