xref: /csrg-svn/lib/libc/db/recno/rec_seq.c (revision 51091)
150995Sbostic /*-
250995Sbostic  * Copyright (c) 1991 The Regents of the University of California.
350995Sbostic  * All rights reserved.
450995Sbostic  *
550995Sbostic  * %sccs.include.redist.c%
650995Sbostic  */
750995Sbostic 
850995Sbostic #ifndef lint
9*51091Sbostic static char sccsid[] = "@(#)rec_seq.c	5.2 (Berkeley) 09/11/91";
1050995Sbostic #endif /* not lint */
1150995Sbostic 
1250995Sbostic #include <sys/types.h>
1350995Sbostic #include <errno.h>
1450995Sbostic #include <db.h>
1550995Sbostic #include <limits.h>
1650995Sbostic #include <stdio.h>
17*51091Sbostic #include "recno.h"
1850995Sbostic 
1950995Sbostic /*
2050995Sbostic  * __REC_SEQ -- Recno sequential scan interface.
2150995Sbostic  *
2250995Sbostic  * Parameters:
2350995Sbostic  *	dbp:	pointer to access method
2450995Sbostic  *	key:	key for positioning and return value
2550995Sbostic  *	data:	data return value
2650995Sbostic  *	flags:	R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
2750995Sbostic  *
2850995Sbostic  * Returns:
2950995Sbostic  *	RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
3050995Sbostic  */
3150995Sbostic int
3250995Sbostic __rec_seq(dbp, key, data, flags)
3350995Sbostic 	const DB *dbp;
3450995Sbostic 	DBT *key, *data;
3550995Sbostic 	u_int flags;
3650995Sbostic {
3750995Sbostic 	BTREE *t;
3850995Sbostic 	EPG *e;
3950995Sbostic 	recno_t nrec;
40*51091Sbostic 	int status;
4150995Sbostic 
4250995Sbostic 	t = dbp->internal;
4350995Sbostic 	switch(flags) {
4450995Sbostic 	case R_CURSOR:
4550995Sbostic 		if ((nrec = *(recno_t *)key->data) == 0) {
4650995Sbostic 			errno = EINVAL;
4750995Sbostic 			return (RET_ERROR);
4850995Sbostic 		}
4950995Sbostic 		break;
5050995Sbostic 	case R_NEXT:
5150995Sbostic 		if (ISSET(t, BTF_SEQINIT)) {
5250995Sbostic 			nrec = t->bt_rcursor + 1;
5350995Sbostic 			break;
5450995Sbostic 		}
5550995Sbostic 		/* FALLTHROUGH */
5650995Sbostic 	case R_FIRST:
5750995Sbostic 		nrec = 1;
5850995Sbostic 		break;
5950995Sbostic 	case R_PREV:
6050995Sbostic 		if (ISSET(t, BTF_SEQINIT)) {
61*51091Sbostic 			if ((nrec = t->bt_rcursor - 1) == 0) {
62*51091Sbostic 				errno = EINVAL;
63*51091Sbostic 				return (RET_ERROR);
64*51091Sbostic 			}
6550995Sbostic 			break;
6650995Sbostic 		}
6750995Sbostic 		/* FALLTHROUGH */
6850995Sbostic 	case R_LAST:
6950995Sbostic 		if (t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
7050995Sbostic 			return (RET_ERROR);
7150995Sbostic 		nrec = t->bt_nrecs;
7250995Sbostic 		break;
7350995Sbostic 	default:
7450995Sbostic 		errno = EINVAL;
7550995Sbostic 		return (RET_ERROR);
7650995Sbostic 	}
7750995Sbostic 
78*51091Sbostic 	if (nrec > t->bt_nrecs) {
79*51091Sbostic 		if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
80*51091Sbostic 			return (status);
81*51091Sbostic 		if (nrec > t->bt_nrecs)
82*51091Sbostic 			return (RET_SPECIAL);
83*51091Sbostic 	}
8450995Sbostic 
85*51091Sbostic 	if ((e = __rec_search(t, nrec - 1, SEARCH)) == NULL)
8650995Sbostic 		return (RET_ERROR);
8750995Sbostic 
88*51091Sbostic 	if ((status = __rec_ret(t, e, data)) == RET_SUCCESS) {
89*51091Sbostic 		t->bt_rcursor = nrec;
90*51091Sbostic 		SET(t, BTF_SEQINIT);
91*51091Sbostic 		UNSET(t, BTF_DELCRSR);
9250995Sbostic 	}
9350995Sbostic 	mpool_put(t->bt_mp, e->page, 0);
9450995Sbostic 	return (status);
9550995Sbostic }
96