admin list parsing base

This commit is contained in:
fall-leaf 2023-08-03 14:57:04 +03:00
parent 29b96f58e4
commit 0eaa8afa93

View File

@ -27,6 +27,7 @@
#define PARAMS_COUNT 5 #define PARAMS_COUNT 5
creds_t creds = {0}; creds_t creds = {0};
char ** admins = NULL;
enum cred_param_ids_e enum cred_param_ids_e
{ {
@ -177,7 +178,6 @@ fail:
return 1; return 1;
} }
void void
clean_creds(void) clean_creds(void)
{ {
@ -189,3 +189,58 @@ clean_creds(void)
FULL_FREE(creds.server); FULL_FREE(creds.server);
#undef FULL_FREE #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;
}