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