xref: /csrg-svn/lib/libc/db/recno/rec_seq.c (revision 56760)
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*56760Sbostic static char sccsid[] = "@(#)rec_seq.c	5.5 (Berkeley) 11/13/92";
1050995Sbostic #endif /* not lint */
1150995Sbostic 
1250995Sbostic #include <sys/types.h>
1355316Sbostic 
14*56760Sbostic #include <db.h>
1550995Sbostic #include <errno.h>
1650995Sbostic #include <limits.h>
1750995Sbostic #include <stdio.h>
1855316Sbostic #include <strings.h>
19*56760Sbostic 
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:
48*56760Sbostic 		if ((nrec = *(recno_t *)key->data) == 0)
49*56760Sbostic 			goto einval;
5050995Sbostic 		break;
5150995Sbostic 	case R_NEXT:
5250995Sbostic 		if (ISSET(t, BTF_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:
6150995Sbostic 		if (ISSET(t, BTF_SEQINIT)) {
62*56760Sbostic 			if ((nrec = t->bt_rcursor - 1) == 0)
63*56760Sbostic 				return (RET_SPECIAL);
6450995Sbostic 			break;
6550995Sbostic 		}
6650995Sbostic 		/* FALLTHROUGH */
6750995Sbostic 	case R_LAST:
68*56760Sbostic 		if (!ISSET(t, BTF_RINMEM) &&
69*56760Sbostic 		    t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
7050995Sbostic 			return (RET_ERROR);
7150995Sbostic 		nrec = t->bt_nrecs;
7250995Sbostic 		break;
7350995Sbostic 	default:
74*56760Sbostic einval:		errno = EINVAL;
7550995Sbostic 		return (RET_ERROR);
7650995Sbostic 	}
7750995Sbostic 
7854283Sbostic 	if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) {
79*56760Sbostic 		if (!ISSET(t, BTF_RINMEM) &&
80*56760Sbostic 		    (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*56760Sbostic 	SET(t, BTF_SEQINIT);
90*56760Sbostic 	t->bt_rcursor = nrec;
91*56760Sbostic 
92*56760Sbostic 	status = __rec_ret(t, e, nrec, key, data);
93*56760Sbostic 
9450995Sbostic 	mpool_put(t->bt_mp, e->page, 0);
9550995Sbostic 	return (status);
9650995Sbostic }
97