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 >
Wrap
Text File
|
1989-08-31
|
4KB
|
157 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "btlist.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <bool.h>
#include <btree.h>
#include <errno.h>
#include <stdio.h>
/* #include <stdlib.h> */
#define EXIT_SUCCESS 0 /* delete if you have stdlib.h */
#define EXIT_FAILURE 1
int cmp(/* void *buf1, void *buf2, size_t count */);
#define USAGE ("Usage: btlist [-r] filename")
/*man---------------------------------------------------------------------------
NAME
btlist - list all keys in btree
SYNOPSIS
btlist [-r] filename
DESCRIPTION
The btlist command reads all of the keys from the btree file filename
and writes the total number of keys followed by the keys themselves in
ascending order to stdout. The -r (raw) switch will cause the keys to
be written in a format suitable for piping to another command; the
key count is suppressed and the keys are written with no formatting
except a newline separator.
SEE ALSO
bta, btdump, btree.
NOTES
btlist is written for btrees sorted using the strncmp function. To
use it with with keys of a different type, modify the cmp function
in the file btlist.c.
------------------------------------------------------------------------------*/
int main(argc, argv)
int argc;
char *argv[];
{
int rs = 0;
bool raw = FALSE;
char filename[FILENAME_MAX];
int i = 0;
btree_t * btp = NULL;
void * key_p = NULL;
char control[81];
/* initialize storage */
memset((void *)filename, 0, sizeof(filename));
/* process command line arguments */
if ((argc < 2) || (argc > 3)) {
printf("%s\n", USAGE);
bexit(EXIT_FAILURE);
}
if (argc == 2) {
strncpy(filename, argv[1], sizeof(filename));
filename[sizeof(filename) - 1] = '\0';
} else {
if (strcmp(argv[1], "-r") != 0) {
printf("%s\n", USAGE);
bexit(EXIT_FAILURE);
}
raw = TRUE;
strncpy(filename, argv[2], sizeof(filename));
filename[sizeof(filename) - 1] = '\0';
}
/* open btree */
btp = btopen(filename, "r", cmp);
if (btp == NULL) {
if (errno == ENOENT) {
printf("B-tree %s does not exist.\n", filename);
bexit(EXIT_FAILURE);
}
printf("*** Error opening B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
/* read lock btree */
rs = btlock(btp, BT_RDLKW);
if (rs == -1) {
printf("*** Error read locking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (!raw) {
sprintf(control, "%%4.4d: \"%%-%d.%ds\"\n", btp->bthdr.keysize, btp->bthdr.keysize);
} else {
sprintf(control, "%%-%d.%ds\n", btp->bthdr.keysize, btp->bthdr.keysize);
}
key_p = calloc(1, btp->bthdr.keysize);
if (key_p == NULL) {
printf("*** Error allocating memory.\n", errno);
bexit(EXIT_FAILURE);
}
if (!raw) {
printf("%lu keys in btree %s\n", (unsigned long)btkeycnt(btp), filename);
printf("-----------------------\n");
}
for (i = 1; ; i++) {
rs = btnext(btp);
if (rs == -1) {
printf("*** Error finding next key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (btcursor(btp) == NULL) {
break;
}
rs = btgetk(btp, key_p);
if (rs == -1) {
printf("*** Error reading key. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (!raw) {
printf(control, i, (char *)key_p);
} else {
printf(control, (char *)key_p);
}
}
free(key_p);
key_p = NULL;
rs = btlock(btp, BT_UNLCK);
if (rs == -1) {
printf("*** Error unlocking B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
/* close btree */
if (btclose(btp) == -1) {
printf("*** Error closing B-tree. Error code %d.\n", errno);
bexit(EXIT_FAILURE);
}
bexit(EXIT_SUCCESS);
}
/* comparison function */
int cmp(buf1, buf2, count)
void *buf1;
void *buf2;
size_t count;
{
return strncmp((char *)buf1, (char *)buf2, count);
}