lstDupl.c revision 236769
172165Sphantom/*	$NetBSD: lstDupl.c,v 1.16 2009/01/23 21:26:30 dsl Exp $	*/
287658Sphantom
372165Sphantom/*
472165Sphantom * Copyright (c) 1988, 1989, 1990, 1993
5235785Stheraven *	The Regents of the University of California.  All rights reserved.
6235785Stheraven *
7235785Stheraven * This code is derived from software contributed to Berkeley by
8235785Stheraven * Adam de Boor.
9235785Stheraven *
1072165Sphantom * Redistribution and use in source and binary forms, with or without
1172165Sphantom * modification, are permitted provided that the following conditions
1272165Sphantom * are met:
1372165Sphantom * 1. Redistributions of source code must retain the above copyright
1472165Sphantom *    notice, this list of conditions and the following disclaimer.
1572165Sphantom * 2. Redistributions in binary form must reproduce the above copyright
1672165Sphantom *    notice, this list of conditions and the following disclaimer in the
1772165Sphantom *    documentation and/or other materials provided with the distribution.
1872165Sphantom * 3. Neither the name of the University nor the names of its contributors
1972165Sphantom *    may be used to endorse or promote products derived from this software
2072165Sphantom *    without specific prior written permission.
2172165Sphantom *
2272165Sphantom * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2372165Sphantom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2472165Sphantom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2572165Sphantom * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2672165Sphantom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2772165Sphantom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2872165Sphantom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2972165Sphantom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3072165Sphantom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3172165Sphantom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3292986Sobrien * SUCH DAMAGE.
3392986Sobrien */
3492986Sobrien
3572390Srwatson#ifndef MAKE_NATIVE
3672390Srwatsonstatic char rcsid[] = "$NetBSD: lstDupl.c,v 1.16 2009/01/23 21:26:30 dsl Exp $";
37116875Sphantom#else
3872165Sphantom#include <sys/cdefs.h>
3972165Sphantom#ifndef lint
4072360Sphantom#if 0
4172360Sphantomstatic char sccsid[] = "@(#)lstDupl.c	8.1 (Berkeley) 6/6/93";
4272360Sphantom#else
4372165Sphantom__RCSID("$NetBSD: lstDupl.c,v 1.16 2009/01/23 21:26:30 dsl Exp $");
44235785Stheraven#endif
45235785Stheraven#endif /* not lint */
46235785Stheraven#endif
47235785Stheraven
48235785Stheraven/*-
49235785Stheraven * listDupl.c --
50235785Stheraven *	Duplicate a list. This includes duplicating the individual
51235785Stheraven *	elements.
5272976Sphantom */
5372165Sphantom
5472165Sphantom#include    "lstInt.h"
5572165Sphantom
5672165Sphantom/*-
5772165Sphantom *-----------------------------------------------------------------------
5872165Sphantom * Lst_Duplicate --
5972165Sphantom *	Duplicate an entire list. If a function to copy a void *is
6072165Sphantom *	given, the individual client elements will be duplicated as well.
61235785Stheraven *
62235785Stheraven * Input:
63235785Stheraven *	l		the list to duplicate
64235785Stheraven *	copyProc	A function to duplicate each void *
65235785Stheraven *
66235785Stheraven * Results:
67235785Stheraven *	The new Lst structure or NULL if failure.
6872165Sphantom *
69235785Stheraven * Side Effects:
70235785Stheraven *	A new list is created.
71101470Sache *-----------------------------------------------------------------------
72101498Sache */
73235785StheravenLst
7472443SphantomLst_Duplicate(Lst l, DuplicateProc *copyProc)
75235785Stheraven{
76235785Stheraven    Lst 	nl;
77101498Sache    ListNode  	ln;
78235785Stheraven    List 	list = l;
79101498Sache
80235785Stheraven    if (!LstValid (l)) {
81235785Stheraven	return NULL;
82235785Stheraven    }
83235785Stheraven
84101498Sache    nl = Lst_Init(list->isCirc);
85101498Sache    if (nl == NULL) {
8672165Sphantom	return NULL;
87235785Stheraven    }
88235785Stheraven
89235785Stheraven    ln = list->firstPtr;
90235785Stheraven    while (ln != NULL) {
91235785Stheraven	if (copyProc != NULL) {
92235785Stheraven	    if (Lst_AtEnd(nl, copyProc(ln->datum)) == FAILURE) {
93235785Stheraven		return NULL;
94235785Stheraven	    }
95235785Stheraven	} else if (Lst_AtEnd(nl, ln->datum) == FAILURE) {
96235785Stheraven	    return NULL;
97235785Stheraven	}
98235785Stheraven
99235785Stheraven	if (list->isCirc && ln == list->lastPtr) {
100235785Stheraven	    ln = NULL;
101235785Stheraven	} else {
102235785Stheraven	    ln = ln->nextPtr;
103235785Stheraven	}
10472165Sphantom    }
10572165Sphantom
106235785Stheraven    return (nl);
107101470Sache}
108235785Stheraven