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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bflpush.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.      bflpush - push block onto free list
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bflpush(bp, bn_p)
  18.      BLKFILE *bp;
  19.      bpos_t *bn_p;
  20.  
  21. DESCRIPTION
  22.      The bflpush function puts the block number pointed to by bn_p into
  23.      the free list of the block file associated with BLKFILE pointer bp.
  24.      If the value pointed to by bn_p is one block past the end of the
  25.      file, it is accepted but not actually added to the list.
  26.  
  27.      bflpush will fail if one or more of the following is true:
  28.  
  29.      [EINVAL]       bp is not a valid BLKFILE pointer.
  30.      [EINVAL]       bn_p is the NULL pointer.
  31.      [EINVAL]       bn_p points to a value less than 1.
  32.      [BEEOF]        bp is empty.
  33.      [BEEOF]        bn_p points to a value farther than one
  34.                     block past the end of file.
  35.      [BENFL]        bp does not have a free list.
  36.      [BENOPEN]      bp is not open for writing.
  37.  
  38. SEE ALSO
  39.      bflpop.
  40.  
  41. DIAGNOSTICS
  42.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  43.      value of -1 is returned, and errno set to indicate the error.
  44.  
  45. NOTES
  46.      To use the free list functions in the blkio library, the first element
  47.      of the file header must be the free list head with type size_t.  This
  48.      must be initialized to 0 immediately after the file is created and not
  49.      accessed afterward except using bflpush and bflpush.  Also, the block
  50.      size must be at least sizeof(size_t).
  51.  
  52. ------------------------------------------------------------------------------*/
  53. int bflpush(bp, bn_p)
  54. BLKFILE * bp;
  55. bpos_t *  bn_p;
  56. {
  57.     int    rs        = 0;
  58.     bpos_t    oldflhno    = 0;
  59.     bpos_t    newflhno    = 0;
  60.     void *    buf        = NULL;
  61.  
  62.     errno = 0;
  63.  
  64.     /* validate arguments */
  65.     if ((!b_valid(bp)) || (bn_p == NULL)) {
  66.         errno = EINVAL;
  67.         return -1;
  68.     }
  69.     if (*bn_p < 1) {
  70.         errno = EINVAL;
  71.         return -1;
  72.     }
  73.  
  74.     /* check if not open */
  75.     if (!(bp->flags & BIOWRITE)) {
  76.         errno = BENOPEN;
  77.         return -1;
  78.     }
  79.  
  80.     /* check if no free list */
  81.     if ((bp->hdrsize < sizeof(oldflhno)) || (bp->blksize < sizeof(oldflhno))) {
  82.         errno = BENFL;
  83.         return -1;
  84.     }
  85.  
  86.     /* check if header not yet written */
  87.     if (bp->endblk < 1) {
  88.         errno = BEEOF;
  89.         return -1;
  90.     }
  91.  
  92.     /* check if block past endblk */
  93.     if (*bn_p > bp->endblk) {
  94.         errno = BEEOF;
  95.         return -1;
  96.     }
  97.  
  98.     /* check if block is endblk */
  99.     if (*bn_p == bp->endblk) {
  100.         errno = 0;
  101.         return 0;
  102.     }
  103.  
  104.     /* get block number of current free list head */
  105.     rs = bgethf(bp, (size_t)0, (void *)&oldflhno, sizeof(oldflhno));
  106.     if (rs == -1) {
  107.         BEPRINT;
  108.         return -1;
  109.     }
  110.  
  111.     /* link to rest of free list */
  112.     newflhno = *bn_p;
  113.     buf = calloc(1, bp->blksize);
  114.     if (buf == NULL) {
  115.         BEPRINT;
  116.         errno = ENOMEM;
  117.         return -1;
  118.     }
  119.     memcpy(buf, (void *)&oldflhno, sizeof(oldflhno));
  120.     rs = bputb(bp, newflhno, buf);
  121.     if (rs == -1) {
  122.         BEPRINT;
  123.         free(buf);
  124.         buf = NULL;
  125.         return -1;
  126.     }
  127.     free(buf);
  128.     buf = NULL;
  129.  
  130.     /* write new free list head number to header */
  131.     rs = bputhf(bp, (size_t)0, (void *)&newflhno, sizeof(newflhno));
  132.     if (rs == -1) {
  133.         BEPRINT;
  134.         return -1;
  135.     }
  136.  
  137.     errno = 0;
  138.     return 0;
  139. }
  140.