Initialize output_fp to 'stdout' upon context creation and check output
stream validity in nft_ctx_set_output(). This allows to drop checks in
nft_{gmp_,}print() and do_command_export(). While doing so for the
latter, simplify it a bit by using nft_print() which takes care of
flushing the output stream.
If applications desire to drop all output, they are supposed to open
/dev/null and assign that.
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
ctx->parser_max_errors = 10;
init_list_head(&ctx->cache.list);
ctx->flags = flags;
+ ctx->output.output_fp = stdout;
if (flags == NFT_CTX_DEFAULT)
nft_ctx_netlink_init(ctx);
{
FILE *old = ctx->output.output_fp;
+ if (!fp || ferror(fp))
+ return NULL;
+
ctx->output.output_fp = fp;
return old;
int ret;
va_list arg;
- if (!octx->output_fp)
- return -1;
-
va_start(arg, fmt);
ret = vfprintf(octx->output_fp, fmt, arg);
va_end(arg);
int ret;
va_list arg;
- if (!octx->output_fp)
- return -1;
-
va_start(arg, fmt);
ret = gmp_vfprintf(octx->output_fp, fmt, arg);
va_end(arg);
int i, val, rc;
nft = nft_ctx_new(NFT_CTX_DEFAULT);
- nft_ctx_set_output(nft, stdout);
while (1) {
val = getopt_long(argc, argv, OPTSTRING, options, NULL);
struct nftnl_ruleset *rs;
FILE *fp = ctx->octx->output_fp;
- if (!fp)
- return 0;
-
do {
rs = netlink_dump_ruleset(ctx, &cmd->handle, &cmd->location);
if (rs == NULL && errno != EINTR)
} while (rs == NULL);
nftnl_ruleset_fprintf(fp, rs, cmd->export->format, 0);
- fprintf(fp, "\n");
- fflush(fp);
+ nft_print(ctx->octx, "\n");
nftnl_ruleset_free(rs);
return 0;