xref: /csrg-svn/lib/libc/db/recno/rec_utils.c (revision 57933)
150995Sbostic /*-
250995Sbostic  * Copyright (c) 1990 The Regents of the University of California.
350995Sbostic  * All rights reserved.
450995Sbostic  *
550995Sbostic  * %sccs.include.redist.c%
650995Sbostic  */
750995Sbostic 
850995Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*57933Sbostic static char sccsid[] = "@(#)rec_utils.c	5.6 (Berkeley) 02/11/93";
1050995Sbostic #endif /* LIBC_SCCS and not lint */
1150995Sbostic 
1250995Sbostic #include <sys/param.h>
1356761Sbostic 
1450995Sbostic #include <stdio.h>
1550995Sbostic #include <stdlib.h>
1650995Sbostic #include <string.h>
1756761Sbostic 
18*57933Sbostic #include <db.h>
1951093Sbostic #include "recno.h"
2050995Sbostic 
2150995Sbostic /*
2250995Sbostic  * __REC_RET -- Build return data as a result of search or scan.
2350995Sbostic  *
2450995Sbostic  * Parameters:
2550995Sbostic  *	t:	tree
2650995Sbostic  *	d:	LEAF to be returned to the user.
2750995Sbostic  *	data:	user's data structure
2850995Sbostic  *
2950995Sbostic  * Returns:
3050995Sbostic  *	RET_SUCCESS, RET_ERROR.
3150995Sbostic  */
3250995Sbostic int
3356761Sbostic __rec_ret(t, e, nrec, key, data)
3450995Sbostic 	BTREE *t;
3550995Sbostic 	EPG *e;
3656761Sbostic 	recno_t nrec;
3756761Sbostic 	DBT *key, *data;
3850995Sbostic {
3950995Sbostic 	register RLEAF *rl;
4056761Sbostic 	register void *p;
4150995Sbostic 
4256761Sbostic 	if (data == NULL)
4356761Sbostic 		goto retkey;
4456761Sbostic 
4550995Sbostic 	rl = GETRLEAF(e->page, e->index);
4656761Sbostic 
4750995Sbostic 	if (rl->flags & P_BIGDATA) {
4850995Sbostic 		if (__ovfl_get(t, rl->bytes,
4950995Sbostic 		    &data->size, &t->bt_dbuf, &t->bt_dbufsz))
5050995Sbostic 			return (RET_ERROR);
5150995Sbostic 	} else {
5254285Sbostic 		/* Use +1 in case the first record retrieved is 0 length. */
5354285Sbostic 		if (rl->dsize + 1 > t->bt_dbufsz) {
5454285Sbostic 			if ((p = realloc(t->bt_dbuf, rl->dsize + 1)) == NULL)
5550995Sbostic 				return (RET_ERROR);
5651093Sbostic 			t->bt_dbuf = p;
5754285Sbostic 			t->bt_dbufsz = rl->dsize + 1;
5850995Sbostic 		}
5956998Sbostic 		bcopy(rl->bytes, t->bt_dbuf, rl->dsize);
6050995Sbostic 		data->size = rl->dsize;
6150995Sbostic 	}
6250995Sbostic 	data->data = t->bt_dbuf;
6350995Sbostic 
6456761Sbostic retkey:	if (key == NULL)
6556761Sbostic 		return (RET_SUCCESS);
6656761Sbostic 
6756761Sbostic 	if (sizeof(recno_t) > t->bt_kbufsz) {
6856761Sbostic 		if ((p = realloc(t->bt_kbuf, sizeof(recno_t))) == NULL)
6956761Sbostic 			return (RET_ERROR);
7056761Sbostic 		t->bt_kbuf = p;
7156761Sbostic 		t->bt_kbufsz = sizeof(recno_t);
7256761Sbostic 	}
7356761Sbostic 	bcopy(&nrec, t->bt_kbuf, sizeof(recno_t));
7456761Sbostic 	key->size = sizeof(recno_t);
7556761Sbostic 	key->data = t->bt_kbuf;
7650995Sbostic 	return (RET_SUCCESS);
7750995Sbostic }
78