From af4051dfbeaaf9f106a18485d6e6f3266464b54d Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Tue, 14 Jan 2020 14:26:12 -0500 Subject: [PATCH] Working read_config_file function --- main.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 2729d25..3553122 100644 --- a/main.c +++ b/main.c @@ -91,7 +91,6 @@ void showHosts() /** * Read the configuration file and give a return code to indicate any changes. - * TODO: overwrites hosts, but doesn't handle if the list shrinks. * @return 1 if successful, 0 if otherwise. */ int read_config_file() { @@ -103,6 +102,8 @@ int read_config_file() { if (config == NULL) { fprintf(stderr, "Error trying to open config file: %s\n", strerror(errno)); + fclose(config); + return 0; } else { // Add each line from the file to the linked list. @@ -110,24 +111,27 @@ int read_config_file() { 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); - // overwrite data in these nodes. - if (ptr != NULL && strcmp(buf, ptr->data) != 0) { + if (ptr != NULL) { if (strlen(ptr->data) < strlen(buf)) { if ((ptr->data = realloc(ptr->data, strlen(buf))) == NULL) { fprintf(stderr, "Failed to realloc pointer\n"); - exit(1); + return 0; } } strcpy(ptr->data, buf); + prev = ptr; ptr = ptr->next; } else { linkedlist_add(&hosts, buf); } } + // In this case, the list got chopped off, we need to reset pointers and free the rest of our list. + if (ptr != NULL) { + prev->next = NULL; + } + // If we didn't exhaust the linked list, then we need to free any remaining nodes because the list // shortened. while (ptr != NULL) { @@ -136,6 +140,8 @@ int read_config_file() { free(prev->data); free(prev); } + fclose(config); + return 1; } }