From abe9ac8a972f610783349062a0e3b91b3891121e Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 14 Apr 2011 03:00:52 +0200 Subject: cleanup entry counting loop --- proto-static.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'proto-static.c') diff --git a/proto-static.c b/proto-static.c index 4e7c052..cdaea76 100644 --- a/proto-static.c +++ b/proto-static.c @@ -123,6 +123,18 @@ parse_v6(const char *str, struct v6_addr *v6, int netmask) return parse_ip_and_netmask(AF_INET6, str, &v6->addr, &v6->prefix); } +static int +count_list_entries(struct uci_option *o) +{ + struct uci_element *e; + int n = 0; + + uci_foreach_element(&o->v.list, e) + n++; + + return n; +} + enum { OPT_IPADDR, OPT_IP6ADDR, @@ -172,10 +184,8 @@ static_attach(struct proto_handler *h, struct interface *iface, if (!parse_v4(tb[OPT_IPADDR]->v.string, v4, netmask)) goto invalid_addr; } else { - uci_foreach_element(&tb[OPT_IPADDR]->v.list, e) - n_v4++; - i = 0; + n_v4 = count_list_entries(tb[OPT_IPADDR]); v4 = alloca(sizeof(*v4) * n_v4); uci_foreach_element(&tb[OPT_IPADDR]->v.list, e) { if (!parse_v4(e->name, &v4[i++], netmask)) @@ -192,10 +202,8 @@ static_attach(struct proto_handler *h, struct interface *iface, if (!parse_v6(tb[OPT_IP6ADDR]->v.string, v6, netmask)) goto invalid_addr; } else { - uci_foreach_element(&tb[OPT_IP6ADDR]->v.list, e) - n_v6++; - i = 0; + n_v6 = count_list_entries(tb[OPT_IP6ADDR]); v6 = alloca(sizeof(*v6) * n_v6); uci_foreach_element(&tb[OPT_IP6ADDR]->v.list, e) { if (!parse_v6(e->name, &v6[i++], netmask)) -- cgit v1.2.3