Added credentials parser

This commit is contained in:
ayaryshev 2023-08-02 17:44:10 +03:00
parent 50ce14b9bb
commit 3085b95473
2 changed files with 66 additions and 0 deletions

53
src/creds_parser.c Normal file
View File

@ -0,0 +1,53 @@
#include "creds_parser.h"
#include <stdio.h>
#include <stdlib.h>
int
parse_creds(creds_t * creds,
char const * creds_file)
{
FILE * stream;
size_t nread = 0;
creds->username = NULL;
creds->password = NULL;
stream = fopen(creds_file, "r");
if (stream == NULL)
{
// TODO: Error macro?
return 1;
}
if (getline(&(creds->username), &nread, stream) < 1) {
// Cannot get username
// TODO: error macro?
goto fail;
}
if (getline(&(creds->password), &nread, stream) < 1) {
// Cannot get password
// TODO: error macro?
goto fail;
}
fclose(stream);
return 0;
// Releasing everything in cause of a failure
fail:
fclose(stream);
clean_creds(creds);
return 1;
}
void
clean_creds(creds_t * creds)
{
free(creds->username);
creds->username = NULL;
free(creds->password);
creds->password = NULL;
}

13
src/creds_parser.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef CREDS_PARSER_H
#define CREDS_PARSER_H
typedef struct
{
char * username;
char * password;
} creds_t;
int parse_creds(creds_t * creds, char const * creds_file);
void clean_creds(creds_t * creds);
#endif