summaryrefslogtreecommitdiff
path: root/bcrypt-test.c
diff options
context:
space:
mode:
Diffstat (limited to 'bcrypt-test.c')
-rw-r--r--bcrypt-test.c26
1 files 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
35 } 35 }
36 } 36 }
37 37
38 FILE *database = fopen(USER_DATABASE, "a"); 38 FILE *database = fopen(USER_DATABASE, "at");
39 if ( database == NULL ) { 39 if ( database == NULL ) {
40 return false; 40 return false;
41 } 41 }
@@ -63,7 +63,7 @@ create_user(const char *username, const char *password) // NOLINT
63bool 63bool
64check_user(const char *const username, const char *const password) // NOLINT 64check_user(const char *const username, const char *const password) // NOLINT
65{ 65{
66 FILE *database = fopen(USER_DATABASE, "r"); 66 FILE *database = fopen(USER_DATABASE, "rt");
67 if ( database == NULL ) { 67 if ( database == NULL ) {
68 return false; 68 return false;
69 } 69 }
@@ -71,8 +71,8 @@ check_user(const char *const username, const char *const password) // NOLINT
71 const size_t username_length = strlen(username); 71 const size_t username_length = strlen(username);
72 char line[MAX_LINE_LENGTH]; 72 char line[MAX_LINE_LENGTH];
73 73
74 bool found = false; 74 bool user_authenticated = false;
75 while ( !found && fgets(line, sizeof line, database) ) { 75 while ( !user_authenticated && fgets(line, sizeof line, database) != NULL ) {
76 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) 76 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists)
77 77
78 if ( strncmp(line, username, username_length) != 0 ) { 78 if ( strncmp(line, username, username_length) != 0 ) {
@@ -87,13 +87,12 @@ check_user(const char *const username, const char *const password) // NOLINT
87 continue; 87 continue;
88 } 88 }
89 89
90 // User successfully authenticated! 90 user_authenticated = true;
91 found = true;
92 } 91 }
93 92
94 (void) fclose(database); 93 (void) fclose(database);
95 94
96 return found; 95 return user_authenticated;
97} 96}
98 97
99/** 98/**
@@ -106,7 +105,7 @@ check_user(const char *const username, const char *const password) // NOLINT
106bool 105bool
107check_user_if_exists(const char *username) 106check_user_if_exists(const char *username)
108{ 107{
109 FILE *database = fopen(USER_DATABASE, "r"); 108 FILE *database = fopen(USER_DATABASE, "rt");
110 if ( database == NULL ) { 109 if ( database == NULL ) {
111 return false; 110 return false;
112 } 111 }
@@ -114,25 +113,24 @@ check_user_if_exists(const char *username)
114 const size_t username_length = strlen(username); 113 const size_t username_length = strlen(username);
115 char line[MAX_LINE_LENGTH]; 114 char line[MAX_LINE_LENGTH];
116 115
117 bool found = false; 116 bool user_found = false;
118 while ( !found && fgets(line, sizeof line, database) ) { 117 while ( !user_found && fgets(line, sizeof line, database) != NULL ) {
119 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists) 118 line[strcspn(line, "\r\n")] = '\0'; // remove newline (if exists)
120 119
121 if ( strncmp(line, username, username_length) != 0 ) { 120 if ( strncmp(line, username, username_length) != 0 ) {
122 continue; 121 continue;
123 } 122 }
124 123
125 if ( line[username_length] != '|' ) { 124 if ( line[username_length] != SEP ) {
126 continue; 125 continue;
127 } 126 }
128 127
129 // User successfully authenticated! 128 user_found = true;
130 found = true;
131 } 129 }
132 130
133 (void) fclose(database); 131 (void) fclose(database);
134 132
135 return found; 133 return user_found;
136} 134}
137 135
138int 136int