From f1dad4e3e334f7f257ccf0375e75985ce18dc575 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 19 Dec 2019 13:47:31 -0500 Subject: [PATCH] Correcting out linkedlist add function. --- linkedlist.c | 21 ++++++++++++--------- linkedlist.h | 4 ++-- main.c | 6 ++++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/linkedlist.c b/linkedlist.c index a51e1e3..2a65f41 100644 --- a/linkedlist.c +++ b/linkedlist.c @@ -14,21 +14,24 @@ LinkedList *linkedlist_new() { * Add a char * to our linked list and set up a new head. * @param head The first node in the list. * @param data Our data to add to the list. - * @return */ -void *linkedlist_add(LinkedList *head, char *data) { +void linkedlist_add(LinkedList *head, char *data) { + // Create a pointer to the head element. + LinkedList *tmp = head; + + // Head was null? Create a new node. if (head == NULL) { head = linkedlist_new(); - } - if (head->data == NULL) { head->data = data; } - LinkedList *tmp = linkedlist_new(); - // Set the head pointer to the next node. - head->next = tmp; + else { + // Iterate until we don't have a next node. + while ((tmp = tmp->next) != NULL) ; - // Set head to tmp. - head = tmp; + tmp->next = linkedlist_new(); + tmp->next->data = data; + tmp->next->next = NULL; + } } void free_node(LinkedList *node) { diff --git a/linkedlist.h b/linkedlist.h index c137ce9..93a4d2e 100644 --- a/linkedlist.h +++ b/linkedlist.h @@ -16,6 +16,6 @@ typedef struct linkedlist { } LinkedList; LinkedList *linkedlist_new(); -void *linkedlist_add(LinkedList *head, char *data); - +void linkedlist_add(LinkedList *head, char *data); +void linkedlist_print(LinkedList *head, char *data); #endif //HOSTBLOCKER_LINKEDLIST_H diff --git a/main.c b/main.c index ab9ae27..931ad6b 100644 --- a/main.c +++ b/main.c @@ -24,7 +24,8 @@ const char *blockString = "0.0.0.0 "; LinkedList *hosts = (LinkedList *) NULL; // The current hardcoded location of the hosts file. -static const char *HOSTFILE = "/etc/hosts"; +static char *HOSTFILE = "/etc/hosts"; +static char *CONFIG; // Our configuration /** @@ -100,6 +101,7 @@ void showHosts() */ int read_config_file() { // increment the refcount for tmp. + linkedlist_add(hosts, "hello.com"); } @@ -179,7 +181,7 @@ int main(int argc, char **argv) showHosts(); } else if (strcmp(argv[i], "-config") == 0) { - CONFIG = argv[i]; + CONFIG = argv[++i]; } // Daemonize this process, allowing for hosts file to be automagically managed.