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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bputhf.c    1.1 - 89/07/03" */
  5.  
  6. #include <errno.h>
  7. /* #include <string.h> */
  8. #include "blkio_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      bputhf - put a header field into a block file
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bputhf(bp, offset, buf, bufsize)
  18.      BLKFILE *bp;
  19.      size_t offset;
  20.      void *buf;
  21.      size_t bufsize;
  22.  
  23. DESCRIPTION
  24.      The bputhf function writes the contents of buf into a field of the
  25.      header of the block file associated with BLKFILE pointer bp.  The field
  26.      begins offset characters from the beginning of the header and is bufsize
  27.      characters long.  buf must point to a storage area at least bufsize
  28.      characters long.
  29.  
  30.      bputhf will fail if one or more of the following is true:
  31.  
  32.      [EINVAL]       bp is not a valid BLKFILE pointer.
  33.      [EINVAL]       bufsize is less than 1.
  34.      [EINVAL]       buf is the NULL pointer.
  35.      [BEBOUND]      offset + bufsize extends beyond the boundary
  36.                     of the header.
  37.      [BEEOF]        Attempt to write a field before the complete
  38.                     header has been written.
  39.      [BENOPEN]      bp is not open for writing.
  40.  
  41. SEE ALSO
  42.      bgethf, bputh, bputbf.
  43.  
  44. DIAGNOSTICS
  45.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  46.      value of -1 is returned, and errno set to indicate the error.
  47.  
  48. NOTES
  49.      When the header if created,  the entire header must be written, so
  50.      offset must have a value of 0 and bufsize must be equal to the block
  51.      size.
  52.  
  53. ------------------------------------------------------------------------------*/
  54. int bputhf(bp, offset, buf, bufsize)
  55. BLKFILE * bp;
  56. size_t    offset;
  57. void *    buf;
  58. size_t    bufsize;
  59. {
  60.     int rs = 0;
  61.  
  62.     errno = 0;
  63.  
  64.     /* validate arguments */
  65.     if (!b_valid(bp)) {
  66.         errno = EINVAL;
  67.         return -1;
  68.     }
  69.     if ((buf == NULL) || (bufsize < 1)) {
  70.         errno = EINVAL;
  71.         return -1;
  72.     }
  73.  
  74.     /* check if not open for reading */
  75.     if (!(bp->flags & BIOWRITE)) {
  76.         errno = BENOPEN;
  77.         return -1;
  78.     }
  79.  
  80.     /* check if block boundary is crossed */
  81.     if ((offset + bufsize) > bp->hdrsize) {
  82.         errno = BEBOUND;
  83.         return -1;
  84.     }
  85.  
  86.     /* check if incomplete header */
  87.     if ((offset != 0) || (bufsize != bp->hdrsize)) {
  88.         if (bp->endblk < 1) {
  89.             errno = BEEOF;
  90.             return -1;
  91.         }
  92.     }
  93.  
  94.     /* check if not buffered */
  95.     if (bp->bufcnt == 0) {
  96.         rs = b_uputf(bp, (bpos_t)0, offset, buf, bufsize);
  97.         if (rs == -1) {
  98.             BEPRINT;
  99.             return -1;
  100.         }
  101.         if (bp->endblk < 1) {
  102.             bp->endblk = 1;
  103.         }
  104.         errno = 0;
  105.         return 0;
  106.     }
  107.  
  108.     /* check if buffer is not loaded */
  109.     if (!(b_block_p(bp, (size_t)0)->flags & BLKREAD)) {
  110.         if ((offset != 0) || (bufsize != bp->hdrsize)) {
  111.             /* read block from file */
  112.             rs = b_get(bp, (size_t)0);
  113.             if (rs == -1) {
  114.                 if (errno != BEEOF) BEPRINT;
  115.                 return -1;
  116.             }
  117.         }
  118.     }
  119.  
  120.     /* copy from buf into block buffer and set flags */
  121.     memcpy((void *)((char *)b_blkbuf(bp, (size_t)0) + offset), buf, bufsize);
  122.     b_block_p(bp, (size_t)0)->flags = BLKREAD | BLKWRITE;
  123.  
  124.     /* adjust endblk */
  125.     if (bp->endblk < 1) {
  126.         bp->endblk = 1;
  127.     }
  128.  
  129.     errno = 0;
  130.     return 0;
  131. }
  132.