home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CBASE101.ZIP / BLKIO112.ZIP / BSYNC.C < prev    next >
Text File  |  1990-06-20  |  2KB  |  91 lines

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