home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR41 / CBASE11.ZIP / BPUTHF.C < prev    next >
C/C++ Source or Header  |  1993-01-01  |  3KB  |  137 lines

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