From 1002e8a153097adb4273e6807b3a7cce7ba2df25 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Mon, 23 May 2022 13:42:03 +0000
Subject: [PATCH] Count number of errors recorded and print in error column

---
 slist.c |   20 +++++++++++---------
 main.c  |   21 ++++++++++++---------
 slist.h |    1 +
 3 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/main.c b/main.c
index e9f7e63..340187f 100644
--- a/main.c
+++ b/main.c
@@ -116,6 +116,7 @@
 	struct dns_list* dns = (struct dns_list*)in;
 	dns->time.tv_sec = 0;
 	dns->time.tv_nsec = 0;
+    dns->errors = 0;
 	for (int i = 0; i < num_tests; i++) {
 		struct hosts_list* curr = hosts;
 		while (curr) {
@@ -126,16 +127,18 @@
 			for (int j = 0; j < 3 && run.tv_sec == -1; j++) {
 				run = resolve(buf, curr->server, dns->server, T_A);
 			}
-			if (run.tv_sec == -1) // if test has failed 3 times, set time taken
+			if (run.tv_sec == -1) { // if test has failed 3 times, set time taken
 			                      // to 3s as penalty
-				run.tv_sec = 3;
-			dns->time.tv_sec += run.tv_sec;
-			dns->time.tv_nsec += run.tv_nsec;
-			if (dns->time.tv_nsec >=
-			    1000000000) { // nanoseconds have overflowed into seconds
-				dns->time.tv_sec += 1;
-				dns->time.tv_nsec -= 1000000000;
-			}
+                dns->errors++;
+            } else {
+                dns->time.tv_sec += run.tv_sec;
+                dns->time.tv_nsec += run.tv_nsec;
+                if (dns->time.tv_nsec >=
+                    1000000000) { // nanoseconds have overflowed into seconds
+                    dns->time.tv_sec += 1;
+                    dns->time.tv_nsec -= 1000000000;
+                }
+            }
 			tests_done++;
 			curr = curr->next;
 		}
diff --git a/slist.c b/slist.c
index fe2b445..db2e935 100644
--- a/slist.c
+++ b/slist.c
@@ -4,7 +4,7 @@
 
 void split(struct dns_list* head, struct dns_list** a, struct dns_list** b);
 struct dns_list* merge(struct dns_list* a, struct dns_list* b);
-int comp_times(struct timespec a, struct timespec b);
+int comp_times(struct dns_list* a, struct dns_list* b);
 
 int add_hosts_server(struct hosts_list** head, char* server)
 {
@@ -103,7 +103,7 @@
 	if (!a) return b;
 	if (!b) return a;
 
-	if (comp_times(a->time, b->time) > 0) {
+	if (comp_times(a, b) > 0) {
 		out = b;
 		out->next = merge(a, b->next);
 	} else {
@@ -113,14 +113,16 @@
 	return out;
 }
 
-int comp_times(struct timespec a, struct timespec b)
+int comp_times(struct dns_list* a, struct dns_list* b)
 {
-	if (a.tv_sec == b.tv_sec) {
-		if (a.tv_nsec >= b.tv_nsec)
+    if (a->errors != b->errors) {
+        return a->errors > b->errors;
+    } else if (a->time.tv_sec == b->time.tv_sec) {
+		if (a->time.tv_nsec >= b->time.tv_nsec)
 			return 1;
 		else
 			return -1;
-	} else if (a.tv_sec > b.tv_sec) {
+	} else if (a->time.tv_sec > b->time.tv_sec) {
 		return 1;
 	} else
 		return -1;
@@ -128,10 +130,10 @@
 
 int print_servers(struct dns_list* head)
 {
-	printf("%-20s | %s\n", "Server", "Time");
+	printf("%-16s | %-11s | %s\n", "Server", "Time", "Errors");
 	while (head) {
-		printf("%-20s | %ld.%09ld\n", head->server, head->time.tv_sec,
-		       head->time.tv_nsec);
+		printf("%-16s | %ld.%09ld | %d\n", head->server, head->time.tv_sec,
+		       head->time.tv_nsec, head->errors);
 		head = head->next;
 	}
 	return 0;
diff --git a/slist.h b/slist.h
index 399ab05..0d0c89a 100644
--- a/slist.h
+++ b/slist.h
@@ -8,6 +8,7 @@
 struct dns_list {
 	char* server;
 	struct timespec time;
+    int errors;
 	struct dns_list* next;
 };
 

--
Gitblit v1.10.0