diff options
author | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-04-19 15:07:13 -0300 |
---|---|---|
committer | Rafael G. Martins <rafael@rafaelmartins.eng.br> | 2015-04-19 15:07:13 -0300 |
commit | 54fa2d51eb9096e265af18ad2f916eedfc018858 (patch) | |
tree | 28efb3dc997260c1affe032180cf6ba2c34c50f2 /src/main.c | |
parent | b81e0c2b1c70badf4131cf8a587a06e1c31df1f5 (diff) | |
download | blogc-54fa2d51eb9096e265af18ad2f916eedfc018858.tar.gz blogc-54fa2d51eb9096e265af18ad2f916eedfc018858.tar.bz2 blogc-54fa2d51eb9096e265af18ad2f916eedfc018858.zip |
main: handle -o properly
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 23 |
1 files changed, 21 insertions, 2 deletions
@@ -10,6 +10,7 @@ #include <config.h> #endif /* HAVE_CONFIG_H */ +#include <errno.h> #include <stdbool.h> #include <stdio.h> #include <string.h> @@ -104,9 +105,27 @@ main(int argc, char **argv) } char *out = blogc_render(l, s); - printf("%s", out); - free(out); + bool write_to_stdout = (output == NULL || (0 == strcmp(output, "-"))); + + FILE *fp = stdout; + if (!write_to_stdout) { + fp = fopen(output, "w"); + if (fp == NULL) { + fprintf(stderr, "blogc: error: failed to open output file (%s): %s\n", + output, strerror(errno)); + rv = 2; + goto cleanup4; + } + } + + fprintf(fp, "%s", out); + + if (!write_to_stdout) + fclose(fp); + +cleanup4: + free(out); cleanup3: b_slist_free_full(s, (b_free_func_t) b_trie_free); cleanup2: |