xref: /csrg-svn/usr.bin/make/lst.lib/lstNext.c (revision 42741)
140412Sbostic /*
240412Sbostic  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
340412Sbostic  * All rights reserved.
440412Sbostic  *
540412Sbostic  * This code is derived from software contributed to Berkeley by
640412Sbostic  * Adam de Boor.
740412Sbostic  *
8*42741Sbostic  * %sccs.include.redist.c%
940412Sbostic  */
1040412Sbostic 
1140412Sbostic #ifndef lint
12*42741Sbostic static char sccsid[] = "@(#)lstNext.c	5.3 (Berkeley) 06/01/90";
1340412Sbostic #endif /* not lint */
1440412Sbostic 
1540411Sbostic /*-
1640411Sbostic  * LstNext.c --
1740411Sbostic  *	Return the next node for a list.
1840411Sbostic  *	The sequential functions access the list in a slightly different way.
1940411Sbostic  *	CurPtr points to their idea of the current node in the list and they
2040411Sbostic  *	access the list based on it. Because the list is circular, Lst_Next
2140411Sbostic  *	and Lst_Prev will go around the list forever. Lst_IsAtEnd must be
2240411Sbostic  *	used to determine when to stop.
2340411Sbostic  */
2440411Sbostic 
2540411Sbostic #include	"lstInt.h"
2640411Sbostic 
2740411Sbostic /*-
2840411Sbostic  *-----------------------------------------------------------------------
2940411Sbostic  * Lst_Next --
3040411Sbostic  *	Return the next node for the given list.
3140411Sbostic  *
3240411Sbostic  * Results:
3340411Sbostic  *	The next node or NILLNODE if the list has yet to be opened. Also
3440411Sbostic  *	if the list is non-circular and the end has been reached, NILLNODE
3540411Sbostic  *	is returned.
3640411Sbostic  *
3740411Sbostic  * Side Effects:
3840411Sbostic  *	the curPtr field is updated.
3940411Sbostic  *
4040411Sbostic  *-----------------------------------------------------------------------
4140411Sbostic  */
4240411Sbostic LstNode
4340411Sbostic Lst_Next (l)
4440411Sbostic     Lst	    	  l;
4540411Sbostic {
4640411Sbostic     register ListNode	tln;
4740411Sbostic     register List 	list = (List)l;
4840411Sbostic 
4940411Sbostic     if ((LstValid (l) == FALSE) ||
5040411Sbostic 	(list->isOpen == FALSE)) {
5140411Sbostic 	    return (NILLNODE);
5240411Sbostic     }
5340411Sbostic 
5440411Sbostic     list->prevPtr = list->curPtr;
5540411Sbostic 
5640411Sbostic     if (list->curPtr == NilListNode) {
5740411Sbostic 	if (list->atEnd == Unknown) {
5840411Sbostic 	    /*
5940411Sbostic 	     * If we're just starting out, atEnd will be Unknown.
6040411Sbostic 	     * Then we want to start this thing off in the right
6140411Sbostic 	     * direction -- at the start with atEnd being Middle.
6240411Sbostic 	     */
6340411Sbostic 	    list->curPtr = tln = list->firstPtr;
6440411Sbostic 	    list->atEnd = Middle;
6540411Sbostic 	} else {
6640411Sbostic 	    tln = NilListNode;
6740411Sbostic 	    list->atEnd = Tail;
6840411Sbostic 	}
6940411Sbostic     } else {
7040411Sbostic 	tln = list->curPtr->nextPtr;
7140411Sbostic 	list->curPtr = tln;
7240411Sbostic 
7340411Sbostic 	if (tln == list->firstPtr || tln == NilListNode) {
7440411Sbostic 	    /*
7540411Sbostic 	     * If back at the front, then we've hit the end...
7640411Sbostic 	     */
7740411Sbostic 	    list->atEnd = Tail;
7840411Sbostic 	} else {
7940411Sbostic 	    /*
8040411Sbostic 	     * Reset to Middle if gone past first.
8140411Sbostic 	     */
8240411Sbostic 	    list->atEnd = Middle;
8340411Sbostic 	}
8440411Sbostic     }
8540411Sbostic 
8640411Sbostic     return ((LstNode)tln);
8740411Sbostic }
8840411Sbostic 
89