1236769Sobrien/*	$NetBSD: lstNext.c,v 1.12 2008/12/13 15:19:29 dsl Exp $	*/
2236769Sobrien
3236769Sobrien/*
4236769Sobrien * Copyright (c) 1988, 1989, 1990, 1993
5236769Sobrien *	The Regents of the University of California.  All rights reserved.
6236769Sobrien *
7236769Sobrien * This code is derived from software contributed to Berkeley by
8236769Sobrien * Adam de Boor.
9236769Sobrien *
10236769Sobrien * Redistribution and use in source and binary forms, with or without
11236769Sobrien * modification, are permitted provided that the following conditions
12236769Sobrien * are met:
13236769Sobrien * 1. Redistributions of source code must retain the above copyright
14236769Sobrien *    notice, this list of conditions and the following disclaimer.
15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
16236769Sobrien *    notice, this list of conditions and the following disclaimer in the
17236769Sobrien *    documentation and/or other materials provided with the distribution.
18236769Sobrien * 3. Neither the name of the University nor the names of its contributors
19236769Sobrien *    may be used to endorse or promote products derived from this software
20236769Sobrien *    without specific prior written permission.
21236769Sobrien *
22236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32236769Sobrien * SUCH DAMAGE.
33236769Sobrien */
34236769Sobrien
35236769Sobrien#ifndef MAKE_NATIVE
36236769Sobrienstatic char rcsid[] = "$NetBSD: lstNext.c,v 1.12 2008/12/13 15:19:29 dsl Exp $";
37236769Sobrien#else
38236769Sobrien#include <sys/cdefs.h>
39236769Sobrien#ifndef lint
40236769Sobrien#if 0
41236769Sobrienstatic char sccsid[] = "@(#)lstNext.c	8.1 (Berkeley) 6/6/93";
42236769Sobrien#else
43236769Sobrien__RCSID("$NetBSD: lstNext.c,v 1.12 2008/12/13 15:19:29 dsl Exp $");
44236769Sobrien#endif
45236769Sobrien#endif /* not lint */
46236769Sobrien#endif
47236769Sobrien
48236769Sobrien/*-
49236769Sobrien * LstNext.c --
50236769Sobrien *	Return the next node for a list.
51236769Sobrien *	The sequential functions access the list in a slightly different way.
52236769Sobrien *	CurPtr points to their idea of the current node in the list and they
53236769Sobrien *	access the list based on it. Because the list is circular, Lst_Next
54236769Sobrien *	and Lst_Prev will go around the list forever. Lst_IsAtEnd must be
55236769Sobrien *	used to determine when to stop.
56236769Sobrien */
57236769Sobrien
58236769Sobrien#include	"lstInt.h"
59236769Sobrien
60236769Sobrien/*-
61236769Sobrien *-----------------------------------------------------------------------
62236769Sobrien * Lst_Next --
63236769Sobrien *	Return the next node for the given list.
64236769Sobrien *
65236769Sobrien * Results:
66236769Sobrien *	The next node or NULL if the list has yet to be opened. Also
67236769Sobrien *	if the list is non-circular and the end has been reached, NULL
68236769Sobrien *	is returned.
69236769Sobrien *
70236769Sobrien * Side Effects:
71236769Sobrien *	the curPtr field is updated.
72236769Sobrien *
73236769Sobrien *-----------------------------------------------------------------------
74236769Sobrien */
75236769SobrienLstNode
76236769SobrienLst_Next(Lst l)
77236769Sobrien{
78236769Sobrien    ListNode	tln;
79236769Sobrien    List 	list = l;
80236769Sobrien
81236769Sobrien    if ((LstValid (l) == FALSE) ||
82236769Sobrien	(list->isOpen == FALSE)) {
83236769Sobrien	    return NULL;
84236769Sobrien    }
85236769Sobrien
86236769Sobrien    list->prevPtr = list->curPtr;
87236769Sobrien
88236769Sobrien    if (list->curPtr == NULL) {
89236769Sobrien	if (list->atEnd == Unknown) {
90236769Sobrien	    /*
91236769Sobrien	     * If we're just starting out, atEnd will be Unknown.
92236769Sobrien	     * Then we want to start this thing off in the right
93236769Sobrien	     * direction -- at the start with atEnd being Middle.
94236769Sobrien	     */
95236769Sobrien	    list->curPtr = tln = list->firstPtr;
96236769Sobrien	    list->atEnd = Middle;
97236769Sobrien	} else {
98236769Sobrien	    tln = NULL;
99236769Sobrien	    list->atEnd = Tail;
100236769Sobrien	}
101236769Sobrien    } else {
102236769Sobrien	tln = list->curPtr->nextPtr;
103236769Sobrien	list->curPtr = tln;
104236769Sobrien
105236769Sobrien	if (tln == list->firstPtr || tln == NULL) {
106236769Sobrien	    /*
107236769Sobrien	     * If back at the front, then we've hit the end...
108236769Sobrien	     */
109236769Sobrien	    list->atEnd = Tail;
110236769Sobrien	} else {
111236769Sobrien	    /*
112236769Sobrien	     * Reset to Middle if gone past first.
113236769Sobrien	     */
114236769Sobrien	    list->atEnd = Middle;
115236769Sobrien	}
116236769Sobrien    }
117236769Sobrien
118236769Sobrien    return (tln);
119236769Sobrien}
120236769Sobrien
121