Switching to using the doubly indirect pointer due to the C

pass-by-value semantics.
pull/4/head
Jacob Windle 2019-12-20 17:33:20 -05:00
parent f1dad4e3e3
commit cdc3a90605
3 changed files with 24 additions and 9 deletions

View File

@ -2,11 +2,13 @@
// Created by jake on 12/17/19.
//
#include <stdio.h>
#include "linkedlist.h"
LinkedList *linkedlist_new() {
LinkedList *tmp = (LinkedList *) malloc(sizeof(LinkedList));
tmp->data = NULL;
tmp->next = NULL;
return tmp;
}
@ -15,16 +17,15 @@ LinkedList *linkedlist_new() {
* @param head The first node in the list.
* @param data Our data to add to the list.
*/
void linkedlist_add(LinkedList *head, char *data) {
void linkedlist_add(LinkedList **head, char *data) {
// Create a pointer to the head element.
LinkedList *tmp = head;
LinkedList *tmp = *head;
// Head was null? Create a new node.
if (head == NULL) {
head = linkedlist_new();
head->data = data;
if (tmp->data == NULL) {
tmp->data = data;
}
else {
fprintf(stderr, "Adding to the body\n");
// Iterate until we don't have a next node.
while ((tmp = tmp->next) != NULL) ;
@ -34,6 +35,17 @@ void linkedlist_add(LinkedList *head, char *data) {
}
}
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);
tmp = tmp->next;
}
}
void free_list(LinkedList *head) {
}
void free_node(LinkedList *node) {
free(node->data);
free(node);

View File

@ -16,6 +16,6 @@ typedef struct linkedlist {
} LinkedList;
LinkedList *linkedlist_new();
void linkedlist_add(LinkedList *head, char *data);
void linkedlist_print(LinkedList *head, char *data);
void linkedlist_add(LinkedList **head, char *data);
void linkedlist_print(LinkedList *head);
#endif //HOSTBLOCKER_LINKEDLIST_H

5
main.c
View File

@ -21,7 +21,7 @@
/** GLOBALS **/
// Our rule we use to blackhole domains
const char *blockString = "0.0.0.0 ";
LinkedList *hosts = (LinkedList *) NULL;
LinkedList *hosts = NULL;
// The current hardcoded location of the hosts file.
static char *HOSTFILE = "/etc/hosts";
@ -140,6 +140,9 @@ 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_print(hosts);
signal(SIGINT, int_handler);
if (getuid() != 0)
{