xref: /csrg-svn/usr.bin/make/lst.lib/lstDupl.c (revision 40412)
1*40412Sbostic /*
2*40412Sbostic  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3*40412Sbostic  * All rights reserved.
4*40412Sbostic  *
5*40412Sbostic  * This code is derived from software contributed to Berkeley by
6*40412Sbostic  * Adam de Boor.
7*40412Sbostic  *
8*40412Sbostic  * Redistribution and use in source and binary forms are permitted
9*40412Sbostic  * provided that the above copyright notice and this paragraph are
10*40412Sbostic  * duplicated in all such forms and that any documentation,
11*40412Sbostic  * advertising materials, and other materials related to such
12*40412Sbostic  * distribution and use acknowledge that the software was developed
13*40412Sbostic  * by the University of California, Berkeley.  The name of the
14*40412Sbostic  * University may not be used to endorse or promote products derived
15*40412Sbostic  * from this software without specific prior written permission.
16*40412Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17*40412Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18*40412Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19*40412Sbostic  */
20*40412Sbostic 
21*40412Sbostic #ifndef lint
22*40412Sbostic static char sccsid[] = "@(#)lstDupl.c	5.2 (Berkeley) 03/11/90";
23*40412Sbostic #endif /* not lint */
24*40412Sbostic 
2540411Sbostic /*-
2640411Sbostic  * listDupl.c --
2740411Sbostic  *	Duplicate a list. This includes duplicating the individual
2840411Sbostic  *	elements.
2940411Sbostic  */
3040411Sbostic 
3140411Sbostic #include    "lstInt.h"
3240411Sbostic 
3340411Sbostic /*-
3440411Sbostic  *-----------------------------------------------------------------------
3540411Sbostic  * Lst_Duplicate --
3640411Sbostic  *	Duplicate an entire list. If a function to copy a ClientData is
3740411Sbostic  *	given, the individual client elements will be duplicated as well.
3840411Sbostic  *
3940411Sbostic  * Results:
4040411Sbostic  *	The new Lst structure or NILLST if failure.
4140411Sbostic  *
4240411Sbostic  * Side Effects:
4340411Sbostic  *	A new list is created.
4440411Sbostic  *-----------------------------------------------------------------------
4540411Sbostic  */
4640411Sbostic Lst
4740411Sbostic Lst_Duplicate (l, copyProc)
4840411Sbostic     Lst     	  l;	    	 /* the list to duplicate */
4940411Sbostic     ClientData	  (*copyProc)(); /* A function to duplicate each ClientData */
5040411Sbostic {
5140411Sbostic     register Lst 	nl;
5240411Sbostic     register ListNode  	ln;
5340411Sbostic     register List 	list = (List)l;
5440411Sbostic 
5540411Sbostic     if (!LstValid (l)) {
5640411Sbostic 	return (NILLST);
5740411Sbostic     }
5840411Sbostic 
5940411Sbostic     nl = Lst_Init (list->isCirc);
6040411Sbostic     if (nl == NILLST) {
6140411Sbostic 	return (NILLST);
6240411Sbostic     }
6340411Sbostic 
6440411Sbostic     ln = list->firstPtr;
6540411Sbostic     while (ln != NilListNode) {
6640411Sbostic 	if (copyProc != NOCOPY) {
6740411Sbostic 	    if (Lst_AtEnd (nl, (*copyProc) (ln->datum)) == FAILURE) {
6840411Sbostic 		return (NILLST);
6940411Sbostic 	    }
7040411Sbostic 	} else if (Lst_AtEnd (nl, ln->datum) == FAILURE) {
7140411Sbostic 	    return (NILLST);
7240411Sbostic 	}
7340411Sbostic 
7440411Sbostic 	if (list->isCirc && ln == list->lastPtr) {
7540411Sbostic 	    ln = NilListNode;
7640411Sbostic 	} else {
7740411Sbostic 	    ln = ln->nextPtr;
7840411Sbostic 	}
7940411Sbostic     }
8040411Sbostic 
8140411Sbostic     return (nl);
8240411Sbostic }
83