xref: /csrg-svn/lib/libc/db/recno/rec_get.c (revision 60051)
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*60051Sbostic static char sccsid[] = "@(#)rec_get.c	5.10 (Berkeley) 05/16/93";
1050995Sbostic #endif /* LIBC_SCCS and not lint */
1150995Sbostic 
1250995Sbostic #include <sys/types.h>
1356753Sbostic 
1450995Sbostic #include <errno.h>
1550995Sbostic #include <stddef.h>
1650995Sbostic #include <stdio.h>
1750995Sbostic #include <stdlib.h>
1850995Sbostic #include <string.h>
1956753Sbostic #include <unistd.h>
2056753Sbostic 
2157933Sbostic #include <db.h>
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;
5858747Sbostic 	if (nrec > t->bt_nrecs) {
59*60051Sbostic 		if (ISSET(t, R_EOF | R_INMEM))
6058747Sbostic 			return (RET_SPECIAL);
6158747Sbostic 		if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
6250995Sbostic 			return (status);
6358747Sbostic 	}
6450995Sbostic 
6550995Sbostic 	--nrec;
6651086Sbostic 	if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
6750995Sbostic 		return (RET_ERROR);
6850995Sbostic 
6956753Sbostic 	status = __rec_ret(t, e, 0, NULL, data);
7050995Sbostic 	mpool_put(t->bt_mp, e->page, 0);
7150995Sbostic 	return (status);
7250995Sbostic }
7350995Sbostic 
7450995Sbostic /*
7550995Sbostic  * __REC_FPIPE -- Get fixed length records from a pipe.
7650995Sbostic  *
7750995Sbostic  * Parameters:
7850995Sbostic  *	t:	tree
7950995Sbostic  *	cnt:	records to read
8050995Sbostic  *
8150995Sbostic  * Returns:
8250995Sbostic  *	RET_ERROR, RET_SUCCESS
8350995Sbostic  */
8450995Sbostic int
8550995Sbostic __rec_fpipe(t, top)
8650995Sbostic 	BTREE *t;
8750995Sbostic 	recno_t top;
8850995Sbostic {
8950995Sbostic 	DBT data;
9050995Sbostic 	recno_t nrec;
9150995Sbostic 	size_t len;
9250995Sbostic 	int ch;
9350995Sbostic 	char *p;
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) {
10456753Sbostic 		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) {
116*60051Sbostic 		SET(t, R_EOF);
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;
13957988Sbostic 	indx_t len;
14050995Sbostic 	size_t sz;
14150995Sbostic 	int bval, ch;
14250995Sbostic 	char *p;
14350995Sbostic 
14450995Sbostic 	bval = t->bt_bval;
14550995Sbostic 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
14650995Sbostic 		for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) {
14750995Sbostic 			if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
14850995Sbostic 				data.data = t->bt_dbuf;
14950995Sbostic 				data.size = p - t->bt_dbuf;
15059793Sbostic 				if (ch == EOF && data.size == 0)
15159793Sbostic 					break;
15250995Sbostic 				if (__rec_iput(t, nrec, &data, 0)
15350995Sbostic 				    != RET_SUCCESS)
15450995Sbostic 					return (RET_ERROR);
15550995Sbostic 				break;
15650995Sbostic 			}
15750995Sbostic 			if (sz == 0) {
15850995Sbostic 				len = p - t->bt_dbuf;
15958098Sbostic 				t->bt_dbufsz += (sz = 256);
16050995Sbostic 				if ((t->bt_dbuf =
16158098Sbostic 				    realloc(t->bt_dbuf, t->bt_dbufsz)) == NULL)
16250995Sbostic 					return (RET_ERROR);
16350995Sbostic 				p = t->bt_dbuf + len;
16450995Sbostic 			}
16550995Sbostic 		}
16650995Sbostic 		if (ch == EOF)
16750995Sbostic 			break;
16850995Sbostic 	}
16950995Sbostic 	if (nrec < top) {
170*60051Sbostic 		SET(t, R_EOF);
17150995Sbostic 		return (RET_SPECIAL);
17250995Sbostic 	}
17350995Sbostic 	return (RET_SUCCESS);
17450995Sbostic }
17550995Sbostic 
17650995Sbostic /*
17750995Sbostic  * __REC_FMAP -- Get fixed length records from a file.
17850995Sbostic  *
17950995Sbostic  * Parameters:
18050995Sbostic  *	t:	tree
18150995Sbostic  *	cnt:	records to read
18250995Sbostic  *
18350995Sbostic  * Returns:
18450995Sbostic  *	RET_ERROR, RET_SUCCESS
18550995Sbostic  */
18650995Sbostic int
18750995Sbostic __rec_fmap(t, top)
18850995Sbostic 	BTREE *t;
18950995Sbostic 	recno_t top;
19050995Sbostic {
19150995Sbostic 	DBT data;
19250995Sbostic 	recno_t nrec;
19350995Sbostic 	caddr_t sp, ep;
19450995Sbostic 	size_t len;
19550995Sbostic 	char *p;
19650995Sbostic 
19758747Sbostic 	sp = t->bt_cmap;
19850995Sbostic 	ep = t->bt_emap;
19950995Sbostic 	data.data = t->bt_dbuf;
20050995Sbostic 	data.size = t->bt_reclen;
20150995Sbostic 
20250995Sbostic 	if (t->bt_dbufsz < t->bt_reclen) {
20350995Sbostic 		if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
20450995Sbostic 			return (RET_ERROR);
20550995Sbostic 		t->bt_dbufsz = t->bt_reclen;
20650995Sbostic 	}
20750995Sbostic 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
20850995Sbostic 		if (sp >= ep) {
209*60051Sbostic 			SET(t, R_EOF);
21050995Sbostic 			return (RET_SPECIAL);
21150995Sbostic 		}
21250995Sbostic 		len = t->bt_reclen;
21350995Sbostic 		for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++);
21450995Sbostic 		memset(p, t->bt_bval, len);
21550995Sbostic 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
21650995Sbostic 			return (RET_ERROR);
21750995Sbostic 	}
21858747Sbostic 	t->bt_cmap = sp;
21950995Sbostic 	return (RET_SUCCESS);
22050995Sbostic }
22150995Sbostic 
22250995Sbostic /*
22350995Sbostic  * __REC_VMAP -- Get variable length records from a file.
22450995Sbostic  *
22550995Sbostic  * Parameters:
22650995Sbostic  *	t:	tree
22750995Sbostic  *	cnt:	records to read
22850995Sbostic  *
22950995Sbostic  * Returns:
23050995Sbostic  *	RET_ERROR, RET_SUCCESS
23150995Sbostic  */
23250995Sbostic int
23350995Sbostic __rec_vmap(t, top)
23450995Sbostic 	BTREE *t;
23550995Sbostic 	recno_t top;
23650995Sbostic {
23750995Sbostic 	DBT data;
23851086Sbostic 	caddr_t sp, ep;
23950995Sbostic 	recno_t nrec;
24050995Sbostic 	int bval;
24150995Sbostic 
24258747Sbostic 	sp = t->bt_cmap;
24350995Sbostic 	ep = t->bt_emap;
24450995Sbostic 	bval = t->bt_bval;
24550995Sbostic 
24650995Sbostic 	for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
24750995Sbostic 		if (sp >= ep) {
248*60051Sbostic 			SET(t, R_EOF);
24950995Sbostic 			return (RET_SPECIAL);
25050995Sbostic 		}
25150995Sbostic 		for (data.data = sp; sp < ep && *sp != bval; ++sp);
25250995Sbostic 		data.size = sp - (caddr_t)data.data;
25350995Sbostic 		if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
25450995Sbostic 			return (RET_ERROR);
25550995Sbostic 		++sp;
25650995Sbostic 	}
25758747Sbostic 	t->bt_cmap = sp;
25850995Sbostic 	return (RET_SUCCESS);
25950995Sbostic }
260