Correcting out linkedlist add function.

pull/4/head
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. * Add a char * to our linked list and set up a new head.
* @param head The first node in the list. * @param head The first node in the list.
* @param data Our data to add to 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) { if (head == NULL) {
head = linkedlist_new(); head = linkedlist_new();
}
if (head->data == NULL) {
head->data = data; head->data = data;
} }
LinkedList *tmp = linkedlist_new(); else {
// Set the head pointer to the next node. // Iterate until we don't have a next node.
head->next = tmp; while ((tmp = tmp->next) != NULL) ;
// Set head to tmp. tmp->next = linkedlist_new();
head = tmp; tmp->next->data = data;
tmp->next->next = NULL;
}
} }
void free_node(LinkedList *node) { void free_node(LinkedList *node) {

View File

@ -16,6 +16,6 @@ typedef struct linkedlist {
} LinkedList; } LinkedList;
LinkedList *linkedlist_new(); 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 #endif //HOSTBLOCKER_LINKEDLIST_H

6
main.c
View File

@ -24,7 +24,8 @@ const char *blockString = "0.0.0.0 ";
LinkedList *hosts = (LinkedList *) NULL; LinkedList *hosts = (LinkedList *) NULL;
// The current hardcoded location of the hosts file. // The current hardcoded location of the hosts file.
static const char *HOSTFILE = "/etc/hosts"; static char *HOSTFILE = "/etc/hosts";
static char *CONFIG;
// Our configuration // Our configuration
/** /**
@ -100,6 +101,7 @@ void showHosts()
*/ */
int read_config_file() { int read_config_file() {
// increment the refcount for tmp. // increment the refcount for tmp.
linkedlist_add(hosts, "hello.com"); linkedlist_add(hosts, "hello.com");
} }
@ -179,7 +181,7 @@ int main(int argc, char **argv)
showHosts(); showHosts();
} }
else if (strcmp(argv[i], "-config") == 0) { else if (strcmp(argv[i], "-config") == 0) {
CONFIG = argv[i]; CONFIG = argv[++i];
} }
// Daemonize this process, allowing for hosts file to be automagically managed. // Daemonize this process, allowing for hosts file to be automagically managed.