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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btfirst.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.      btfirst - first btree key
  13.  
  14. SYNOPSIS
  15.      #include <btree.h>
  16.  
  17.      int btfirst(btp)
  18.      btree_t *btp;
  19.  
  20. DESCRIPTION
  21.      The btfirst function positions the cursor of btree btp on the first
  22.      key in that btree.
  23.  
  24.      btfirst 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.      btkeycnt, btlast, 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 btfirst(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 first key */
  65.     btp->cbtpos.node = btp->bthdr.first;
  66.     btp->cbtpos.key = 1;
  67.  
  68.     /* check if tree is empty */
  69.     if (btp->cbtpos.node == 0) {
  70.         btp->cbtpos.key = 0;
  71.         bt_ndinit(btp, btp->cbtnp);
  72.         errno = BTENKEY;
  73.         return -1;
  74.     }
  75.  
  76.     /* read current node */
  77.     rs = bt_ndget(btp, btp->cbtpos.node, btp->cbtnp);
  78.     if (rs == -1) {
  79.         BTEPRINT;
  80.         btp->cbtpos.node = 0;
  81.         btp->cbtpos.key = 0;
  82.         bt_ndinit(btp, btp->cbtnp);
  83.         return -1;
  84.     }
  85.  
  86.     errno = 0;
  87.     return 0;
  88. }
  89.