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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btsearch.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.      btsearch - search a btree
  13.  
  14. SYNOPSIS
  15.      #include <btree.h>
  16.  
  17.      int btsearch(btp, buf)
  18.      btree_t *btp;
  19.      void *buf;
  20.  
  21. DESCRIPTION
  22.      The btsearch function searches the btree btp for the key pointed to
  23.      by buf.  If it is found, the cursor is set to the location of the key
  24.      and 1 is returned.  If it is not found, the cursor is set to the next
  25.      higher key and 0 is returned.
  26.  
  27.      btsearch will fail if one or more of the following is true:
  28.  
  29.      [EINVAL]       btp is not a valid btree pointer.
  30.      [EINVAL]       buf is the NULL pointer.
  31.      [BTELOCK]      btp is not read locked.
  32.  
  33. SEE ALSO
  34.      btdelcur, btdelete, btinsert.
  35.  
  36. DIAGNOSTICS
  37.      Upon successful completion, a value of 1 is returned if the key was
  38.      found or a value of 0 if it was not found.  On failure, a value of
  39.      -1 is returned, and errno set to indicate the error.
  40.      
  41. ------------------------------------------------------------------------------*/
  42. int btsearch(btp, buf)
  43. btree_t * btp;
  44. void *    buf;
  45. {
  46.     int    rs    = 0;
  47.     int    found    = 0;
  48.  
  49.     errno = 0;
  50.  
  51.     /* validate arguments */
  52.     if (!bt_valid(btp)) {
  53.         errno = EINVAL;
  54.         return -1;
  55.     }
  56.     if (buf == NULL) {
  57.         errno = EINVAL;
  58.         return -1;
  59.     }
  60.  
  61.     /* check locks */
  62.     if (!(btp->flags & BTRDLCK)) {
  63.         errno = BTELOCK;
  64.         return -1;
  65.     }
  66.  
  67.     /* search to position to insert */
  68.     found = bt_search(btp, buf);
  69.     if (found == -1) {
  70.         BTEPRINT;
  71.         return -1;
  72.     }
  73.  
  74.     /* check if cursor on empty slot */
  75.     if (btp->cbtpos.key > btp->cbtnp->n) {
  76.         if (found != 0) BTEPRINT;
  77.         btp->cbtpos.key = btp->cbtnp->n;
  78.         rs = btnext(btp);
  79.         if (rs == -1) {
  80.             BTEPRINT;
  81.             return -1;
  82.         }
  83.     }
  84.  
  85.     errno = 0;
  86.     return ((found == 1) ? 1: 0);
  87. }
  88.