From d2824247a2bd62a81c7efd41b3a136488a8c6551 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sat, 19 Dec 2020 02:24:16 +0100 Subject: rule: Avoid printing trailing spaces Introduce 'sep' variable to track whether something was printed already. While being at it, introduce PRIu64 for 'handle' and 'position' attributes. Signed-off-by: Phil Sutter --- src/rule.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/rule.c b/src/rule.c index 480afc8..e82cf73 100644 --- a/src/rule.c +++ b/src/rule.c @@ -551,44 +551,53 @@ static int nftnl_rule_snprintf_default(char *buf, size_t size, { struct nftnl_expr *expr; int ret, remain = size, offset = 0, i; + const char *sep = ""; if (r->flags & (1 << NFTNL_RULE_FAMILY)) { - ret = snprintf(buf + offset, remain, "%s ", + ret = snprintf(buf + offset, remain, "%s%s", sep, nftnl_family2str(r->family)); SNPRINTF_BUFFER_SIZE(ret, remain, offset); + sep = " "; } if (r->flags & (1 << NFTNL_RULE_TABLE)) { - ret = snprintf(buf + offset, remain, "%s ", + ret = snprintf(buf + offset, remain, "%s%s", sep, r->table); SNPRINTF_BUFFER_SIZE(ret, remain, offset); + sep = " "; } if (r->flags & (1 << NFTNL_RULE_CHAIN)) { - ret = snprintf(buf + offset, remain, "%s ", + ret = snprintf(buf + offset, remain, "%s%s", sep, r->chain); SNPRINTF_BUFFER_SIZE(ret, remain, offset); + sep = " "; } if (r->flags & (1 << NFTNL_RULE_HANDLE)) { - ret = snprintf(buf + offset, remain, "%llu ", - (unsigned long long)r->handle); + ret = snprintf(buf + offset, remain, "%s%" PRIu64, sep, + r->handle); SNPRINTF_BUFFER_SIZE(ret, remain, offset); + sep = " "; } if (r->flags & (1 << NFTNL_RULE_POSITION)) { - ret = snprintf(buf + offset, remain, "%llu ", - (unsigned long long)r->position); + ret = snprintf(buf + offset, remain, "%s%" PRIu64, sep, + r->position); SNPRINTF_BUFFER_SIZE(ret, remain, offset); + sep = " "; } if (r->flags & (1 << NFTNL_RULE_ID)) { - ret = snprintf(buf + offset, remain, "%u ", r->id); + ret = snprintf(buf + offset, remain, "%s%u", sep, r->id); SNPRINTF_BUFFER_SIZE(ret, remain, offset); + sep = " "; } if (r->flags & (1 << NFTNL_RULE_POSITION_ID)) { - ret = snprintf(buf + offset, remain, "%u ", r->position_id); + ret = snprintf(buf + offset, remain, "%s%u", sep, + r->position_id); SNPRINTF_BUFFER_SIZE(ret, remain, offset); + sep = " "; } ret = snprintf(buf + offset, remain, "\n"); -- cgit v1.2.3