From c89f53cc627133217a04aa0633beeaf54d17baa0 Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Mon, 13 Jan 2020 12:02:14 -0500 Subject: [PATCH] Overwrite hosts, and only reallocate memory if needed. --- main.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 0d14b51..8fa0c60 100644 --- a/main.c +++ b/main.c @@ -106,25 +106,37 @@ void showHosts() /** * Read the configuration file and give a return code to indicate any changes. - * TODO: this method needs to create/delete hosts. + * TODO: overwrites hosts, but doesn't handle if the list shrinks. * @return 1 if successful, 0 if otherwise. */ int read_config_file() { // increment the refcount for tmp. FILE *config = fopen(CONFIG, "r"); + LinkedList *ptr = hosts; + char buf[1024]; + if (config == NULL) { fprintf(stderr, "Error trying to open config file: %s\n", strerror(errno)); } - 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. fprintf(stderr, "Read %s from config file\n", buf); - linkedlist_add(&hosts, buf); + + // overwrite data in these nodes. + // TODO - this could be more efficient + if (ptr != NULL && strcmp(buf, ptr->data) != 0) { + free(ptr->data); + ptr->data = strdup(buf); + ptr = ptr->next; + } else { + linkedlist_add(&hosts, buf); + } } } }