diff options
author | Arturo Borrero <arturo.borrero.glez@gmail.com> | 2013-10-29 23:44:25 +0100 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2013-11-03 22:11:56 +0100 |
commit | bd28075ee0bf121ef4fe50e34146efe14acee07b (patch) | |
tree | deb218fce40f0bdf877b249160145adb0de5b5f8 /src/utils.c | |
parent | dc4c1e1ded43875d082f0ef5ed794b5bf9eb8f58 (diff) |
src: add fprintf API functions
Now it's possible to print directly from libnftables to a file or
other stream. The caller must explicitly print the trailing '\n'
in this call.
The error reporting of fprintf (< 0) is respected. However, we have
already print some information in case that the default (plain text)
output is used, that output is mostly intended for debugging so it
should not be a problem.
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c index 9a0bcfe..2415917 100644 --- a/src/utils.c +++ b/src/utils.c @@ -174,3 +174,30 @@ void xfree(const void *ptr) { free((void *)ptr); } + +int nft_fprintf(FILE *fp, void *obj, uint32_t type, uint32_t flags, + int (*snprintf_cb)(char *buf, size_t bufsiz, void *obj, + uint32_t type, uint32_t flags)) +{ + char _buf[NFT_SNPRINTF_BUFSIZ]; + char *buf = _buf; + size_t bufsiz = sizeof(_buf); + int ret; + + ret = snprintf_cb(buf, bufsiz, obj, type, flags); + if (ret > NFT_SNPRINTF_BUFSIZ) { + buf = calloc(1, ret); + if (buf == NULL) + return -1; + + bufsiz = ret; + ret = snprintf_cb(buf, bufsiz, obj, type, flags); + } + + ret = fprintf(fp, "%s", buf); + + if (buf != _buf) + xfree(buf); + + return ret; +} |