From 0eaa8afa930e3575ec2b0974eba75f0aea861c00 Mon Sep 17 00:00:00 2001 From: fall-leaf Date: Thu, 3 Aug 2023 14:57:04 +0300 Subject: [PATCH] admin list parsing base --- src/parse.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/parse.c b/src/parse.c index 122060d..85522b3 100644 --- a/src/parse.c +++ b/src/parse.c @@ -27,6 +27,7 @@ #define PARAMS_COUNT 5 creds_t creds = {0}; +char ** admins = NULL; enum cred_param_ids_e { @@ -177,7 +178,6 @@ fail: return 1; } - void clean_creds(void) { @@ -189,3 +189,58 @@ clean_creds(void) FULL_FREE(creds.server); #undef FULL_FREE } + +DECL int +parse_admin_list(char const * admin_list_path) +{ + #define ADMIN_LIST_INIT_SIZE 8 + /* prealloc with 8 */ + size_t current_array_size = ADMIN_LIST_INIT_SIZE; + + FILE * stream; + size_t lines_read = 0; + char * line = NULL; + size_t nread = 0; + + admins = calloc(ADMIN_LIST_INIT_SIZE, sizeof(char *)); + + stream = fopen(admin_list_path, "r"); + if (stream == NULL) + { + PERROR(1); + } + + while (getline(&line, &nread, stream) > 0) + { + if (++lines_read > current_array_size) + { + /* double the space*/ + current_array_size *= 2; + realloc(admins, current_array_size); + } + + admins[lines_read - 1] = line; + } + + /* set up array end marker for proper clean-up later */ + if (lines_read + 1 > current_array_size) + { + realloc(admins, current_array_size + 1); + } + admins[lines_read] = NULL; + + return 0; +} + +DECL void +clean_admin_list() +{ + if (admins == NULL) + { return; } + + for (size_t i = 0; admins[i]; ++i) + { free(admins[i]); } + + free(admins); + admins = NULL; +} \ No newline at end of file