1 /* $OpenBSD: rec_seq.c,v 1.4 1999/02/15 05:11:25 millert Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)rec_utils.c 8.6 (Berkeley) 7/16/94"; 39 #else 40 static char rcsid[] = "$OpenBSD: rec_seq.c,v 1.4 1999/02/15 05:11:25 millert Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/types.h> 45 46 #include <errno.h> 47 #include <limits.h> 48 #include <stdio.h> 49 #include <string.h> 50 51 #include <db.h> 52 #include "recno.h" 53 54 /* 55 * __REC_SEQ -- Recno sequential scan interface. 56 * 57 * Parameters: 58 * dbp: pointer to access method 59 * key: key for positioning and return value 60 * data: data return value 61 * flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV. 62 * 63 * Returns: 64 * RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key. 65 */ 66 int 67 __rec_seq(dbp, key, data, flags) 68 const DB *dbp; 69 DBT *key, *data; 70 u_int flags; 71 { 72 BTREE *t; 73 EPG *e; 74 recno_t nrec; 75 int status; 76 77 t = dbp->internal; 78 79 /* Toss any page pinned across calls. */ 80 if (t->bt_pinned != NULL) { 81 mpool_put(t->bt_mp, t->bt_pinned, 0); 82 t->bt_pinned = NULL; 83 } 84 85 switch(flags) { 86 case R_CURSOR: 87 if ((nrec = *(recno_t *)key->data) == 0) 88 goto einval; 89 break; 90 case R_NEXT: 91 if (F_ISSET(&t->bt_cursor, CURS_INIT)) { 92 nrec = t->bt_cursor.rcursor + 1; 93 break; 94 } 95 /* FALLTHROUGH */ 96 case R_FIRST: 97 nrec = 1; 98 break; 99 case R_PREV: 100 if (F_ISSET(&t->bt_cursor, CURS_INIT)) { 101 if ((nrec = t->bt_cursor.rcursor - 1) == 0) 102 return (RET_SPECIAL); 103 break; 104 } 105 /* FALLTHROUGH */ 106 case R_LAST: 107 if (!F_ISSET(t, R_EOF | R_INMEM) && 108 t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR) 109 return (RET_ERROR); 110 nrec = t->bt_nrecs; 111 break; 112 default: 113 einval: errno = EINVAL; 114 return (RET_ERROR); 115 } 116 117 if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) { 118 if (!F_ISSET(t, R_EOF | R_INMEM) && 119 (status = t->bt_irec(t, nrec)) != RET_SUCCESS) 120 return (status); 121 if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) 122 return (RET_SPECIAL); 123 } 124 125 if ((e = __rec_search(t, nrec - 1, SEARCH)) == NULL) 126 return (RET_ERROR); 127 128 F_SET(&t->bt_cursor, CURS_INIT); 129 t->bt_cursor.rcursor = nrec; 130 131 status = __rec_ret(t, e, nrec, key, data); 132 if (F_ISSET(t, B_DB_LOCK)) 133 mpool_put(t->bt_mp, e->page, 0); 134 else 135 t->bt_pinned = e->page; 136 return (status); 137 } 138