From 7270dcb04a0cff4189e7f43cf7415fc8238eda37 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 15 Jun 2025 10:17:56 +0200 Subject: Tidy --- bcrypt-test.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/bcrypt-test.c b/bcrypt-test.c index 19bd85e..3bebda3 100644 --- a/bcrypt-test.c +++ b/bcrypt-test.c @@ -35,7 +35,7 @@ create_user(const char *username, const char *password) // NOLINT } } - FILE *database = fopen(USER_DATABASE, "a"); + FILE *database = fopen(USER_DATABASE, "at"); if ( database == NULL ) { return false; } @@ -63,7 +63,7 @@ create_user(const char *username, const char *password) // NOLINT bool check_user(const char *const username, const char *const password) // NOLINT { - FILE *database = fopen(USER_DATABASE, "r"); + FILE *database = fopen(USER_DATABASE, "rt"); if ( database == NULL ) { return false; } @@ -71,8 +71,8 @@ check_user(const char *const username, const char *const password) // NOLINT const size_t username_length = strlen(username); char line[MAX_LINE_LENGTH]; - bool found = false; - while ( !found && fgets(line, sizeof line, database) ) { + bool user_authenticated = false; + while ( !user_authenticated && fgets(line, sizeof line, database) != NULL ) { line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) if ( strncmp(line, username, username_length) != 0 ) { @@ -87,13 +87,12 @@ check_user(const char *const username, const char *const password) // NOLINT continue; } - // User successfully authenticated! - found = true; + user_authenticated = true; } (void) fclose(database); - return found; + return user_authenticated; } /** @@ -106,7 +105,7 @@ check_user(const char *const username, const char *const password) // NOLINT bool check_user_if_exists(const char *username) { - FILE *database = fopen(USER_DATABASE, "r"); + FILE *database = fopen(USER_DATABASE, "rt"); if ( database == NULL ) { return false; } @@ -114,25 +113,24 @@ check_user_if_exists(const char *username) const size_t username_length = strlen(username); char line[MAX_LINE_LENGTH]; - bool found = false; - while ( !found && fgets(line, sizeof line, database) ) { + bool user_found = false; + while ( !user_found && fgets(line, sizeof line, database) != NULL ) { line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) if ( strncmp(line, username, username_length) != 0 ) { continue; } - if ( line[username_length] != '|' ) { + if ( line[username_length] != SEP ) { continue; } - // User successfully authenticated! - found = true; + user_found = true; } (void) fclose(database); - return found; + return user_found; } int -- cgit v1.3