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

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