From 585d68902891b6377a90649cc08d13661653f56c Mon Sep 17 00:00:00 2001
From: Alexey Makhalov <amakhalov@vmware.com>
Date: Wed, 31 Aug 2016 11:06:17 +0000
Subject: [PATCH 2/6] Alternative csv output

---
 src/plugins/Makefile.am             |  1 +
 src/plugins/output/csv2/Makefile.am | 16 +++++++
 src/plugins/output/csv2/csv2.c      | 96 +++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 src/plugins/output/csv2/Makefile.am
 create mode 100644 src/plugins/output/csv2/csv2.c

diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
index 0d56f8e..2c728af 100644
--- a/src/plugins/Makefile.am
+++ b/src/plugins/Makefile.am
@@ -6,6 +6,7 @@ pkglib_LTLIBRARIES =
 # Output plugins
 include output/cli/Makefile.am
 include output/csv/Makefile.am
+include output/csv2/Makefile.am
 include output/html/Makefile.am
 
 # Packaging plugins
diff --git a/src/plugins/output/csv2/Makefile.am b/src/plugins/output/csv2/Makefile.am
new file mode 100644
index 0000000..dd753d4
--- /dev/null
+++ b/src/plugins/output/csv2/Makefile.am
@@ -0,0 +1,16 @@
+pkglib_LTLIBRARIES += \
+	csv2.la
+
+csv2_la_SOURCES = \
+	output/csv2/csv2.c
+
+csv2_la_LIBADD = \
+	$(MODULE_COMMON_LIBS) \
+	${top_builddir}/src/libcve.la
+
+csv2_la_CFLAGS = \
+	$(MODULE_COMMON_CFLAGS) \
+	$(AM_CFLAGS)
+
+csv2_la_LDFLAGS = \
+	$(MODULE_FLAGS)
diff --git a/src/plugins/output/csv2/csv2.c b/src/plugins/output/csv2/csv2.c
new file mode 100644
index 0000000..fe9f579
--- /dev/null
+++ b/src/plugins/output/csv2/csv2.c
@@ -0,0 +1,96 @@
+/*
+ * csv2.c - CSV output
+ *
+ * Copyright (C) 2016 Alexey Makhalov <amakhalov@vmware.com>
+ *
+ * cve-check-tool is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <errno.h>
+
+#include "config.h"
+#include "util.h"
+#include "cve-check-tool.h"
+#include "plugin.h"
+
+static bool csv_write_report(CveCheckTool *self)
+{
+        GHashTableIter iter;
+        gchar *key = NULL;
+        struct source_package_t *v = NULL;
+        struct cve_entry_t *entry = NULL;
+        GList *c = NULL;
+        FILE *fd = NULL;
+        bool ret = false;
+
+        if (self->output_file) {
+                fd = fopen(self->output_file, "w");
+                if (!fd) {
+                        fprintf(stderr, "Unable to open %s for writing: %s\n", self->output_file, strerror(errno));
+                        return false;
+                }
+        } else {
+                fd = stdout;
+        }
+
+        /* CVE score|CVE number|package name|CVE summary */
+        g_hash_table_iter_init(&iter, self->db);
+        while (g_hash_table_iter_next(&iter, (void**)&key, (void**)&v)) {
+                if (!v->issues && !v->patched && !self->show_unaffected) {
+                        continue;
+                }
+                if (!v->issues && self->hide_patched) {
+                        continue;
+                }
+                for (c = v->issues; c; c = c->next) {
+                        entry = cve_db_get_cve(self->cve_db, (gchar*)c->data);
+                        if (self->modified > 0 && entry->modified > self->modified) {
+                                cve_free(entry);
+                                continue;
+                        }
+                        if (fprintf(fd, "%s|%s|%s|%s\n", entry->score, entry->id, key, entry->summary) < 0) {
+                                goto io_error;
+                        }
+                }
+        }
+
+        ret = true;
+        goto success;
+
+io_error:
+        fprintf(stderr, "Error writing to file: %s\n", strerror(errno));
+success:
+        ret = true;
+        if (fd != stdout && self->output_file) {
+                fclose(fd);
+        }
+
+        return ret;
+}
+
+_module_export_ bool cve_plugin_module_init(CvePlugin *self)
+{
+        self->report = csv_write_report;
+        self->flags = PLUGIN_TYPE_REPORT;
+        self->name = "csv2";
+        return true;
+}
+
+/*
+ * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * vi: set shiftwidth=8 tabstop=8 expandtab:
+ * :indentSize=8:tabSize=8:noTabs=true:
+ */
-- 
2.10.1