xref: /csrg-svn/usr.bin/make/lst.lib/lstDupl.c (revision 40411)
1 /*-
2  * listDupl.c --
3  *	Duplicate a list. This includes duplicating the individual
4  *	elements.
5  *
6  * Copyright (c) 1988 by the Regents of the University of California
7  *
8  */
9 #ifndef lint
10 static char *rcsid =
11 "$Id: lstDupl.c,v 1.4 88/11/17 20:52:21 adam Exp $ SPRITE (Berkeley)";
12 #endif lint
13 
14 #include    "lstInt.h"
15 
16 /*-
17  *-----------------------------------------------------------------------
18  * Lst_Duplicate --
19  *	Duplicate an entire list. If a function to copy a ClientData is
20  *	given, the individual client elements will be duplicated as well.
21  *
22  * Results:
23  *	The new Lst structure or NILLST if failure.
24  *
25  * Side Effects:
26  *	A new list is created.
27  *-----------------------------------------------------------------------
28  */
29 Lst
30 Lst_Duplicate (l, copyProc)
31     Lst     	  l;	    	 /* the list to duplicate */
32     ClientData	  (*copyProc)(); /* A function to duplicate each ClientData */
33 {
34     register Lst 	nl;
35     register ListNode  	ln;
36     register List 	list = (List)l;
37 
38     if (!LstValid (l)) {
39 	return (NILLST);
40     }
41 
42     nl = Lst_Init (list->isCirc);
43     if (nl == NILLST) {
44 	return (NILLST);
45     }
46 
47     ln = list->firstPtr;
48     while (ln != NilListNode) {
49 	if (copyProc != NOCOPY) {
50 	    if (Lst_AtEnd (nl, (*copyProc) (ln->datum)) == FAILURE) {
51 		return (NILLST);
52 	    }
53 	} else if (Lst_AtEnd (nl, ln->datum) == FAILURE) {
54 	    return (NILLST);
55 	}
56 
57 	if (list->isCirc && ln == list->lastPtr) {
58 	    ln = NilListNode;
59 	} else {
60 	    ln = ln->nextPtr;
61 	}
62     }
63 
64     return (nl);
65 }
66