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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bsetvbuf.c    1.1 - 89/07/03" */
  5.  
  6. #include <errno.h>
  7. /* #include <stdlib.h> */
  8. #include "blkio_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      bsetvbuf - assign buffering to a block file
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bsetvbuf(bp, buf, blksize, bufcnt)
  18.      BLKFILE *bp;
  19.      void *buf;
  20.      size_t blksize;
  21.      size_t bufcnt;
  22.  
  23. DESCRIPTION
  24.      The bsetvbuf function causes the buffer block count and block size of
  25.      the block file associated with BLKFILE pointer bp to be changed to
  26.      bufcnt and blksize, with the same effects as if the file had been opened
  27.      with these values.  If bufcnt has a value of 0, the file will be
  28.      completely unbuffered.  If buf is not the NULL pointer, it is used as
  29.      the buffer area, in which case is must point to a storage area of size
  30.      (header size + bufffer count * block size).
  31.  
  32.      bsetvbuf may be called at any time after opening the block file, before
  33.      and after it is read or written.
  34.  
  35.      bsetvbuf will fail if one or more of the following is true:
  36.  
  37.      [EINVAL]       bp is not a valid BLKFILE pointer.
  38.      [EINVAL]       blksize is less than 1.
  39.      [BENOPEN]      bp is not open.
  40.  
  41. SEE ALSO
  42.      bopen, bsetbuf.
  43.  
  44. DIAGNOSTICS
  45.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  46.      non-zero value is returned, and errno set to indicate the error.
  47.  
  48. ------------------------------------------------------------------------------*/
  49. int bsetvbuf(bp, buf, blksize, bufcnt)
  50. BLKFILE * bp;
  51. void *    buf;
  52. size_t    blksize;
  53. size_t    bufcnt;
  54. {
  55.     int rs = 0;
  56.  
  57.     errno = 0;
  58.  
  59.     /* validate arguments */
  60.     if ((!b_valid(bp)) || (blksize == 0)) {
  61.         errno = EINVAL;
  62.         return -1;
  63.     }
  64.  
  65.     /* check if not open */
  66.     if (!(bp->flags & BIOOPEN)) {
  67.         errno = BENOPEN;
  68.         return -1;
  69.     }
  70.  
  71.     /* synchronize file with buffers */
  72.     rs = bsync(bp);
  73.     if (rs == -1) {
  74.         BEPRINT;
  75.         return -1;
  76.     }
  77.  
  78.     /* free old buffer storage */
  79.     b_free(bp);
  80.  
  81.     /* load main body of buf control structure */
  82.     bp->flags &= ~BIOUSRBUF;
  83.     bp->blksize = blksize;
  84.     bp->bufcnt = bufcnt;
  85.     bp->endblk = 0;
  86.     bp->most = 0;
  87.     bp->least = 0;
  88.     rs = b_uendblk(bp, &(bp->endblk));
  89.     if (rs == -1) {
  90.         if ((errno != BEBOUND) && (errno != BEEOF)) BEPRINT;
  91.         return -1;
  92.     }
  93.     /* check if not buffered */
  94.     if (bp->bufcnt == 0) {
  95.         errno = 0;
  96.         return 0;
  97.     }
  98.  
  99.     /* check if user supplied buffer */
  100.     if (buf != NULL) {
  101.         bp->flags |= BIOUSRBUF;
  102.         bp->blkbuf = buf;
  103.     }
  104.  
  105.     /* allocate buffer storage */
  106.     rs = b_alloc(bp);
  107.     if (rs == -1) {
  108.         BEPRINT;
  109.         return -1;
  110.     }
  111.  
  112.     /* initialize */
  113.     rs = b_init(bp);
  114.     if (rs == -1) {
  115.         BEPRINT;
  116.         return NULL;
  117.     }
  118.  
  119.     errno = 0;
  120.     return 0;
  121. }
  122.