xref: /csrg-svn/usr.bin/make/lst.lib/lstDupl.c (revision 69094)
140412Sbostic /*
262085Sbostic  * Copyright (c) 1988, 1989, 1990, 1993
362085Sbostic  *	The Regents of the University of California.  All rights reserved.
440412Sbostic  *
540412Sbostic  * This code is derived from software contributed to Berkeley by
640412Sbostic  * Adam de Boor.
740412Sbostic  *
842741Sbostic  * %sccs.include.redist.c%
940412Sbostic  */
1040412Sbostic 
1140412Sbostic #ifndef lint
12*69094Schristos static char sccsid[] = "@(#)lstDupl.c	8.2 (Berkeley) 04/28/95";
1340412Sbostic #endif /* not lint */
1440412Sbostic 
1540411Sbostic /*-
1640411Sbostic  * listDupl.c --
1740411Sbostic  *	Duplicate a list. This includes duplicating the individual
1840411Sbostic  *	elements.
1940411Sbostic  */
2040411Sbostic 
2140411Sbostic #include    "lstInt.h"
2240411Sbostic 
2340411Sbostic /*-
2440411Sbostic  *-----------------------------------------------------------------------
2540411Sbostic  * Lst_Duplicate --
2640411Sbostic  *	Duplicate an entire list. If a function to copy a ClientData is
2740411Sbostic  *	given, the individual client elements will be duplicated as well.
2840411Sbostic  *
2940411Sbostic  * Results:
3040411Sbostic  *	The new Lst structure or NILLST if failure.
3140411Sbostic  *
3240411Sbostic  * Side Effects:
3340411Sbostic  *	A new list is created.
3440411Sbostic  *-----------------------------------------------------------------------
3540411Sbostic  */
3640411Sbostic Lst
Lst_Duplicate(l,copyProc)3740411Sbostic Lst_Duplicate (l, copyProc)
3840411Sbostic     Lst     	  l;	    	 /* the list to duplicate */
39*69094Schristos     /* A function to duplicate each ClientData */
40*69094Schristos     ClientData	  (*copyProc) __P((ClientData));
4140411Sbostic {
4240411Sbostic     register Lst 	nl;
4340411Sbostic     register ListNode  	ln;
4440411Sbostic     register List 	list = (List)l;
4540411Sbostic 
4640411Sbostic     if (!LstValid (l)) {
4740411Sbostic 	return (NILLST);
4840411Sbostic     }
4940411Sbostic 
5040411Sbostic     nl = Lst_Init (list->isCirc);
5140411Sbostic     if (nl == NILLST) {
5240411Sbostic 	return (NILLST);
5340411Sbostic     }
5440411Sbostic 
5540411Sbostic     ln = list->firstPtr;
5640411Sbostic     while (ln != NilListNode) {
5740411Sbostic 	if (copyProc != NOCOPY) {
5840411Sbostic 	    if (Lst_AtEnd (nl, (*copyProc) (ln->datum)) == FAILURE) {
5940411Sbostic 		return (NILLST);
6040411Sbostic 	    }
6140411Sbostic 	} else if (Lst_AtEnd (nl, ln->datum) == FAILURE) {
6240411Sbostic 	    return (NILLST);
6340411Sbostic 	}
6440411Sbostic 
6540411Sbostic 	if (list->isCirc && ln == list->lastPtr) {
6640411Sbostic 	    ln = NilListNode;
6740411Sbostic 	} else {
6840411Sbostic 	    ln = ln->nextPtr;
6940411Sbostic 	}
7040411Sbostic     }
7140411Sbostic 
7240411Sbostic     return (nl);
7340411Sbostic }
74