Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit c4e675a

Browse files
author
Ikey Doherty
committed
csv: Support -o parameter per issue #28
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 1ea94fd commit c4e675a

File tree

1 file changed

+30
-2
lines changed
  • src/plugins/output/csv

1 file changed

+30
-2
lines changed

src/plugins/output/csv/csv.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define _GNU_SOURCE
1313

1414
#include <stdio.h>
15+
#include <errno.h>
1516

1617
#include "config.h"
1718
#include "util.h"
@@ -87,6 +88,19 @@ static bool csv_write_report(CveCheckTool *self)
8788
gchar *key = NULL;
8889
struct source_package_t *v = NULL;
8990

91+
FILE *fd = NULL;
92+
bool ret = false;
93+
94+
if (self->output_file) {
95+
fd = fopen(self->output_file, "w");
96+
if (!fd) {
97+
fprintf(stderr, "Unable to open %s for writing: %s\n", self->output_file, strerror(errno));
98+
return false;
99+
}
100+
} else {
101+
fd = stdout;
102+
}
103+
90104
/* package,version,unpatched CVE numbers space delimited,patched CVE numbers space delimited, open bug count */
91105
g_hash_table_iter_init(&iter, self->db);
92106
while (g_hash_table_iter_next(&iter, (void**)&key, (void**)&v)) {
@@ -117,9 +131,23 @@ static bool csv_write_report(CveCheckTool *self)
117131
} else {
118132
open_bug_count = g_strdup("0");
119133
}
120-
printf("%s,%s,%s,%s,%s\n", key, (char*)v->version, is, pa, open_bug_count);
134+
if (fprintf(fd, "%s,%s,%s,%s,%s\n", key, (char*)v->version, is, pa, open_bug_count) < 0) {
135+
goto io_error;
136+
}
121137
}
122-
return true;
138+
139+
ret = true;
140+
goto success;
141+
142+
io_error:
143+
fprintf(stderr, "Error writing to file: %s\n", strerror(errno));
144+
success:
145+
ret = true;
146+
if (fd != stdout && self->output_file) {
147+
fclose(fd);
148+
}
149+
150+
return ret;
123151
}
124152

125153
_module_export_ bool cve_plugin_module_init(CvePlugin *self)

0 commit comments

Comments
 (0)