xref: /original-bsd/usr.bin/make/lst.lib/lstDupl.c (revision c3e32dec)
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.1 (Berkeley) 06/06/93";
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
37 Lst_Duplicate (l, copyProc)
38     Lst     	  l;	    	 /* the list to duplicate */
39     ClientData	  (*copyProc)(); /* A function to duplicate each ClientData */
40 {
41     register Lst 	nl;
42     register ListNode  	ln;
43     register List 	list = (List)l;
44 
45     if (!LstValid (l)) {
46 	return (NILLST);
47     }
48 
49     nl = Lst_Init (list->isCirc);
50     if (nl == NILLST) {
51 	return (NILLST);
52     }
53 
54     ln = list->firstPtr;
55     while (ln != NilListNode) {
56 	if (copyProc != NOCOPY) {
57 	    if (Lst_AtEnd (nl, (*copyProc) (ln->datum)) == FAILURE) {
58 		return (NILLST);
59 	    }
60 	} else if (Lst_AtEnd (nl, ln->datum) == FAILURE) {
61 	    return (NILLST);
62 	}
63 
64 	if (list->isCirc && ln == list->lastPtr) {
65 	    ln = NilListNode;
66 	} else {
67 	    ln = ln->nextPtr;
68 	}
69     }
70 
71     return (nl);
72 }
73