xref: /original-bsd/lib/libc/db/recno/rec_utils.c (revision f737e041)
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.3 (Berkeley) 02/21/94";
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 	/*
48 	 * We always copy big data to make it contigous.  Otherwise, we
49 	 * leave the page pinned and don't copy unless the user specified
50 	 * concurrent access.
51 	 */
52 	if (rl->flags & P_BIGDATA) {
53 		if (__ovfl_get(t, rl->bytes,
54 		    &data->size, &t->bt_dbuf, &t->bt_dbufsz))
55 			return (RET_ERROR);
56 		data->data = t->bt_dbuf;
57 	} else if (ISSET(t, B_DB_LOCK)) {
58 		/* Use +1 in case the first record retrieved is 0 length. */
59 		if (rl->dsize + 1 > t->bt_dbufsz) {
60 			if ((p =
61 			    (void *)realloc(t->bt_dbuf, rl->dsize + 1)) == NULL)
62 				return (RET_ERROR);
63 			t->bt_dbuf = p;
64 			t->bt_dbufsz = rl->dsize + 1;
65 		}
66 		memmove(t->bt_dbuf, rl->bytes, rl->dsize);
67 		data->size = rl->dsize;
68 		data->data = t->bt_dbuf;
69 	} else {
70 		data->size = rl->dsize;
71 		data->data = rl->bytes;
72 	}
73 
74 retkey:	if (key == NULL)
75 		return (RET_SUCCESS);
76 
77 	/* We have to copy the key, it's not on the page. */
78 	if (sizeof(recno_t) > t->bt_kbufsz) {
79 		if ((p = (void *)realloc(t->bt_kbuf, sizeof(recno_t))) == NULL)
80 			return (RET_ERROR);
81 		t->bt_kbuf = p;
82 		t->bt_kbufsz = sizeof(recno_t);
83 	}
84 	memmove(t->bt_kbuf, &nrec, sizeof(recno_t));
85 	key->size = sizeof(recno_t);
86 	key->data = t->bt_kbuf;
87 	return (RET_SUCCESS);
88 }
89