xref: /original-bsd/usr.bin/make/lst.lib/lstDupl.c (revision 2bb4d294)
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Adam de Boor.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)lstDupl.c	8.2 (Berkeley) 04/28/95";
13 #endif /* not lint */
14 
15 /*-
16  * listDupl.c --
17  *	Duplicate a list. This includes duplicating the individual
18  *	elements.
19  */
20 
21 #include    "lstInt.h"
22 
23 /*-
24  *-----------------------------------------------------------------------
25  * Lst_Duplicate --
26  *	Duplicate an entire list. If a function to copy a ClientData is
27  *	given, the individual client elements will be duplicated as well.
28  *
29  * Results:
30  *	The new Lst structure or NILLST if failure.
31  *
32  * Side Effects:
33  *	A new list is created.
34  *-----------------------------------------------------------------------
35  */
36 Lst
Lst_Duplicate(l,copyProc)37 Lst_Duplicate (l, copyProc)
38     Lst     	  l;	    	 /* the list to duplicate */
39     /* A function to duplicate each ClientData */
40     ClientData	  (*copyProc) __P((ClientData));
41 {
42     register Lst 	nl;
43     register ListNode  	ln;
44     register List 	list = (List)l;
45 
46     if (!LstValid (l)) {
47 	return (NILLST);
48     }
49 
50     nl = Lst_Init (list->isCirc);
51     if (nl == NILLST) {
52 	return (NILLST);
53     }
54 
55     ln = list->firstPtr;
56     while (ln != NilListNode) {
57 	if (copyProc != NOCOPY) {
58 	    if (Lst_AtEnd (nl, (*copyProc) (ln->datum)) == FAILURE) {
59 		return (NILLST);
60 	    }
61 	} else if (Lst_AtEnd (nl, ln->datum) == FAILURE) {
62 	    return (NILLST);
63 	}
64 
65 	if (list->isCirc && ln == list->lastPtr) {
66 	    ln = NilListNode;
67 	} else {
68 	    ln = ln->nextPtr;
69 	}
70     }
71 
72     return (nl);
73 }
74