xref: /csrg-svn/usr.bin/make/lst.lib/lstPrev.c (revision 40411)
1 /*-
2  * LstPrev.c --
3  *	Get the node previous to the current one in the list and make it the
4  *	current node.
5  *	The sequential functions access the list in a slightly different way.
6  *	CurPtr points to their idea of the current node in the list and they
7  *	access the list based on it. Because the list is circular, Lst_Next
8  *	and Lst_Prev will go around the list forever. Lst_IsAtEnd must be
9  *	used to determine when to stop.
10  *
11  * Copyright (c) 1988 by University of California Regents
12  *
13  * Permission to use, copy, modify, and distribute this
14  * software and its documentation for any purpose and without
15  * fee is hereby granted, provided that the above copyright
16  * notice appears in all copies.  Neither the University of California nor
17  * Adam de Boor makes any representations about the suitability of this
18  * software for any purpose.  It is provided "as is" without
19  * express or implied warranty.
20  */
21 #ifndef lint
22 static char *rcsid =
23 "$Id: lstPrev.c,v 1.8 88/11/17 20:53:54 adam Exp $ SPRITE (Berkeley)";
24 #endif lint
25 
26 #include	"lstInt.h"
27 
28 /*-
29  *-----------------------------------------------------------------------
30  * Lst_Prev --
31  *	Return the node previous to the current one for the given list.
32  *
33  * Results:
34  *	The previous node or NILLNODE if the list hasn't been opened
35  *	yet or the beginning was reached.
36  *
37  * Side Effects:
38  *	the curPtr is changed to reflect reality.
39  *
40  *-----------------------------------------------------------------------
41  */
42 LstNode
43 Lst_Prev (l)
44     Lst	    	  	l;
45 {
46     register ListNode	tln;
47     register List 	list = (List)l;
48 
49     if ((LstValid (l) == FALSE) ||
50 	(list->isOpen == FALSE)) {
51 	    return (NILLNODE);
52     }
53 
54     list->prevPtr = list->curPtr;
55 
56     if (list->curPtr == NilListNode) {
57 	if (list->atEnd == Unknown) {
58 	    list->curPtr = tln = list->lastPtr;
59 	    list->atEnd = Middle;
60 	} else {
61 	    tln = NilListNode;
62 	    list->atEnd = Head;
63 	}
64     } else {
65 	tln = list->curPtr->prevPtr;
66 	list->curPtr = tln;
67 	if (tln == list->lastPtr || tln == NilListNode) {
68 	    list->atEnd = Head;
69 	} else {
70 	    list->atEnd = Middle;
71 	}
72     }
73 
74     return ((LstNode)tln);
75 }
76 
77