Switching to using the doubly indirect pointer due to the C

pass-by-value semantics.
This commit is contained in:
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. // Created by jake on 12/17/19.
// //
#include <stdio.h>
#include "linkedlist.h" #include "linkedlist.h"
LinkedList *linkedlist_new() { LinkedList *linkedlist_new() {
LinkedList *tmp = (LinkedList *) malloc(sizeof(LinkedList)); LinkedList *tmp = (LinkedList *) malloc(sizeof(LinkedList));
tmp->data = NULL; tmp->data = NULL;
tmp->next = NULL;
return tmp; return tmp;
} }
@ -15,16 +17,15 @@ LinkedList *linkedlist_new() {
* @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.
*/ */
void linkedlist_add(LinkedList *head, char *data) { void linkedlist_add(LinkedList **head, char *data) {
// Create a pointer to the head element. // Create a pointer to the head element.
LinkedList *tmp = head; LinkedList *tmp = *head;
// Head was null? Create a new node. if (tmp->data == NULL) {
if (head == NULL) { tmp->data = data;
head = linkedlist_new();
head->data = data;
} }
else { else {
fprintf(stderr, "Adding to the body\n");
// Iterate until we don't have a next node. // Iterate until we don't have a next node.
while ((tmp = tmp->next) != NULL) ; 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) { void free_node(LinkedList *node) {
free(node->data); free(node->data);
free(node); free(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); void linkedlist_print(LinkedList *head);
#endif //HOSTBLOCKER_LINKEDLIST_H #endif //HOSTBLOCKER_LINKEDLIST_H

5
main.c
View File

@ -21,7 +21,7 @@
/** GLOBALS **/ /** GLOBALS **/
// Our rule we use to blackhole domains // Our rule we use to blackhole domains
const char *blockString = "0.0.0.0 "; const char *blockString = "0.0.0.0 ";
LinkedList *hosts = (LinkedList *) NULL; LinkedList *hosts = NULL;
// The current hardcoded location of the hosts file. // The current hardcoded location of the hosts file.
static char *HOSTFILE = "/etc/hosts"; static char *HOSTFILE = "/etc/hosts";
@ -140,6 +140,9 @@ void daemonize() {
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// Install a sigint handler to help us clean up. // 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); signal(SIGINT, int_handler);
if (getuid() != 0) if (getuid() != 0)
{ {