From 3085b954731b8598438a534bdb699cbc0ba6fb46 Mon Sep 17 00:00:00 2001 From: ayaryshev Date: Wed, 2 Aug 2023 17:44:10 +0300 Subject: [PATCH] Added credentials parser --- src/creds_parser.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/creds_parser.h | 13 +++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/creds_parser.c create mode 100644 src/creds_parser.h diff --git a/src/creds_parser.c b/src/creds_parser.c new file mode 100644 index 0000000..3796666 --- /dev/null +++ b/src/creds_parser.c @@ -0,0 +1,53 @@ +#include "creds_parser.h" + +#include +#include + +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; +} \ No newline at end of file diff --git a/src/creds_parser.h b/src/creds_parser.h new file mode 100644 index 0000000..25dbc13 --- /dev/null +++ b/src/creds_parser.h @@ -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 \ No newline at end of file