home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / BTREE.ZIP / BTLAST.C < prev    next >
Text File  |  1989-08-31  |  2KB  |  91 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btlast.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. #include "btree_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      btlast - last btree key
  13.  
  14. SYNOPSIS
  15.      #include <btree.h>
  16.  
  17.      int btlast(btp)
  18.      btree_t *btp;
  19.  
  20. DESCRIPTION
  21.      The btlast function positions the cursor of btree btp on the last
  22.      key in that btree.
  23.  
  24.      btlast will fail if one or more of the following is true:
  25.  
  26.      [EINVAL]       btp is not a valid btree pointer.
  27.      [BTELOCK]      btp is not locked.
  28.      [BTENKEY]      btp is empty.
  29.      [BTENOPEN]     btp is not open.
  30.  
  31. SEE ALSO
  32.      btfirst, btkeycnt, btnext, btprev.
  33.  
  34. DIAGNOSTICS
  35.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  36.      value of -1 is returned, and errno set to indicate the error.
  37.  
  38. ------------------------------------------------------------------------------*/
  39. int btlast(btp)
  40. btree_t *btp;
  41. {
  42.     int rs = 0;
  43.  
  44.     errno = 0;
  45.  
  46.     /* validate arguments */
  47.     if (!bt_valid(btp)) {
  48.         errno = EINVAL;
  49.         return -1;
  50.     }
  51.  
  52.     /* check if not open */
  53.     if (!(btp->flags & BTOPEN)) {
  54.         errno = BTENOPEN;
  55.         return -1;
  56.     }
  57.  
  58.     /* check locks */
  59.     if (!(btp->flags & BTLOCKS)) {
  60.         errno = BTELOCK;
  61.         return -1;
  62.     }
  63.  
  64.     /* set cursor to last key */
  65.     btp->cbtpos.node = btp->bthdr.last;
  66.  
  67.     /* check if tree is empty */
  68.     if (btp->cbtpos.node == 0) {
  69.         btp->cbtpos.key = 0;
  70.         bt_ndinit(btp, btp->cbtnp);
  71.         errno = BTENKEY;
  72.         return -1;
  73.     }
  74.  
  75.     /* read current node */
  76.     rs = bt_ndget(btp, btp->cbtpos.node, btp->cbtnp);
  77.     if (rs == -1) {
  78.         BTEPRINT;
  79.         btp->cbtpos.node = 0;
  80.         btp->cbtpos.key = 0;
  81.         bt_ndinit(btp, btp->cbtnp);
  82.         return -1;
  83.     }
  84.  
  85.     /* set cursor to last key in node */
  86.     btp->cbtpos.key = btp->cbtnp->n;
  87.  
  88.     errno = 0;
  89.     return 0;
  90. }
  91.