Working on linked list data structure for hosts.

pull/4/head
Jacob Windle 2019-12-18 13:46:16 -05:00
parent 7f78683035
commit f367d3a319
4 changed files with 64 additions and 3 deletions

View File

@ -1,3 +1,3 @@
project(HostBlocker)
find_package(PkgConfig REQUIRED)
add_executable(hb main.c)
add_executable(hb main.c linkedlist.c linkedlist.h)

37
linkedlist.c Normal file
View File

@ -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);
}

21
linkedlist.h Normal file
View File

@ -0,0 +1,21 @@
//
// Created by jake on 12/17/19.
//
#ifndef HOSTBLOCKER_LINKEDLIST_H
#define HOSTBLOCKER_LINKEDLIST_H
#include <stdlib.h>
/**
* 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

7
main.c
View File

@ -6,6 +6,8 @@
#include <sys/types.h>
#include <sys/wait.h>
#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");
}
/**