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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bsetvbuf.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.      bsetvbuf - assign buffering to a block file
  16.  
  17. SYNOPSIS
  18.      #include <blkio.h>
  19.  
  20.      int bsetvbuf(bp, buf, blksize, bufcnt)
  21.      BLKFILE *bp;
  22.      void *buf;
  23.      size_t blksize;
  24.      size_t bufcnt;
  25.  
  26. DESCRIPTION
  27.      The bsetvbuf function causes the block size and buffer block
  28.      count of the block file associated with BLKFILE pointer bp to be
  29.      changed to bufcnt and blksize, with the same effects as if the
  30.      file had been opened with these values.  If bufcnt has a value of
  31.      zero, the file will be completely unbuffered.  If buf is not the
  32.      NULL pointer, the storage area it points to will be used instead
  33.      of one automatically allocated for buffering.  In this case, buf
  34.      must point to a storage area of size no less than
  35.  
  36.           header size + block size * buffer count
  37.  
  38.      bsetvbuf may be called at any time after opening the block file,
  39.      before and after it is read or written.
  40.  
  41.      bsetvbuf will fail if one or more of the following is true:
  42.  
  43.      [EINVAL]       bp is not a valid BLKFILE pointer.
  44.      [EINVAL]       blksize is less than 1.
  45.      [ENOMEM]       Enough memory is not available for the
  46.                     calling process to allocate.
  47.      [BENOPEN]      bp is not open.
  48.  
  49. SEE ALSO
  50.      bopen, bsetbuf.
  51.  
  52. DIAGNOSTICS
  53.      Upon successful completion, a value of 0 is returned.  Otherwise,
  54.      a value of -1 is returned, and errno set to indicate the error.
  55.  
  56. NOTES
  57.      A common source of error is allocating buffer space as an
  58.      automatic variable in a code block, and then failing to close the
  59.      block file in the same block.
  60.  
  61. ------------------------------------------------------------------------------*/
  62. int bsetvbuf(bp, buf, blksize, bufcnt)
  63. BLKFILE *bp;
  64. void *buf;
  65. size_t blksize;
  66. size_t bufcnt;
  67. {
  68.     /* validate arguments */
  69.     if (!b_valid(bp) || blksize == 0) {
  70.         errno = EINVAL;
  71.         return -1;
  72.     }
  73.  
  74.     /* check if not open */
  75.     if (!(bp->flags & BIOOPEN)) {
  76.         errno = BENOPEN;
  77.         return -1;
  78.     }
  79.  
  80.     /* synchronize file with buffers */
  81.     if (bsync(bp) == -1) {
  82.         BEPRINT;
  83.         return -1;
  84.     }
  85.  
  86.     /* free old buffer storage */
  87.     b_free(bp);
  88.  
  89.     /* modify BLKFILE control structure contents */
  90.     bp->flags &= ~BIOUSRBUF;
  91.     bp->blksize = blksize;
  92.     bp->bufcnt = bufcnt;
  93.     bp->endblk = 0;
  94.     bp->most = 0;
  95.     bp->least = 0;
  96.     if (b_uendblk(bp, &bp->endblk) == -1) {
  97.         BEPRINT;
  98.         return -1;
  99.     }
  100.  
  101.     /* check if not buffered */
  102.     if (bp->bufcnt == 0) {
  103.         errno = 0;
  104.         return 0;
  105.     }
  106.  
  107.     /* set user supplied buffer flag */
  108.     if (buf != NULL) {
  109.         bp->flags |= BIOUSRBUF;
  110.     }
  111.  
  112.     /* allocate memory for bp */
  113.     if (b_alloc(bp) == -1) {
  114.         BEPRINT;
  115.         return -1;
  116.     }
  117.  
  118.     /* connect user supplied buffer */
  119.     if (bp->flags & BIOUSRBUF) {
  120.         bp->blkbuf = buf;
  121.     }
  122.  
  123.     /* initialize linked list of buffers */
  124.     if (b_initlist(bp) == -1) {
  125.         BEPRINT;
  126.         return -1;
  127.     }
  128.  
  129.     errno = 0;
  130.     return 0;
  131. }
  132.