xref: /original-bsd/lib/libc/db/recno/rec_utils.c (revision 2cb1372a)
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.2 (Berkeley) 09/07/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 	/*
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 = realloc(t->bt_dbuf, rl->dsize + 1)) == NULL)
61 				return (RET_ERROR);
62 			t->bt_dbuf = p;
63 			t->bt_dbufsz = rl->dsize + 1;
64 		}
65 		memmove(t->bt_dbuf, rl->bytes, rl->dsize);
66 		data->size = rl->dsize;
67 		data->data = t->bt_dbuf;
68 	} else {
69 		data->size = rl->dsize;
70 		data->data = rl->bytes;
71 	}
72 
73 retkey:	if (key == NULL)
74 		return (RET_SUCCESS);
75 
76 	/* We have to copy the key, it's not on the page. */
77 	if (sizeof(recno_t) > t->bt_kbufsz) {
78 		if ((p = realloc(t->bt_kbuf, sizeof(recno_t))) == NULL)
79 			return (RET_ERROR);
80 		t->bt_kbuf = p;
81 		t->bt_kbufsz = sizeof(recno_t);
82 	}
83 	memmove(t->bt_kbuf, &nrec, sizeof(recno_t));
84 	key->size = sizeof(recno_t);
85 	key->data = t->bt_kbuf;
86 	return (RET_SUCCESS);
87 }
88