From 95dada12eb1c7c6d52a52b89e4bf1a56b95d692a Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Wed, 01 Jun 2022 04:52:13 +0000
Subject: [PATCH] checks for server conductibility if user is root

---
 slist.c |   36 +++++++++++++++++++++++++-----------
 1 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/slist.c b/slist.c
index fe2b445..971685f 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,11 +130,23 @@
 
 int print_servers(struct dns_list* head)
 {
-	printf("%-20s | %s\n", "Server", "Time");
-	while (head) {
-		printf("%-20s | %ld.%09ld\n", head->server, head->time.tv_sec,
-		       head->time.tv_nsec);
-		head = head->next;
+    struct dns_list* curr = head;
+	printf("%-16s | %-11s | %s\n", "Server", "Time", "Errors");
+	while (curr) {
+        if (curr->errors != -1) {
+            printf("%-16s | %ld.%09ld | %d\n", curr->server, curr->time.tv_sec,
+                   curr->time.tv_nsec, curr->errors);
+        }
+        curr = curr->next;
 	}
+    fflush(stdout);
+    curr = head;
+    if (head->errors == -1) {
+        printf("The following servers were unreachable:\n");
+        while (curr && curr->errors == -1) {
+            printf("%s\n", curr->server);
+            curr = curr->next;
+        }
+    }
 	return 0;
 }

--
Gitblit v1.10.0