xref: /csrg-svn/usr.bin/make/lst.lib/lstDupl.c (revision 40412)
1 /*
2  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Adam de Boor.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20 
21 #ifndef lint
22 static char sccsid[] = "@(#)lstDupl.c	5.2 (Berkeley) 03/11/90";
23 #endif /* not lint */
24 
25 /*-
26  * listDupl.c --
27  *	Duplicate a list. This includes duplicating the individual
28  *	elements.
29  */
30 
31 #include    "lstInt.h"
32 
33 /*-
34  *-----------------------------------------------------------------------
35  * Lst_Duplicate --
36  *	Duplicate an entire list. If a function to copy a ClientData is
37  *	given, the individual client elements will be duplicated as well.
38  *
39  * Results:
40  *	The new Lst structure or NILLST if failure.
41  *
42  * Side Effects:
43  *	A new list is created.
44  *-----------------------------------------------------------------------
45  */
46 Lst
47 Lst_Duplicate (l, copyProc)
48     Lst     	  l;	    	 /* the list to duplicate */
49     ClientData	  (*copyProc)(); /* A function to duplicate each ClientData */
50 {
51     register Lst 	nl;
52     register ListNode  	ln;
53     register List 	list = (List)l;
54 
55     if (!LstValid (l)) {
56 	return (NILLST);
57     }
58 
59     nl = Lst_Init (list->isCirc);
60     if (nl == NILLST) {
61 	return (NILLST);
62     }
63 
64     ln = list->firstPtr;
65     while (ln != NilListNode) {
66 	if (copyProc != NOCOPY) {
67 	    if (Lst_AtEnd (nl, (*copyProc) (ln->datum)) == FAILURE) {
68 		return (NILLST);
69 	    }
70 	} else if (Lst_AtEnd (nl, ln->datum) == FAILURE) {
71 	    return (NILLST);
72 	}
73 
74 	if (list->isCirc && ln == list->lastPtr) {
75 	    ln = NilListNode;
76 	} else {
77 	    ln = ln->nextPtr;
78 	}
79     }
80 
81     return (nl);
82 }
83