home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BTREE.ZIP
/
BTCREATE.C
< prev
next >
Wrap
Text File
|
1989-08-31
|
3KB
|
130 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "btcreate.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <errno.h>
/* #include <string.h> */
#include "btree_.h"
/*man---------------------------------------------------------------------------
NAME
btcreate - create a btree
SYNOPSIS
int btcreate(filename, m, keysize)
char *filename;
size_t m;
size_t keysize;
DESCRIPTION
The btcreate function creates the file named by filename as a btree.
m is the degree of the tree and keysize is the size of the keys.
filename points to a character string that contains the name of the
file to be createed.
m is the degree of the tree and keysize is the size of the keys.
btcreate will fail if one or more of the following is true:
[EINVAL] filename is the NULL pointer.
[EINVAL] m is less than 3.
[EINVAL] keysize is less than 1.
[BTEMFILE] Too many open btrees. The maximum
is defined as BTOPEN_MAX in btree.h.
SEE ALSO
btopen.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int btcreate(filename, m, keysize)
char * filename;
size_t m;
size_t keysize;
{
int rs = 0;
int terrno = 0;
btree_t * btp = NULL;
errno = 0;
/* validate input parameters */
if ((filename == NULL) || (m < 3) || (keysize < 1)) {
errno = EINVAL;
return -1;
}
/* find free slot in btb table */
for (btp = btb; btp < (btb + BTOPEN_MAX); btp++) {
if (!(btp->flags & BTOPEN)) {
break; /* found */
}
}
if (btp >= btb + BTOPEN_MAX) {
errno = BTEMFILE;
return -1; /* no free slots */
}
/* load btree_t structure */
btp->bthdr.flh = 0;
btp->bthdr.m = m;
btp->bthdr.keysize = keysize;
btp->bthdr.flags = 0;
btp->bthdr.root = 0;
btp->bthdr.first = 0;
btp->bthdr.last = 0;
btp->bthdr.keycnt = 0;
btp->bthdr.height = 0;
btp->flags = BTREAD | BTWRITE;
btp->cmp_p = memcmp;
btp->cbtpos.node = 0; /* cursor */
btp->cbtpos.key = 0;
rs = bt_alloc(btp);
if (rs == -1) {
BTEPRINT;
memset((void *)btp, 0, sizeof(btb[0]));
btp->flags = 0;
return -1;
}
/* create file */
btp->bp = bopen(filename, "c", sizeof(bthdr_t), (size_t)1, (size_t)0);
if (btp->bp == NULL) {
if (errno != EEXIST) BTEPRINT;
bt_free(btp);
memset((void *)btp, 0, sizeof(btb[0]));
btp->flags = 0;
return -1;
}
/* write header to file */
rs = bputh(btp->bp, (void *)&btp->bthdr); /* header */
if (rs == -1) {
BTEPRINT;
terrno = errno;
bclose(btp->bp);
bt_free(btp);
memset((void *)btp, 0, sizeof(btb[0]));
btp->flags = 0;
errno = terrno;
return -1;
}
/* close btp */
rs = btclose(btp);
if (rs == -1) {
BTEPRINT;
bt_free(btp);
return -1;
}
errno = 0;
return 0;
}