This repository has been archived on 2024-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
probotic/src/creds_parser.c

53 lines
957 B
C
Raw Normal View History

2023-08-02 10:44:10 -04:00
#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;
}