From f367d3a3191315dda7f00a1351e885e37247f46c Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 18 Dec 2019 13:46:16 -0500 Subject: [PATCH] Working on linked list data structure for hosts. --- CMakeLists.txt | 2 +- linkedlist.c | 37 +++++++++++++++++++++++++++++++++++++ linkedlist.h | 21 +++++++++++++++++++++ main.c | 7 +++++-- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 linkedlist.c create mode 100644 linkedlist.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 619ee79..81bbc27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,3 @@ project(HostBlocker) find_package(PkgConfig REQUIRED) -add_executable(hb main.c) \ No newline at end of file +add_executable(hb main.c linkedlist.c linkedlist.h) \ No newline at end of file diff --git a/linkedlist.c b/linkedlist.c new file mode 100644 index 0000000..a51e1e3 --- /dev/null +++ b/linkedlist.c @@ -0,0 +1,37 @@ +// +// Created by jake on 12/17/19. +// + +#include "linkedlist.h" + +LinkedList *linkedlist_new() { + LinkedList *tmp = (LinkedList *) malloc(sizeof(LinkedList)); + tmp->data = NULL; + return tmp; +} + +/** + * 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) { + 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; + + // Set head to tmp. + head = tmp; +} + +void free_node(LinkedList *node) { + free(node->data); + free(node); +} \ No newline at end of file diff --git a/linkedlist.h b/linkedlist.h new file mode 100644 index 0000000..c137ce9 --- /dev/null +++ b/linkedlist.h @@ -0,0 +1,21 @@ +// +// Created by jake on 12/17/19. +// + +#ifndef HOSTBLOCKER_LINKEDLIST_H +#define HOSTBLOCKER_LINKEDLIST_H + +#include + +/** + * Our linked list structure to be used for holding configuration files. + */ +typedef struct linkedlist { + char *data; + struct linkedlist *next; +} LinkedList; + +LinkedList *linkedlist_new(); +void *linkedlist_add(LinkedList *head, char *data); + +#endif //HOSTBLOCKER_LINKEDLIST_H diff --git a/main.c b/main.c index db7902e..ab9ae27 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,8 @@ #include #include +#include "linkedlist.h" + // Macro for status checks. #define ONFAILED(status, fn) if(status > fn) @@ -19,7 +21,8 @@ /** GLOBALS **/ // Our rule we use to blackhole domains const char *blockString = "0.0.0.0 "; -const char *CONFIG; +LinkedList *hosts = (LinkedList *) NULL; + // The current hardcoded location of the hosts file. static const char *HOSTFILE = "/etc/hosts"; // Our configuration @@ -97,7 +100,7 @@ void showHosts() */ int read_config_file() { // increment the refcount for tmp. - + linkedlist_add(hosts, "hello.com"); } /**