xref: /original-bsd/lib/libc/db/recno/rec_utils.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)rec_utils.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include <db.h>
19 #include "recno.h"
20 
21 /*
22  * __REC_RET -- Build return data as a result of search or scan.
23  *
24  * Parameters:
25  *	t:	tree
26  *	d:	LEAF to be returned to the user.
27  *	data:	user's data structure
28  *
29  * Returns:
30  *	RET_SUCCESS, RET_ERROR.
31  */
32 int
33 __rec_ret(t, e, nrec, key, data)
34 	BTREE *t;
35 	EPG *e;
36 	recno_t nrec;
37 	DBT *key, *data;
38 {
39 	register RLEAF *rl;
40 	register void *p;
41 
42 	if (data == NULL)
43 		goto retkey;
44 
45 	rl = GETRLEAF(e->page, e->index);
46 
47 	if (rl->flags & P_BIGDATA) {
48 		if (__ovfl_get(t, rl->bytes,
49 		    &data->size, &t->bt_dbuf, &t->bt_dbufsz))
50 			return (RET_ERROR);
51 	} else {
52 		/* Use +1 in case the first record retrieved is 0 length. */
53 		if (rl->dsize + 1 > t->bt_dbufsz) {
54 			if ((p = realloc(t->bt_dbuf, rl->dsize + 1)) == NULL)
55 				return (RET_ERROR);
56 			t->bt_dbuf = p;
57 			t->bt_dbufsz = rl->dsize + 1;
58 		}
59 		memmove(t->bt_dbuf, rl->bytes, rl->dsize);
60 		data->size = rl->dsize;
61 	}
62 	data->data = t->bt_dbuf;
63 
64 retkey:	if (key == NULL)
65 		return (RET_SUCCESS);
66 
67 	if (sizeof(recno_t) > t->bt_kbufsz) {
68 		if ((p = realloc(t->bt_kbuf, sizeof(recno_t))) == NULL)
69 			return (RET_ERROR);
70 		t->bt_kbuf = p;
71 		t->bt_kbufsz = sizeof(recno_t);
72 	}
73 	memmove(t->bt_kbuf, &nrec, sizeof(recno_t));
74 	key->size = sizeof(recno_t);
75 	key->data = t->bt_kbuf;
76 	return (RET_SUCCESS);
77 }
78