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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bgethf.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.      bgethf - get a header field from a block file
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bgethf(bp, offset, buf, bufsize)
  18.      BLKFILE *bp;
  19.      size_t offset;
  20.      void *buf;
  21.      size_t bufsize;
  22.  
  23. DESCRIPTION
  24.      The bgethf function reads a field from the header in the block file
  25.      associated with BLKFILE pointer bp into buf.  The field begins offset
  26.      characters from the beginning of the header and is bufsize characters
  27.      long.  buf must point to a storage area at least bufsize characters
  28.      long.
  29.  
  30.      bgethf will fail if one or more of the following is true:
  31.  
  32.      [EINVAL]       bp is not a valid BLKFILE pointer.
  33.      [EINVAL]       bn or 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.      [BEBOUND]      End of file not on block boundary.
  38.      [BEEOF]        bp is empty.
  39.      [BENOPEN]      bp is not open for reading.
  40.  
  41. SEE ALSO
  42.      bgeth, bgetbf, bputhf.
  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. ------------------------------------------------------------------------------*/
  49. int bgethf(bp, offset, buf, bufsize)
  50. BLKFILE * bp;
  51. size_t    offset;
  52. void *    buf;
  53. size_t    bufsize;
  54. {
  55.     int rs = 0;
  56.  
  57.     errno = 0;
  58.  
  59.     /* validate arguments */
  60.     if (!b_valid(bp)) {
  61.         errno = EINVAL;
  62.         return -1;
  63.     }
  64.     if ((buf == NULL) || (bufsize < 1)) {
  65.         errno = EINVAL;
  66.         return -1;
  67.     }
  68.  
  69.     /* check if not open for reading */
  70.     if (!(bp->flags & BIOREAD)) {
  71.         errno = BENOPEN;
  72.         return -1;
  73.     }
  74.  
  75.     /* check if block boundary is crossed */
  76.     if ((offset + bufsize) > bp->hdrsize) {
  77.         errno = BEBOUND;
  78.         return -1;
  79.     }
  80.  
  81.     /* check if header not yet written */
  82.     if (bp->endblk < 1) {
  83.         errno = BEEOF;
  84.         return -1;
  85.     }
  86.  
  87.     /* check if not buffered */
  88.     if (bp->bufcnt == 0) {
  89.         rs = b_ugetf(bp, (size_t)0, offset, buf, bufsize);
  90.         if (rs == -1) {
  91.             BEPRINT;
  92.             return -1;
  93.         }
  94.         errno = 0;
  95.         return 0;
  96.     }
  97.  
  98.     /* check if buffer is not loaded */
  99.     if (!(b_block_p(bp, (size_t)0)->flags & BLKREAD)) {
  100.         /* read header from file */
  101.         b_block_p(bp, (size_t)0)->bn = 0;
  102.         rs = b_get(bp, (size_t)0);
  103.         if (rs == -1) {
  104.             BEPRINT;
  105.             return -1;
  106.         }
  107.     }
  108.  
  109.     /* copy from block buffer into buf */
  110.     memcpy(buf, (void *)((char *)b_blkbuf(bp, (size_t)0) + offset), bufsize);
  111.  
  112.     errno = 0;
  113.     return 0;
  114. }
  115.