1 /*
2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Adam de Boor.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21 #ifndef lint
22 static char sccsid[] = "@(#)lstPrev.c 5.2 (Berkeley) 03/11/90";
23 #endif /* not lint */
24
25 /*-
26 * LstPrev.c --
27 * Get the node previous to the current one in the list and make it the
28 * current node.
29 * The sequential functions access the list in a slightly different way.
30 * CurPtr points to their idea of the current node in the list and they
31 * access the list based on it. Because the list is circular, Lst_Next
32 * and Lst_Prev will go around the list forever. Lst_IsAtEnd must be
33 * used to determine when to stop.
34 */
35
36 #include "lstInt.h"
37
38 /*-
39 *-----------------------------------------------------------------------
40 * Lst_Prev --
41 * Return the node previous to the current one for the given list.
42 *
43 * Results:
44 * The previous node or NILLNODE if the list hasn't been opened
45 * yet or the beginning was reached.
46 *
47 * Side Effects:
48 * the curPtr is changed to reflect reality.
49 *
50 *-----------------------------------------------------------------------
51 */
52 LstNode
Lst_Prev(l)53 Lst_Prev (l)
54 Lst l;
55 {
56 register ListNode tln;
57 register List list = (List)l;
58
59 if ((LstValid (l) == FALSE) ||
60 (list->isOpen == FALSE)) {
61 return (NILLNODE);
62 }
63
64 list->prevPtr = list->curPtr;
65
66 if (list->curPtr == NilListNode) {
67 if (list->atEnd == Unknown) {
68 list->curPtr = tln = list->lastPtr;
69 list->atEnd = Middle;
70 } else {
71 tln = NilListNode;
72 list->atEnd = Head;
73 }
74 } else {
75 tln = list->curPtr->prevPtr;
76 list->curPtr = tln;
77 if (tln == list->lastPtr || tln == NilListNode) {
78 list->atEnd = Head;
79 } else {
80 list->atEnd = Middle;
81 }
82 }
83
84 return ((LstNode)tln);
85 }
86
87