xref: /csrg-svn/lib/libc/db/recno/rec_get.c (revision 56753)
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*56753Sbostic static char sccsid[] = "@(#)rec_get.c	5.4 (Berkeley) 11/13/92";
1050995Sbostic #endif /* LIBC_SCCS and not lint */
1150995Sbostic 
1250995Sbostic #include <sys/types.h>
13*56753Sbostic 
14*56753Sbostic #include <db.h>
1550995Sbostic #include <errno.h>
1650995Sbostic #include <stddef.h>
1750995Sbostic #include <stdio.h>
1850995Sbostic #include <stdlib.h>
1950995Sbostic #include <string.h>
20*56753Sbostic #include <unistd.h>
21*56753Sbostic 
2251086Sbostic #include "recno.h"
2350995Sbostic 
2450995Sbostic /*
2550995Sbostic  * __REC_GET -- Get a record from the btree.
2650995Sbostic  *
2750995Sbostic  * Parameters:
2850995Sbostic  *	dbp:	pointer to access method
2950995Sbostic  *	key:	key to find
3050995Sbostic  *	data:	data to return
3150995Sbostic  *	flag:	currently unused
3250995Sbostic  *
3350995Sbostic  * Returns:
3450995Sbostic  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
3550995Sbostic  */
3650995Sbostic int
3750995Sbostic __rec_get(dbp, key, data, flags)
3850995Sbostic 	const DB *dbp;
3951086Sbostic 	const DBT *key;
4051086Sbostic 	DBT *data;
4150995Sbostic 	u_int flags;
4250995Sbostic {
4350995Sbostic 	BTREE *t;
4450995Sbostic 	EPG *e;
4550995Sbostic 	recno_t nrec;
4654279Sbostic 	int status;
4750995Sbostic 
4850995Sbostic 	if (flags || (nrec = *(recno_t *)key->data) == 0) {
4950995Sbostic 		errno = EINVAL;
5050995Sbostic 		return (RET_ERROR);
5150995Sbostic 	}
5250995Sbostic 
5350995Sbostic 	/*
5450995Sbostic 	 * If we haven't seen this record yet, try to find it in the
5550995Sbostic 	 * original file.
5650995Sbostic 	 */
5750995Sbostic 	t = dbp->internal;
58*56753Sbostic 	if (nrec > t->bt_nrecs && (ISSET(t, BTF_RINMEM) ||
59*56753Sbostic 	    (status = t->bt_irec(t, nrec)) != RET_SUCCESS))
6050995Sbostic 			return (status);
6150995Sbostic 
6250995Sbostic 	--nrec;
6351086Sbostic 	if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
6450995Sbostic 		return (RET_ERROR);
6550995Sbostic 
66*56753Sbostic 	status = __rec_ret(t, e, 0, NULL, data);
6750995Sbostic 	mpool_put(t->bt_mp, e->page, 0);
6850995Sbostic 	return (status);
6950995Sbostic }
7050995Sbostic 
7150995Sbostic /*
7250995Sbostic  * __REC_FPIPE -- Get fixed length records from a pipe.
7350995Sbostic  *
7450995Sbostic  * Parameters:
7550995Sbostic  *	t:	tree
7650995Sbostic  *	cnt:	records to read
7750995Sbostic  *
7850995Sbostic  * Returns:
7950995Sbostic  *	RET_ERROR, RET_SUCCESS
8050995Sbostic  */
8150995Sbostic int
8250995Sbostic __rec_fpipe(t, top)
8350995Sbostic 	BTREE *t;
8450995Sbostic 	recno_t top;
8550995Sbostic {
8650995Sbostic 	DBT data;
8750995Sbostic 	recno_t nrec;
8850995Sbostic 	size_t len;
8950995Sbostic 	int ch;
9050995Sbostic 	char *p;
9150995Sbostic 
9254279Sbostic 	if (t->bt_reof)
9350995Sbostic 		return (RET_SPECIAL);
9450995Sbostic 
9550995Sbostic 	data.data = t->bt_dbuf;
9650995Sbostic 	data.size = t->bt_reclen;
9750995Sbostic 
9850995Sbostic 	if (t->bt_dbufsz < t->bt_reclen) {
9950995Sbostic 		if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
10050995Sbostic 			return (RET_ERROR);
10150995Sbostic 		t->bt_dbufsz = t->bt_reclen;
10250995Sbostic 	}
10350995Sbostic 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
104*56753Sbostic 		len = t->bt_reclen;
10550995Sbostic 		for (p = t->bt_dbuf;; *p++ = ch)
10650995Sbostic 			if ((ch = getc(t->bt_rfp)) == EOF || !len--) {
10750995Sbostic 				if (__rec_iput(t, nrec, &data, 0)
10850995Sbostic 				    != RET_SUCCESS)
10950995Sbostic 					return (RET_ERROR);
11050995Sbostic 				break;
11150995Sbostic 			}
11250995Sbostic 		if (ch == EOF)
11350995Sbostic 			break;
11450995Sbostic 	}
11550995Sbostic 	if (nrec < top) {
11654279Sbostic 		t->bt_reof = 1;
11750995Sbostic 		return (RET_SPECIAL);
11850995Sbostic 	}
11950995Sbostic 	return (RET_SUCCESS);
12050995Sbostic }
12150995Sbostic 
12250995Sbostic /*
12350995Sbostic  * __REC_VPIPE -- Get variable length records from a pipe.
12450995Sbostic  *
12550995Sbostic  * Parameters:
12650995Sbostic  *	t:	tree
12750995Sbostic  *	cnt:	records to read
12850995Sbostic  *
12950995Sbostic  * Returns:
13050995Sbostic  *	RET_ERROR, RET_SUCCESS
13150995Sbostic  */
13250995Sbostic int
13350995Sbostic __rec_vpipe(t, top)
13450995Sbostic 	BTREE *t;
13550995Sbostic 	recno_t top;
13650995Sbostic {
13750995Sbostic 	DBT data;
13850995Sbostic 	recno_t nrec;
13950995Sbostic 	index_t len;
14050995Sbostic 	size_t sz;
14150995Sbostic 	int bval, ch;
14250995Sbostic 	char *p;
14350995Sbostic 
14454279Sbostic 	if (t->bt_reof)
14550995Sbostic 		return (RET_SPECIAL);
14650995Sbostic 
14750995Sbostic 	bval = t->bt_bval;
14850995Sbostic 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
14950995Sbostic 		for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) {
15050995Sbostic 			if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
15150995Sbostic 				data.data = t->bt_dbuf;
15250995Sbostic 				data.size = p - t->bt_dbuf;
15350995Sbostic 				if (__rec_iput(t, nrec, &data, 0)
15450995Sbostic 				    != RET_SUCCESS)
15550995Sbostic 					return (RET_ERROR);
15650995Sbostic 				break;
15750995Sbostic 			}
15850995Sbostic 			if (sz == 0) {
15950995Sbostic 				len = p - t->bt_dbuf;
16050995Sbostic 				sz = t->bt_dbufsz += 256;
16150995Sbostic 				if ((t->bt_dbuf =
16250995Sbostic 				    realloc(t->bt_dbuf, sz)) == NULL)
16350995Sbostic 					return (RET_ERROR);
16450995Sbostic 				p = t->bt_dbuf + len;
16550995Sbostic 			}
16650995Sbostic 		}
16750995Sbostic 		if (ch == EOF)
16850995Sbostic 			break;
16950995Sbostic 	}
17050995Sbostic 	if (nrec < top) {
17154279Sbostic 		t->bt_reof = 1;
17250995Sbostic 		return (RET_SPECIAL);
17350995Sbostic 	}
17450995Sbostic 	return (RET_SUCCESS);
17550995Sbostic }
17650995Sbostic 
17750995Sbostic /*
17850995Sbostic  * __REC_FMAP -- Get fixed length records from a file.
17950995Sbostic  *
18050995Sbostic  * Parameters:
18150995Sbostic  *	t:	tree
18250995Sbostic  *	cnt:	records to read
18350995Sbostic  *
18450995Sbostic  * Returns:
18550995Sbostic  *	RET_ERROR, RET_SUCCESS
18650995Sbostic  */
18750995Sbostic int
18850995Sbostic __rec_fmap(t, top)
18950995Sbostic 	BTREE *t;
19050995Sbostic 	recno_t top;
19150995Sbostic {
19250995Sbostic 	DBT data;
19350995Sbostic 	recno_t nrec;
19450995Sbostic 	caddr_t sp, ep;
19550995Sbostic 	size_t len;
19650995Sbostic 	char *p;
19750995Sbostic 
19854279Sbostic 	if (t->bt_reof)
19950995Sbostic 		return (RET_SPECIAL);
20050995Sbostic 
20150995Sbostic 	sp = t->bt_smap;
20250995Sbostic 	ep = t->bt_emap;
20350995Sbostic 	data.data = t->bt_dbuf;
20450995Sbostic 	data.size = t->bt_reclen;
20550995Sbostic 
20650995Sbostic 	if (t->bt_dbufsz < t->bt_reclen) {
20750995Sbostic 		if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
20850995Sbostic 			return (RET_ERROR);
20950995Sbostic 		t->bt_dbufsz = t->bt_reclen;
21050995Sbostic 	}
21150995Sbostic 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
21250995Sbostic 		if (sp >= ep) {
21354279Sbostic 			t->bt_reof = 1;
21450995Sbostic 			return (RET_SPECIAL);
21550995Sbostic 		}
21650995Sbostic 		len = t->bt_reclen;
21750995Sbostic 		for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++);
21850995Sbostic 		memset(p, t->bt_bval, len);
21950995Sbostic 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
22050995Sbostic 			return (RET_ERROR);
22150995Sbostic 	}
22250995Sbostic 	t->bt_smap = sp;
22350995Sbostic 	return (RET_SUCCESS);
22450995Sbostic }
22550995Sbostic 
22650995Sbostic /*
22750995Sbostic  * __REC_VMAP -- Get variable length records from a file.
22850995Sbostic  *
22950995Sbostic  * Parameters:
23050995Sbostic  *	t:	tree
23150995Sbostic  *	cnt:	records to read
23250995Sbostic  *
23350995Sbostic  * Returns:
23450995Sbostic  *	RET_ERROR, RET_SUCCESS
23550995Sbostic  */
23650995Sbostic int
23750995Sbostic __rec_vmap(t, top)
23850995Sbostic 	BTREE *t;
23950995Sbostic 	recno_t top;
24050995Sbostic {
24150995Sbostic 	DBT data;
24251086Sbostic 	caddr_t sp, ep;
24350995Sbostic 	recno_t nrec;
24450995Sbostic 	int bval;
24550995Sbostic 
24654279Sbostic 	if (t->bt_reof)
24750995Sbostic 		return (RET_SPECIAL);
24850995Sbostic 
24950995Sbostic 	sp = t->bt_smap;
25050995Sbostic 	ep = t->bt_emap;
25150995Sbostic 	bval = t->bt_bval;
25250995Sbostic 
25350995Sbostic 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
25450995Sbostic 		if (sp >= ep) {
25554279Sbostic 			t->bt_reof = 1;
25650995Sbostic 			return (RET_SPECIAL);
25750995Sbostic 		}
25850995Sbostic 		for (data.data = sp; sp < ep && *sp != bval; ++sp);
25950995Sbostic 		data.size = sp - (caddr_t)data.data;
26050995Sbostic 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
26150995Sbostic 			return (RET_ERROR);
26250995Sbostic 		++sp;
26350995Sbostic 	}
26450995Sbostic 	t->bt_smap = sp;
26550995Sbostic 	return (RET_SUCCESS);
26650995Sbostic }
267