From 3dda0a77a578e584fb8048b64f3c91e1650c0ed1 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sat, 11 Nov 2017 23:09:33 +0000 Subject: [PATCH] output: deduplicate file closing code Signed-off-by: Sami Kerola --- src/output.c | 170 ++++++++++++--------------------------------------- 1 file changed, 40 insertions(+), 130 deletions(-) diff --git a/src/output.c b/src/output.c index e858a96..225e8cf 100644 --- a/src/output.c +++ b/src/output.c @@ -143,6 +143,33 @@ static int start_color(struct output_helper_t *oh, FILE *outfile) } return 0; } + +static FILE *open_outfile(void) +{ + FILE *outfile; + + if (config.output_file[0]) { + outfile = fopen(config.output_file, "w+"); + if (outfile == NULL) { + error(EXIT_FAILURE, errno, "open_outfile: %s", config.output_file); + } + } else { + outfile = stdout; + } + return outfile; +} + +static void close_outfile(FILE *outfile) +{ + if (outfile == stdout) { + if (fflush(stdout)) + error(EXIT_FAILURE, errno, "close_outfile: fflush"); + } else { + if (close_stream(outfile)) + error(EXIT_FAILURE, errno, "close_outfile: fclose"); + } +} + /*! \brief Text output format, which is the default. */ static int output_txt(void) { @@ -150,7 +177,6 @@ static int output_txt(void) struct range_t *range_p; struct shared_network_t *shared_p; struct output_helper_t oh; - int ret; FILE *outfile; int max_ipaddr_length = config.ip_version == IPv6 ? 39 : 16; @@ -158,15 +184,7 @@ static int output_txt(void) config.color_mode = color_on; } - if (config.output_file[0]) { - outfile = fopen(config.output_file, "w+"); - if (outfile == NULL) { - error(EXIT_FAILURE, errno, "output_txt: %s", config.output_file); - } - } else { - outfile = stdout; - } - + outfile = open_outfile(); range_p = ranges; shared_p = shared_networks; @@ -301,18 +319,7 @@ static int output_txt(void) fputs(COLOR_RESET, outfile); fprintf(outfile, "\n"); } - if (outfile == stdout) { - ret = fflush(stdout); - if (ret) { - error(EXIT_FAILURE, errno, "output_txt: fflush"); - } - } else { - ret = close_stream(outfile); - if (ret) { - error(EXIT_FAILURE, errno, "output_txt: fclose"); - } - } - + close_outfile(outfile); return 0; } @@ -323,18 +330,9 @@ static int output_xml(const int print_mac_addreses) struct range_t *range_p; struct shared_network_t *shared_p; struct output_helper_t oh; - int ret; FILE *outfile; - if (config.output_file[0]) { - outfile = fopen(config.output_file, "w+"); - if (outfile == NULL) { - error(EXIT_FAILURE, errno, "output_xml: %s", config.output_file); - } - } else { - outfile = stdout; - } - + outfile = open_outfile(); range_p = ranges; shared_p = shared_networks; @@ -410,18 +408,7 @@ static int output_xml(const int print_mac_addreses) } fprintf(outfile, "\n"); - if (outfile == stdout) { - ret = fflush(stdout); - if (ret) { - error(EXIT_FAILURE, errno, "output_xml: fflush"); - } - } else { - ret = close_stream(outfile); - if (ret) { - error(EXIT_FAILURE, errno, "output_xml: fclose"); - } - } - + close_outfile(outfile); return 0; } @@ -432,19 +419,10 @@ static int output_json(const int print_mac_addreses) struct range_t *range_p; struct shared_network_t *shared_p; struct output_helper_t oh; - int ret; FILE *outfile; unsigned int sep; - if (config.output_file[0]) { - outfile = fopen(config.output_file, "w+"); - if (outfile == NULL) { - error(EXIT_FAILURE, errno, "output_json: %s", config.output_file); - } - } else { - outfile = stdout; - } - + outfile = open_outfile(); range_p = ranges; shared_p = shared_networks; sep = 0; @@ -587,20 +565,8 @@ static int output_json(const int print_mac_addreses) fprintf(outfile, " \"status\":%d\n", oh.status); fprintf(outfile, " }"); /* end of summary */ } - fprintf(outfile, "\n}\n"); - if (outfile == stdout) { - ret = fflush(stdout); - if (ret) { - error(EXIT_FAILURE, errno, "output_json: fflush"); - } - } else { - ret = close_stream(outfile); - if (ret) { - error(EXIT_FAILURE, errno, "output_json: fclose"); - } - } - + close_outfile(outfile); return 0; } @@ -753,18 +719,9 @@ static int output_html(void) struct range_t *range_p; struct shared_network_t *shared_p; struct output_helper_t oh; - int ret; FILE *outfile; - if (config.output_file[0]) { - outfile = fopen(config.output_file, "w+"); - if (outfile == NULL) { - error(EXIT_FAILURE, errno, "output_html: %s", config.output_file); - } - } else { - outfile = stdout; - } - + outfile = open_outfile(); range_p = ranges; shared_p = shared_networks; html_header(outfile); @@ -903,17 +860,7 @@ static int output_html(void) } table_end(outfile); html_footer(outfile); - if (outfile == stdout) { - ret = fflush(stdout); - if (ret) { - error(EXIT_FAILURE, errno, "output_html: fflush"); - } - } else { - ret = close_stream(outfile); - if (ret) { - error(EXIT_FAILURE, errno, "output_html: fclose"); - } - } + close_outfile(outfile); return 0; } @@ -925,17 +872,8 @@ static int output_csv(void) struct shared_network_t *shared_p; struct output_helper_t oh; FILE *outfile; - int ret; - - if (config.output_file[0]) { - outfile = fopen(config.output_file, "w+"); - if (outfile == NULL) { - error(EXIT_FAILURE, errno, "output_csv: %s", config.output_file); - } - } else { - outfile = stdout; - } + outfile = open_outfile(); range_p = ranges; shared_p = shared_networks; if (config.header_limit & R_BIT) { @@ -1043,17 +981,7 @@ static int output_csv(void) } fprintf(outfile, "\n"); } - if (outfile == stdout) { - ret = fflush(stdout); - if (ret) { - error(EXIT_FAILURE, errno, "output_cvs: fflush"); - } - } else { - ret = close_stream(outfile); - if (ret) { - error(EXIT_FAILURE, errno, "output_cvs: fclose"); - } - } + close_outfile(outfile); return 0; } @@ -1067,21 +995,13 @@ static int output_alarming(void) struct output_helper_t oh; unsigned int i; int rw = 0, rc = 0, ro = 0, ri = 0, sw = 0, sc = 0, so = 0, si = 0; - int ret_val, ret; + int ret_val; + outfile = open_outfile(); range_p = ranges; range_size = get_range_size(range_p); shared_p = shared_networks; - if (config.output_file[0]) { - outfile = fopen(config.output_file, "w+"); - if (outfile == NULL) { - error(EXIT_FAILURE, errno, "output_alarming: %s", config.output_file); - } - } else { - outfile = stdout; - } - if (config.number_limit & R_BIT) { for (i = 0; i < num_ranges; i++) { range_output_helper(&oh, range_p); @@ -1221,17 +1141,7 @@ static int output_alarming(void) } } fprintf(outfile, "\n"); - if (outfile == stdout) { - ret = fflush(stdout); - if (ret) { - error(EXIT_FAILURE, errno, "output_alarming: fflush"); - } - } else { - ret = close_stream(outfile); - if (ret) { - error(EXIT_FAILURE, errno, "output_alarming: fclose"); - } - } + close_outfile(outfile); return ret_val; }