commit 3697b0d1cce2223c9d094830776cf3cf282c3b7f
parent 2777bf97572783865fc805cd07eb71d89e02cdcb
Author: Frederic Cambus <fred@statdns.com>
Date: Thu, 3 Jan 2019 12:34:54 +0100
Initial C version, only reading the file line by line for now.
Diffstat:
M | LICENSE | | | 2 | +- |
A | src/config.h | | | 33 | +++++++++++++++++++++++++++++++++ |
A | src/statzone.c | | | 111 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 145 insertions(+), 1 deletion(-)
diff --git a/LICENSE b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2012-2017, Frederic Cambus
+Copyright (c) 2012-2019, Frederic Cambus
All rights reserved.
Redistribution and use in source and binary forms, with or without
diff --git a/src/config.h b/src/config.h
@@ -0,0 +1,33 @@
+/*
+ * StatZone
+ * Copyright (c) 2012-2019, Frederic Cambus
+ * https://www.statdns.com
+ *
+ * Created: 2012-02-13
+ * Last Updated: 2019-01-03
+ *
+ * StatZone is released under the BSD 2-Clause license
+ * See LICENSE file for details.
+ */
+
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#define VERSION "StatZone 1.0.0"
+
+enum {
+ LINE_LENGTH_MAX = 65536
+};
+
+struct results {
+ uint64_t processedLines;
+ uint64_t a;
+ uint64_t aaaa;
+ uint64_t ds;
+ uint64_t ns;
+ uint64_t domains;
+ uint64_t idn;
+ double runtime;
+};
+
+#endif /* CONFIG_H */
diff --git a/src/statzone.c b/src/statzone.c
@@ -0,0 +1,111 @@
+/*
+ * StatZone
+ * Copyright (c) 2012-2019, Frederic Cambus
+ * https://www.statdns.com
+ *
+ * Created: 2012-02-13
+ * Last Updated: 2019-01-03
+ *
+ * StatZone is released under the BSD 2-Clause license
+ * See LICENSE file for details.
+ */
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <err.h>
+#include <getopt.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+#include "config.h"
+
+struct timespec begin, end, elapsed;
+
+char lineBuffer[LINE_LENGTH_MAX];
+
+struct results results;
+
+FILE *zoneFile;
+struct stat zoneFileStat;
+
+int8_t getoptFlag;
+
+char *intputFile;
+
+void
+displayUsage() {
+ printf("USAGE: statzone [options] inputfile\n\n" \
+ "Options are:\n\n" \
+ " -h Display usage\n" \
+ " -v Display version\n");
+}
+
+int
+main(int argc, char *argv[]) {
+ while ((getoptFlag = getopt(argc, argv, "hv")) != -1) {
+ switch(getoptFlag) {
+
+ case 'h':
+ displayUsage();
+ return 0;
+
+ case 'v':
+ printf("%s\n", VERSION);
+ return 0;
+ }
+ }
+
+ if (optind < argc) {
+ intputFile = argv[optind];
+ } else {
+ displayUsage();
+ return 0;
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ /* Starting timer */
+ clock_gettime(CLOCK_MONOTONIC, &begin);
+
+ /* Open log file */
+ if (!strcmp(intputFile, "-")) {
+ /* Read from standard input */
+ zoneFile = stdin;
+ } else {
+ /* Attempt to read from file */
+ if (!(zoneFile = fopen(intputFile, "r"))) {
+ perror("Can't open log file");
+ return 1;
+ }
+ }
+
+ /* Get log file size */
+ if (fstat(fileno(zoneFile), &zoneFileStat)) {
+ perror("Can't stat log file");
+ return 1;
+ }
+
+ while (fgets(lineBuffer, LINE_LENGTH_MAX, zoneFile)) {
+ if (lineBuffer)
+ results.processedLines++;
+ }
+
+ /* Stopping timer */
+ clock_gettime(CLOCK_MONOTONIC, &end);
+
+ timespecsub(&end, &begin, &elapsed);
+ results.runtime = elapsed.tv_sec + elapsed.tv_nsec / 1E9;
+
+ /* Printing results */
+ fprintf(stderr, "Processed %" PRIu64 " lines in %f seconds\n", results.processedLines, results.runtime);
+
+ /* Clean up */
+ fclose(zoneFile);
+
+ return 0;
+}