home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c023 / 1.img / PROGRAMS / CHGKEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-01  |  1.2 KB  |  42 lines

  1. /* (C) Copyright 1984,85,86,87 Walter L. Peacock    All Rights Reserved    */
  2. /****************************************************************************
  3.  *
  4.  *
  5.  *   chgkey.c    --    CBTREE sample support function to change a key.
  6.  *
  7.  */
  8.  
  9. #include "cbtree.h"
  10.  
  11. chgkey(fdidx, btcp, newkey)
  12. int fdidx;                    /* index file descriptor            */
  13. struct btcommo *btcp;        /* should contain OLD key & location */
  14. char *newkey;                /* new value to use for key */
  15. {
  16.     extern char *calloc();
  17.     int cbcode;    /* cbtree result code */
  18.     char *savkey;
  19.  
  20.     if ((savkey = calloc(btcp->btkeylen+1, 1)) == NULL)
  21.           return (NOMEM);
  22.     strnncpy(savkey, btcp->btkey, btcp->btkeylen);
  23.     btcp->btoptype = DELTKY;    /* delete key only */
  24.     if ((cbcode = cbtree(0, fdidx, btcp)) != BTCALLOK)
  25.         goto key0_exit;
  26.     btcp->btoptype = ISRTKY;    /* new key */
  27.     strnncpy(btcp->btkey, newkey, btcp->btkeylen);
  28.     if ((cbcode = cbtree(0, fdidx, btcp)) == BTCALLOK)
  29.         goto key0_exit;
  30.  
  31.     /* error: restore data base */
  32.     btcp->btoptype = DELTKY;    /* delete NEW key */
  33.     cbtree(0, fdidx, btcp);
  34.     btcp->btoptype = ISRTKY;    /* restore save  key */
  35.     strnncpy(btcp->btkey, savkey, btcp->btkeylen);
  36.     cbtree(0, fdidx, btcp);
  37.  
  38. key0_exit:
  39.           free(savkey);
  40.     return (cbcode);
  41. }
  42.