diff --git a/linkedlist.c b/linkedlist.c index bd76ae2..f98429b 100644 --- a/linkedlist.c +++ b/linkedlist.c @@ -3,6 +3,7 @@ // #include +#include #include "linkedlist.h" LinkedList *linkedlist_new() { @@ -12,6 +13,12 @@ LinkedList *linkedlist_new() { return tmp; } +static LinkedList *_linkedlist_new_with_data(char *data) { + LinkedList *tmp = linkedlist_new(); + tmp->data = strdup(data); + return tmp; +} + /** * Add a char * to our linked list and set up a new head. * @param head The first node in the list. @@ -21,22 +28,20 @@ void linkedlist_add(LinkedList **head, char *data) { // Create a pointer to the head element. LinkedList *tmp = *head; - if (tmp->data == NULL) { - tmp->data = data; + if (tmp == NULL) { + *head = _linkedlist_new_with_data(data); } else { - fprintf(stderr, "Adding to the body\n"); // Iterate until we don't have a next node. - while ((tmp = tmp->next) != NULL) ; + while (tmp->next != NULL) + tmp = tmp->next; - tmp->next = linkedlist_new(); - tmp->next->data = data; + tmp->next = _linkedlist_new_with_data(data); tmp->next->next = NULL; } } void linkedlist_print(LinkedList *head) { - fprintf(stderr, "Entering linked list print\n"); LinkedList *tmp = head; while (tmp != NULL) { fprintf(stderr, "Node: %s\n", tmp->data); diff --git a/main.c b/main.c index dbcf222..a5f4c0a 100644 --- a/main.c +++ b/main.c @@ -102,7 +102,7 @@ void showHosts() int read_config_file() { // increment the refcount for tmp. - linkedlist_add(hosts, "hello.com"); + linkedlist_add(&hosts, "hello.com"); } /** @@ -140,8 +140,8 @@ void daemonize() { int main(int argc, char **argv) { // Install a sigint handler to help us clean up. - linkedlist_add(hosts, "hello.com"); - linkedlist_add(hosts, "heloworld.com"); + linkedlist_add(&hosts, "hello.com"); + linkedlist_add(&hosts, "heloworld.com"); linkedlist_print(hosts); signal(SIGINT, int_handler); if (getuid() != 0)