xref: /csrg-svn/usr.bin/make/lst.lib/lstDupl.c (revision 42741)
140412Sbostic /*
240412Sbostic  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
340412Sbostic  * All rights reserved.
440412Sbostic  *
540412Sbostic  * This code is derived from software contributed to Berkeley by
640412Sbostic  * Adam de Boor.
740412Sbostic  *
8*42741Sbostic  * %sccs.include.redist.c%
940412Sbostic  */
1040412Sbostic 
1140412Sbostic #ifndef lint
12*42741Sbostic static char sccsid[] = "@(#)lstDupl.c	5.3 (Berkeley) 06/01/90";
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
3740411Sbostic Lst_Duplicate (l, copyProc)
3840411Sbostic     Lst     	  l;	    	 /* the list to duplicate */
3940411Sbostic     ClientData	  (*copyProc)(); /* A function to duplicate each ClientData */
4040411Sbostic {
4140411Sbostic     register Lst 	nl;
4240411Sbostic     register ListNode  	ln;
4340411Sbostic     register List 	list = (List)l;
4440411Sbostic 
4540411Sbostic     if (!LstValid (l)) {
4640411Sbostic 	return (NILLST);
4740411Sbostic     }
4840411Sbostic 
4940411Sbostic     nl = Lst_Init (list->isCirc);
5040411Sbostic     if (nl == NILLST) {
5140411Sbostic 	return (NILLST);
5240411Sbostic     }
5340411Sbostic 
5440411Sbostic     ln = list->firstPtr;
5540411Sbostic     while (ln != NilListNode) {
5640411Sbostic 	if (copyProc != NOCOPY) {
5740411Sbostic 	    if (Lst_AtEnd (nl, (*copyProc) (ln->datum)) == FAILURE) {
5840411Sbostic 		return (NILLST);
5940411Sbostic 	    }
6040411Sbostic 	} else if (Lst_AtEnd (nl, ln->datum) == FAILURE) {
6140411Sbostic 	    return (NILLST);
6240411Sbostic 	}
6340411Sbostic 
6440411Sbostic 	if (list->isCirc && ln == list->lastPtr) {
6540411Sbostic 	    ln = NilListNode;
6640411Sbostic 	} else {
6740411Sbostic 	    ln = ln->nextPtr;
6840411Sbostic 	}
6940411Sbostic     }
7040411Sbostic 
7140411Sbostic     return (nl);
7240411Sbostic }
73