Working on linked list data structure for hosts.
This commit is contained in:
parent
7f78683035
commit
f367d3a319
@ -1,3 +1,3 @@
|
|||||||
project(HostBlocker)
|
project(HostBlocker)
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
add_executable(hb main.c)
|
add_executable(hb main.c linkedlist.c linkedlist.h)
|
37
linkedlist.c
Normal file
37
linkedlist.c
Normal 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
21
linkedlist.h
Normal 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
7
main.c
@ -6,6 +6,8 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
#include "linkedlist.h"
|
||||||
|
|
||||||
// Macro for status checks.
|
// Macro for status checks.
|
||||||
#define ONFAILED(status, fn) if(status > fn)
|
#define ONFAILED(status, fn) if(status > fn)
|
||||||
|
|
||||||
@ -19,7 +21,8 @@
|
|||||||
/** 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 ";
|
||||||
const char *CONFIG;
|
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 const char *HOSTFILE = "/etc/hosts";
|
||||||
// Our configuration
|
// Our configuration
|
||||||
@ -97,7 +100,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");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user