output: deduplicate file closing code

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2017-11-11 23:09:33 +00:00
parent 7575294c36
commit 3dda0a77a5
No known key found for this signature in database
GPG key ID: A9553245FDE9B739

View file

@ -143,6 +143,33 @@ static int start_color(struct output_helper_t *oh, FILE *outfile)
} }
return 0; 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. */ /*! \brief Text output format, which is the default. */
static int output_txt(void) static int output_txt(void)
{ {
@ -150,7 +177,6 @@ static int output_txt(void)
struct range_t *range_p; struct range_t *range_p;
struct shared_network_t *shared_p; struct shared_network_t *shared_p;
struct output_helper_t oh; struct output_helper_t oh;
int ret;
FILE *outfile; FILE *outfile;
int max_ipaddr_length = config.ip_version == IPv6 ? 39 : 16; int max_ipaddr_length = config.ip_version == IPv6 ? 39 : 16;
@ -158,15 +184,7 @@ static int output_txt(void)
config.color_mode = color_on; config.color_mode = color_on;
} }
if (config.output_file[0]) { outfile = open_outfile();
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
error(EXIT_FAILURE, errno, "output_txt: %s", config.output_file);
}
} else {
outfile = stdout;
}
range_p = ranges; range_p = ranges;
shared_p = shared_networks; shared_p = shared_networks;
@ -301,18 +319,7 @@ static int output_txt(void)
fputs(COLOR_RESET, outfile); fputs(COLOR_RESET, outfile);
fprintf(outfile, "\n"); fprintf(outfile, "\n");
} }
if (outfile == stdout) { close_outfile(outfile);
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");
}
}
return 0; return 0;
} }
@ -323,18 +330,9 @@ static int output_xml(const int print_mac_addreses)
struct range_t *range_p; struct range_t *range_p;
struct shared_network_t *shared_p; struct shared_network_t *shared_p;
struct output_helper_t oh; struct output_helper_t oh;
int ret;
FILE *outfile; FILE *outfile;
if (config.output_file[0]) { outfile = open_outfile();
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
error(EXIT_FAILURE, errno, "output_xml: %s", config.output_file);
}
} else {
outfile = stdout;
}
range_p = ranges; range_p = ranges;
shared_p = shared_networks; shared_p = shared_networks;
@ -410,18 +408,7 @@ static int output_xml(const int print_mac_addreses)
} }
fprintf(outfile, "</dhcpstatus>\n"); fprintf(outfile, "</dhcpstatus>\n");
if (outfile == stdout) { close_outfile(outfile);
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");
}
}
return 0; return 0;
} }
@ -432,19 +419,10 @@ static int output_json(const int print_mac_addreses)
struct range_t *range_p; struct range_t *range_p;
struct shared_network_t *shared_p; struct shared_network_t *shared_p;
struct output_helper_t oh; struct output_helper_t oh;
int ret;
FILE *outfile; FILE *outfile;
unsigned int sep; unsigned int sep;
if (config.output_file[0]) { outfile = open_outfile();
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
error(EXIT_FAILURE, errno, "output_json: %s", config.output_file);
}
} else {
outfile = stdout;
}
range_p = ranges; range_p = ranges;
shared_p = shared_networks; shared_p = shared_networks;
sep = 0; sep = 0;
@ -587,20 +565,8 @@ static int output_json(const int print_mac_addreses)
fprintf(outfile, " \"status\":%d\n", oh.status); fprintf(outfile, " \"status\":%d\n", oh.status);
fprintf(outfile, " }"); /* end of summary */ fprintf(outfile, " }"); /* end of summary */
} }
fprintf(outfile, "\n}\n"); fprintf(outfile, "\n}\n");
if (outfile == stdout) { close_outfile(outfile);
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");
}
}
return 0; return 0;
} }
@ -753,18 +719,9 @@ static int output_html(void)
struct range_t *range_p; struct range_t *range_p;
struct shared_network_t *shared_p; struct shared_network_t *shared_p;
struct output_helper_t oh; struct output_helper_t oh;
int ret;
FILE *outfile; FILE *outfile;
if (config.output_file[0]) { outfile = open_outfile();
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
error(EXIT_FAILURE, errno, "output_html: %s", config.output_file);
}
} else {
outfile = stdout;
}
range_p = ranges; range_p = ranges;
shared_p = shared_networks; shared_p = shared_networks;
html_header(outfile); html_header(outfile);
@ -903,17 +860,7 @@ static int output_html(void)
} }
table_end(outfile); table_end(outfile);
html_footer(outfile); html_footer(outfile);
if (outfile == stdout) { close_outfile(outfile);
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");
}
}
return 0; return 0;
} }
@ -925,17 +872,8 @@ static int output_csv(void)
struct shared_network_t *shared_p; struct shared_network_t *shared_p;
struct output_helper_t oh; struct output_helper_t oh;
FILE *outfile; 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; range_p = ranges;
shared_p = shared_networks; shared_p = shared_networks;
if (config.header_limit & R_BIT) { if (config.header_limit & R_BIT) {
@ -1043,17 +981,7 @@ static int output_csv(void)
} }
fprintf(outfile, "\n"); fprintf(outfile, "\n");
} }
if (outfile == stdout) { close_outfile(outfile);
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");
}
}
return 0; return 0;
} }
@ -1067,21 +995,13 @@ static int output_alarming(void)
struct output_helper_t oh; struct output_helper_t oh;
unsigned int i; unsigned int i;
int rw = 0, rc = 0, ro = 0, ri = 0, sw = 0, sc = 0, so = 0, si = 0; 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_p = ranges;
range_size = get_range_size(range_p); range_size = get_range_size(range_p);
shared_p = shared_networks; 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) { if (config.number_limit & R_BIT) {
for (i = 0; i < num_ranges; i++) { for (i = 0; i < num_ranges; i++) {
range_output_helper(&oh, range_p); range_output_helper(&oh, range_p);
@ -1221,17 +1141,7 @@ static int output_alarming(void)
} }
} }
fprintf(outfile, "\n"); fprintf(outfile, "\n");
if (outfile == stdout) { close_outfile(outfile);
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");
}
}
return ret_val; return ret_val;
} }