From 4db521516667fd8be24df108f6fb2c8e116486fb Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 10 Jan 2020 22:17:21 -0500 Subject: [PATCH] Working on method that reads config file. --- main.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index a6edf4f..d518efd 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "linkedlist.h" @@ -105,12 +106,26 @@ void showHosts() /** * Read the configuration file and give a return code to indicate any changes. + * TODO: this method needs to create/delete hosts. * @return 1 if successful, 0 if otherwise. */ int read_config_file() { // increment the refcount for tmp. + FILE *config = fopen(CONFIG, "r"); + char buf[1024]; + if (config == NULL) { + fprintf(stderr, "Error trying to open config file: %s\n", strerror(errno)); + } - linkedlist_add(&hosts, "hello.com"); + else { + // Add each line from the file to the linked list. + while (fgets(buf, 1024, config) != NULL) { + buf[strlen(buf) - 1] = 0; + + // We should add a method that overwrites the linked list node at an index. + linkedlist_add(&hosts, buf); + } + } } /**