xref: /csrg-svn/lib/libc/db/recno/rec_seq.c (revision 60054)
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*60054Sbostic static char sccsid[] = "@(#)rec_seq.c	5.9 (Berkeley) 05/16/93";
1050995Sbostic #endif /* not lint */
1150995Sbostic 
1250995Sbostic #include <sys/types.h>
1355316Sbostic 
1450995Sbostic #include <errno.h>
1550995Sbostic #include <limits.h>
1650995Sbostic #include <stdio.h>
1757935Sbostic #include <string.h>
1856760Sbostic 
1957933Sbostic #include <db.h>
2051091Sbostic #include "recno.h"
2150995Sbostic 
2250995Sbostic /*
2350995Sbostic  * __REC_SEQ -- Recno sequential scan interface.
2450995Sbostic  *
2550995Sbostic  * Parameters:
2650995Sbostic  *	dbp:	pointer to access method
2750995Sbostic  *	key:	key for positioning and return value
2850995Sbostic  *	data:	data return value
2950995Sbostic  *	flags:	R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.
3050995Sbostic  *
3150995Sbostic  * Returns:
3250995Sbostic  *	RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.
3350995Sbostic  */
3450995Sbostic int
3550995Sbostic __rec_seq(dbp, key, data, flags)
3650995Sbostic 	const DB *dbp;
3750995Sbostic 	DBT *key, *data;
3850995Sbostic 	u_int flags;
3950995Sbostic {
4050995Sbostic 	BTREE *t;
4150995Sbostic 	EPG *e;
4250995Sbostic 	recno_t nrec;
4351091Sbostic 	int status;
4450995Sbostic 
4550995Sbostic 	t = dbp->internal;
4650995Sbostic 	switch(flags) {
4750995Sbostic 	case R_CURSOR:
4856760Sbostic 		if ((nrec = *(recno_t *)key->data) == 0)
4956760Sbostic 			goto einval;
5050995Sbostic 		break;
5150995Sbostic 	case R_NEXT:
52*60054Sbostic 		if (ISSET(t, B_SEQINIT)) {
5350995Sbostic 			nrec = t->bt_rcursor + 1;
5450995Sbostic 			break;
5550995Sbostic 		}
5650995Sbostic 		/* FALLTHROUGH */
5750995Sbostic 	case R_FIRST:
5850995Sbostic 		nrec = 1;
5950995Sbostic 		break;
6050995Sbostic 	case R_PREV:
61*60054Sbostic 		if (ISSET(t, B_SEQINIT)) {
6256760Sbostic 			if ((nrec = t->bt_rcursor - 1) == 0)
6356760Sbostic 				return (RET_SPECIAL);
6450995Sbostic 			break;
6550995Sbostic 		}
6650995Sbostic 		/* FALLTHROUGH */
6750995Sbostic 	case R_LAST:
68*60054Sbostic 		if (!ISSET(t, R_EOF | R_INMEM) &&
6956760Sbostic 		    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
7050995Sbostic 			return (RET_ERROR);
7150995Sbostic 		nrec = t->bt_nrecs;
7250995Sbostic 		break;
7350995Sbostic 	default:
7456760Sbostic einval:		errno = EINVAL;
7550995Sbostic 		return (RET_ERROR);
7650995Sbostic 	}
7750995Sbostic 
7854283Sbostic 	if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) {
79*60054Sbostic 		if (!ISSET(t, R_EOF | R_INMEM) &&
8056760Sbostic 		    (status = t->bt_irec(t, nrec)) != RET_SUCCESS)
8151091Sbostic 			return (status);
8254283Sbostic 		if (t->bt_nrecs == 0 || nrec > t->bt_nrecs)
8351091Sbostic 			return (RET_SPECIAL);
8451091Sbostic 	}
8550995Sbostic 
8651091Sbostic 	if ((e = __rec_search(t, nrec - 1, SEARCH)) == NULL)
8750995Sbostic 		return (RET_ERROR);
8850995Sbostic 
89*60054Sbostic 	SET(t, B_SEQINIT);
9056760Sbostic 	t->bt_rcursor = nrec;
9156760Sbostic 
9256760Sbostic 	status = __rec_ret(t, e, nrec, key, data);
9356760Sbostic 
9450995Sbostic 	mpool_put(t->bt_mp, e->page, 0);
9550995Sbostic 	return (status);
9650995Sbostic }
97