/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1991, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/types.h>3233#include <errno.h>34#include <limits.h>35#include <stdio.h>36#include <string.h>3738#include <db.h>39#include "recno.h"4041/*42* __REC_SEQ -- Recno sequential scan interface.43*44* Parameters:45* dbp: pointer to access method46* key: key for positioning and return value47* data: data return value48* flags: R_CURSOR, R_FIRST, R_LAST, R_NEXT, R_PREV.49*50* Returns:51* RET_ERROR, RET_SUCCESS or RET_SPECIAL if there's no next key.52*/53int54__rec_seq(const DB *dbp, DBT *key, DBT *data, u_int flags)55{56BTREE *t;57EPG *e;58recno_t nrec;59int status;6061t = dbp->internal;6263/* Toss any page pinned across calls. */64if (t->bt_pinned != NULL) {65mpool_put(t->bt_mp, t->bt_pinned, 0);66t->bt_pinned = NULL;67}6869switch(flags) {70case R_CURSOR:71if ((nrec = *(recno_t *)key->data) == 0)72goto einval;73break;74case R_NEXT:75if (F_ISSET(&t->bt_cursor, CURS_INIT)) {76nrec = t->bt_cursor.rcursor + 1;77break;78}79/* FALLTHROUGH */80case R_FIRST:81nrec = 1;82break;83case R_PREV:84if (F_ISSET(&t->bt_cursor, CURS_INIT)) {85if ((nrec = t->bt_cursor.rcursor - 1) == 0)86return (RET_SPECIAL);87break;88}89/* FALLTHROUGH */90case R_LAST:91if (!F_ISSET(t, R_EOF | R_INMEM) &&92t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)93return (RET_ERROR);94nrec = t->bt_nrecs;95break;96default:97einval: errno = EINVAL;98return (RET_ERROR);99}100101if (t->bt_nrecs == 0 || nrec > t->bt_nrecs) {102if (!F_ISSET(t, R_EOF | R_INMEM) &&103(status = t->bt_irec(t, nrec)) != RET_SUCCESS)104return (status);105if (t->bt_nrecs == 0 || nrec > t->bt_nrecs)106return (RET_SPECIAL);107}108109if ((e = __rec_search(t, nrec - 1, SEARCH)) == NULL)110return (RET_ERROR);111112F_SET(&t->bt_cursor, CURS_INIT);113t->bt_cursor.rcursor = nrec;114115status = __rec_ret(t, e, nrec, key, data);116if (F_ISSET(t, B_DB_LOCK))117mpool_put(t->bt_mp, e->page, 0);118else119t->bt_pinned = e->page;120return (status);121}122123124