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

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