home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BTREE.ZIP
/
BTSEARCH.C
< prev
next >
Wrap
Text File
|
1989-08-31
|
2KB
|
88 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "btsearch.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <errno.h>
#include "btree_.h"
/*man---------------------------------------------------------------------------
NAME
btsearch - search a btree
SYNOPSIS
#include <btree.h>
int btsearch(btp, buf)
btree_t *btp;
void *buf;
DESCRIPTION
The btsearch function searches the btree btp for the key pointed to
by buf. If it is found, the cursor is set to the location of the key
and 1 is returned. If it is not found, the cursor is set to the next
higher key and 0 is returned.
btsearch will fail if one or more of the following is true:
[EINVAL] btp is not a valid btree pointer.
[EINVAL] buf is the NULL pointer.
[BTELOCK] btp is not read locked.
SEE ALSO
btdelcur, btdelete, btinsert.
DIAGNOSTICS
Upon successful completion, a value of 1 is returned if the key was
found or a value of 0 if it was not found. On failure, a value of
-1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int btsearch(btp, buf)
btree_t * btp;
void * buf;
{
int rs = 0;
int found = 0;
errno = 0;
/* validate arguments */
if (!bt_valid(btp)) {
errno = EINVAL;
return -1;
}
if (buf == NULL) {
errno = EINVAL;
return -1;
}
/* check locks */
if (!(btp->flags & BTRDLCK)) {
errno = BTELOCK;
return -1;
}
/* search to position to insert */
found = bt_search(btp, buf);
if (found == -1) {
BTEPRINT;
return -1;
}
/* check if cursor on empty slot */
if (btp->cbtpos.key > btp->cbtnp->n) {
if (found != 0) BTEPRINT;
btp->cbtpos.key = btp->cbtnp->n;
rs = btnext(btp);
if (rs == -1) {
BTEPRINT;
return -1;
}
}
errno = 0;
return ((found == 1) ? 1: 0);
}