Correcting out linkedlist add function.

This commit is contained in:
Jacob Windle 2019-12-19 13:47:31 -05:00
parent f367d3a319
commit f1dad4e3e3
3 changed files with 18 additions and 13 deletions

View File

@ -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) {

View File

@ -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

6
main.c
View File

@ -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.