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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btlist.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <bool.h>
  8. #include <btree.h>
  9. #include <errno.h>
  10. #include <stdio.h>
  11. /* #include <stdlib.h> */
  12. #define EXIT_SUCCESS    0    /* delete if you have stdlib.h */
  13. #define EXIT_FAILURE    1
  14.  
  15. int cmp(/* void *buf1, void *buf2, size_t count */);
  16.  
  17. #define USAGE    ("Usage: btlist [-r] filename")
  18.  
  19. /*man---------------------------------------------------------------------------
  20. NAME
  21.      btlist - list all keys in btree
  22.  
  23. SYNOPSIS
  24.      btlist [-r] filename
  25.  
  26. DESCRIPTION
  27.      The btlist command reads all of the keys from the btree file filename
  28.      and writes the total number of keys followed by the keys themselves in
  29.      ascending order to stdout.  The -r (raw) switch will cause the keys to
  30.      be written in a format suitable for piping to another command;  the
  31.      key count is suppressed and the keys are written with no formatting
  32.      except a newline separator.
  33.  
  34. SEE ALSO
  35.      bta, btdump, btree.
  36.  
  37. NOTES
  38.      btlist is written for btrees sorted using the strncmp function.  To
  39.      use it with with keys of a different type, modify the cmp function
  40.      in the file btlist.c.
  41.  
  42. ------------------------------------------------------------------------------*/
  43. int main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.     int        rs        = 0;
  48.     bool        raw        = FALSE;
  49.     char        filename[FILENAME_MAX];
  50.     int        i        = 0;
  51.     btree_t *    btp        = NULL;
  52.     void *        key_p        = NULL;
  53.     char        control[81];
  54.  
  55.     /* initialize storage */
  56.     memset((void *)filename, 0, sizeof(filename));
  57.  
  58.     /* process command line arguments */
  59.     if ((argc < 2) || (argc > 3)) {
  60.         printf("%s\n", USAGE);
  61.         bexit(EXIT_FAILURE);
  62.     }
  63.     if (argc == 2) {
  64.         strncpy(filename, argv[1], sizeof(filename));
  65.         filename[sizeof(filename) - 1] = '\0';
  66.     } else {
  67.         if (strcmp(argv[1], "-r") != 0) {
  68.             printf("%s\n", USAGE);
  69.             bexit(EXIT_FAILURE);
  70.         }
  71.         raw = TRUE;
  72.         strncpy(filename, argv[2], sizeof(filename));
  73.         filename[sizeof(filename) - 1] = '\0';
  74.     }
  75.  
  76.     /* open btree */
  77.     btp = btopen(filename, "r", cmp);
  78.     if (btp == NULL) {
  79.         if (errno == ENOENT) {
  80.             printf("B-tree %s does not exist.\n", filename);
  81.             bexit(EXIT_FAILURE);
  82.         }
  83.         printf("*** Error opening B-tree.  Error code %d.\n", errno);
  84.         bexit(EXIT_FAILURE);
  85.     }
  86.  
  87.     /* read lock btree */
  88.     rs = btlock(btp, BT_RDLKW);
  89.     if (rs == -1) {
  90.         printf("*** Error read locking B-tree.  Error code %d.\n", errno);
  91.         bexit(EXIT_FAILURE);
  92.     }
  93.  
  94.     if (!raw) {
  95.         sprintf(control, "%%4.4d: \"%%-%d.%ds\"\n", btp->bthdr.keysize, btp->bthdr.keysize);
  96.     } else {
  97.         sprintf(control, "%%-%d.%ds\n", btp->bthdr.keysize, btp->bthdr.keysize);
  98.     }
  99.  
  100.     key_p = calloc(1, btp->bthdr.keysize);
  101.     if (key_p == NULL) {
  102.         printf("*** Error allocating memory.\n", errno);
  103.         bexit(EXIT_FAILURE);
  104.     }
  105.  
  106.     if (!raw) {
  107.         printf("%lu keys in btree %s\n", (unsigned long)btkeycnt(btp), filename);
  108.         printf("-----------------------\n");
  109.     }
  110.  
  111.     for (i = 1; ; i++) {
  112.         rs = btnext(btp);
  113.         if (rs == -1) {
  114.             printf("*** Error finding next key.  Error code %d.\n", errno);
  115.             bexit(EXIT_FAILURE);
  116.         }
  117.         if (btcursor(btp) == NULL) {
  118.             break;
  119.         }
  120.         rs = btgetk(btp, key_p);
  121.         if (rs == -1) {
  122.             printf("*** Error reading key.  Error code %d.\n", errno);
  123.             bexit(EXIT_FAILURE);
  124.         }
  125.         if (!raw) {
  126.             printf(control, i, (char *)key_p);
  127.         } else {
  128.             printf(control, (char *)key_p);
  129.         }
  130.     }
  131.     free(key_p);
  132.     key_p = NULL;
  133.  
  134.     rs = btlock(btp, BT_UNLCK);
  135.     if (rs == -1) {
  136.         printf("*** Error unlocking B-tree.  Error code %d.\n", errno);
  137.         bexit(EXIT_FAILURE);
  138.     }
  139.  
  140.     /* close btree */
  141.     if (btclose(btp) == -1) {
  142.         printf("*** Error closing B-tree.  Error code %d.\n", errno);
  143.         bexit(EXIT_FAILURE);
  144.     }
  145.  
  146.     bexit(EXIT_SUCCESS);
  147. }
  148.  
  149. /* comparison function */
  150. int cmp(buf1, buf2, count)
  151. void *buf1;
  152. void *buf2;
  153. size_t count;
  154. {
  155.     return strncmp((char *)buf1, (char *)buf2, count);
  156. }
  157.