From: Nick Porter Date: Wed, 6 Jan 2021 11:31:32 +0000 (+0000) Subject: Add fr_pair_sublist_copy() function X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3c61e726286e5763fbbdcdea4ec1225e8dd315ac;p=thirdparty%2Ffreeradius-server.git Add fr_pair_sublist_copy() function --- diff --git a/src/lib/util/dpair.c b/src/lib/util/dpair.c index 18555af4744..1f31806a122 100644 --- a/src/lib/util/dpair.c +++ b/src/lib/util/dpair.c @@ -1540,6 +1540,44 @@ int fr_pair_list_copy_by_ancestor(TALLOC_CTX *ctx, fr_pair_list_t *to, return cnt; } +/** Duplicate a list of pairs starting at a particular item + * + * Copy all pairs from 'from' regardless of tag, attribute or vendor, starting at 'item'. + * + * @param[in] ctx for new #fr_pair_t (s) to be allocated in. + * @param[in] to where to copy attributes to. + * @param[in] from whence to copy #fr_pair_t (s). + * @param[in] item to start copying at + * @return + * - >0 the number of attributes copied. + * - 0 if no attributes copied. + * - -1 on error. + */ +int fr_pair_sublist_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_pair_t *item) +{ + fr_pair_list_t tmp_list; + fr_pair_t *vp, *new_vp; + int cnt = 0; + + fr_pair_list_init(&tmp_list); + + for (vp = item; + vp; + vp = fr_pair_list_next(from, vp), cnt++) { + VP_VERIFY(vp); + new_vp = fr_pair_copy(ctx, vp); + if (!new_vp) { + fr_pair_list_free(&tmp_list); + return -1; + } + fr_pair_add(&tmp_list, new_vp); + } + + fr_dlist_move(&to->head, &tmp_list.head); + + return cnt; +} + /** Free/zero out value (or children) of a given VP * * @param[in] vp to clear value from. diff --git a/src/lib/util/dpair.h b/src/lib/util/dpair.h index b470c502387..5dea44a3bb8 100644 --- a/src/lib/util/dpair.h +++ b/src/lib/util/dpair.h @@ -282,6 +282,7 @@ int fr_pair_list_copy_by_da(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t *from, fr_dict_attr_t const *da, unsigned int count); int fr_pair_list_copy_by_ancestor(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t *from, fr_dict_attr_t const *parent_da, unsigned int count); +int fr_pair_sublist_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_pair_t *item); void fr_tmp_pair_list_move(fr_pair_list_t *dst, fr_pair_list_t *tmp_list); /** @hidecallergraph */