upgraded to the latest NetBSD version
[unix-history] / usr / src / usr.bin / make / lst.lib / lstInsert.c
CommitLineData
bb2109e7 1/*
4f703748
KB
2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
c65fedcf 4 *
bb2109e7
KB
5 * This code is derived from software contributed to Berkeley by
6 * Adam de Boor.
c65fedcf 7 *
f15db449 8 * %sccs.include.redist.c%
c65fedcf 9 */
bb2109e7 10
c65fedcf 11#ifndef lint
bfdbffbb 12static char sccsid[] = "@(#)lstInsert.c 8.2 (Berkeley) %G%";
bb2109e7
KB
13#endif /* not lint */
14
15/*-
16 * LstInsert.c --
17 * Insert a new datum before an old one
18 */
c65fedcf
KB
19
20#include "lstInt.h"
21
22/*-
23 *-----------------------------------------------------------------------
24 * Lst_Insert --
25 * Insert a new node with the given piece of data before the given
26 * node in the given list.
27 *
28 * Results:
29 * SUCCESS or FAILURE.
30 *
31 * Side Effects:
32 * the firstPtr field will be changed if ln is the first node in the
33 * list.
34 *
35 *-----------------------------------------------------------------------
36 */
37ReturnStatus
38Lst_Insert (l, ln, d)
39 Lst l; /* list to manipulate */
40 LstNode ln; /* node before which to insert d */
41 ClientData d; /* datum to be inserted */
42{
43 register ListNode nLNode; /* new lnode for d */
44 register ListNode lNode = (ListNode)ln;
45 register List list = (List)l;
46
47
48 /*
49 * check validity of arguments
50 */
51 if (LstValid (l) && (LstIsEmpty (l) && ln == NILLNODE))
52 goto ok;
53
54 if (!LstValid (l) || LstIsEmpty (l) || !LstNodeValid (ln, l)) {
55 return (FAILURE);
56 }
57
58 ok:
59 PAlloc (nLNode, ListNode);
60
61 nLNode->datum = d;
62 nLNode->useCount = nLNode->flags = 0;
63
64 if (ln == NILLNODE) {
65 if (list->isCirc) {
66 nLNode->prevPtr = nLNode->nextPtr = nLNode;
67 } else {
68 nLNode->prevPtr = nLNode->nextPtr = NilListNode;
69 }
70 list->firstPtr = list->lastPtr = nLNode;
71 } else {
72 nLNode->prevPtr = lNode->prevPtr;
73 nLNode->nextPtr = lNode;
74
75 if (nLNode->prevPtr != NilListNode) {
76 nLNode->prevPtr->nextPtr = nLNode;
77 }
78 lNode->prevPtr = nLNode;
79
80 if (lNode == list->firstPtr) {
81 list->firstPtr = nLNode;
82 }
83 }
84
85 return (SUCCESS);
86}
87