-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.c
More file actions
404 lines (334 loc) · 14.7 KB
/
users.c
File metadata and controls
404 lines (334 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// File: users.c
// Description: This file contains functions to manage user accounts in a library system, including creating, modifying, deleting users, and logging in.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // For isdigit
#include "library.h"
#define USERNAME_MAX_LEN 50
#define PASSWORD_MAX_LEN 256
#define ROLE_MAX_LEN 20
#define FACULTY_MAX_LEN 50
#define NAME_MAX_LEN 50
// Structure to hold user information
typedef struct {
int id;
char first_name[NAME_MAX_LEN];
char last_name[NAME_MAX_LEN];
int age;
char role[ROLE_MAX_LEN];
char faculty[FACULTY_MAX_LEN];
char username[USERNAME_MAX_LEN];
char password[PASSWORD_MAX_LEN];
} USER;
// Function to create a new user
void createuser() {
USER new_user;
FILE *users = fopen("users.csv", "a");
if (users == NULL) {
printf("Error opening file.\n");
return;
}
// User ID
printf("Enter the user's ID: ");
scanf("%d", &new_user.id);
// Check if ID is valid (integers only)
if (new_user.id <= 0) {
printf("Invalid ID. Must be a positive integer.\n");
fclose(users);
return;
}
getchar(); // clear newline
// First name
printf("Enter the user's first name: ");
fgets(new_user.first_name, sizeof(new_user.first_name), stdin);
new_user.first_name[strcspn(new_user.first_name, "\n")] = 0;
// Last name
printf("Enter the user's last name: ");
fgets(new_user.last_name, sizeof(new_user.last_name), stdin);
new_user.last_name[strcspn(new_user.last_name, "\n")] = 0;
// User age
printf("Enter the user's age: ");
scanf("%d", &new_user.age);
if (new_user.age <= 0) {
printf("Invalid age. Must be a positive integer.\n");
fclose(users);
return;
}
getchar(); // clear newline
// User role
printf("Enter the user's role: ");
fgets(new_user.role, sizeof(new_user.role), stdin);
new_user.role[strcspn(new_user.role, "\n")] = 0;
// User faculty
printf("Enter the user's faculty: ");
fgets(new_user.faculty, sizeof(new_user.faculty), stdin);
new_user.faculty[strcspn(new_user.faculty, "\n")] = 0;
// User username
printf("Enter the user's username: ");
fgets(new_user.username, sizeof(new_user.username), stdin);
new_user.username[strcspn(new_user.username, "\n")] = 0;
// User password
printf("Enter the user's password: ");
fgets(new_user.password, sizeof(new_user.password), stdin);
new_user.password[strcspn(new_user.password, "\n")] = 0;
// Writing data to users.csv
fprintf(users, "\n%d,%s,%s,%d,%s,%s,%s,%s\n",
new_user.id, new_user.first_name, new_user.last_name, new_user.age,
new_user.role, new_user.faculty, new_user.username,
new_user.password);
fclose(users);
// Success Message
printf("User '%s' created successfully.\n", new_user.username);
}
// Function to log in a user
int login(const char *username, const char *password) {
FILE *users = fopen("users.csv", "r");
if (users == NULL) {
printf("No user database found.\n");
return 0; // Login failed
}
printf("Successfully opened users.csv\n");
USER user;
int logged_in = 0;
rewind(users);
char header_line[256];
fgets(header_line, sizeof(header_line), users); // Read and discard the header line
// Read user data from the file
while (fscanf(users, "%d,%49[^,],%49[^,],%d,%19[^,],%49[^,],%49[^,],%255[^\n]\n",
&user.id, user.first_name, user.last_name, &user.age,
user.role, user.faculty, user.username, user.password) == 8) {
// Trim trailing newline characters from username and password
user.username[strcspn(user.username, "\n")] = 0;
user.password[strcspn(user.password, "\n")] = 0;
// Check for username
//printf("Input username: [%s], Input password: [%s]\n", username, password);
//printf("User username: [%s], User password: [%s]\n", user.username, user.password);
if (strcmp(user.username, username) == 0) {
// Check for password
if (strcmp(password, user.password) == 0) {
logged_in = 1;
break; // Exit loop if login is successful
}
}
}
fclose(users);
if (logged_in == 1) {
printf("Login successful!\n");
return user.id;
} else {
printf("Login failed. Invalid username or password.\n");
return logged_in;
}
}
// Function to change a user's password
void changepassword(int user_id, const char *new_password) {
FILE *users = fopen("users.csv", "r"); // Open the user file for reading
FILE *temp = fopen("temp.csv", "w"); // Open a temporary file for writing
if (users == NULL || temp == NULL) {
printf("Error opening files.\n"); // Handle file opening errors
return;
}
USER user;
int found = 0;
char line[512];
char old_password[PASSWORD_MAX_LEN]; // Buffer for the old password
// Copy the header from the original to the temp file
fgets(line, sizeof(line), users);
fprintf(temp, "%s", line);
// Read user data, change password if ID matches
while (fscanf(users, "%d,%49[^,],%49[^,],%d,%19[^,],%49[^,],%49[^,],%255[^\n]\n",
&user.id, user.first_name, user.last_name, &user.age,
user.role, user.faculty, user.username, user.password) == 8) {
if (user.id == user_id) {
found = 1; // Mark that the user ID was found
// Get old password and verify
printf("Enter your old password: ");
fgets(old_password, sizeof(old_password), stdin);
old_password[strcspn(old_password, "\n")] = 0;
// Verify the old password
if (strcmp(old_password, user.password) != 0) {
printf("Incorrect old password.\n");
fclose(users);
fclose(temp);
remove("temp.csv");
return;
}
// Store the new password directly
strcpy(user.password, new_password);
fprintf(temp, "%d,%s,%s,%d,%s,%s,%s,%s\n",
user.id, user.first_name, user.last_name, user.age,
user.role, user.faculty, user.username, user.password); // Write the modified user data to the temp file with the plain text password
} else {
// Keep the original record
fprintf(temp, "%d,%s,%s,%d,%s,%s,%s,%s\n",
user.id, user.first_name, user.last_name, user.age,
user.role, user.faculty, user.username, user.password); // Write the original user data to the temp file
}
}
fclose(users); // Close the original user file
fclose(temp); // Close the temporary file
// Replace original file with the temporary file
remove("users.csv");
rename("temp.csv", "users.csv");
if (found) {
printf("Password changed successfully.\n"); // Success message
} else {
printf("User ID not found.\n"); // Error message
}
}
// Function to list all users
void listuser() {
FILE *users = fopen("users.csv", "r"); // Open the user file for reading
if (users == NULL) {
printf("No user database found.\n"); // Error message if file cannot be opened
return;
}
USER user; // Assuming USER structure does not now include username and password
char line[512];
fgets(line, sizeof(line), users); // Skip the header line
printf("\nUsers:\n");
printf("%-5s %-20s %-20s %-5s %-10s %-30s\n", "ID", "First Name", "Last Name", "Age", "Role", "Faculty");
printf("------------------------------------------------------------------------------------------\n");
// Read each line and parse user information
while (fgets(line, sizeof(line), users) != NULL) {
// Parse user data, ignoring username and password
if (sscanf(line, "%d,%49[^,],%49[^,],%d,%19[^,],%49[^,]",
&user.id, user.first_name, user.last_name,
&user.age, user.role, user.faculty) == 6) { // Expecting 6 fields
printf("%-5d %-20s %-20s %-5d %-10s %-30s\n",
user.id, user.first_name, user.last_name, user.age, user.role, user.faculty);
} else {
printf("Error reading or parsing line: %s\n", line); // More specific error message
}
}
fclose(users); // Close the user file
}
// Function to modify user information
void modifyuser() {
int target_id;
FILE *users = fopen("users.csv", "r"); // Open user file for reading
FILE *temp = fopen("temp.csv", "w"); // Open temp file for writing
if (users == NULL || temp == NULL) {
printf("Error opening files.\n"); // Error message
return;
}
printf("Enter the ID of the user to modify: ");
scanf("%d", &target_id); // Get the ID of the user to modify
getchar(); // Consume the newline character
USER user; // Assuming USER structure is already defined
int found = 0;
char line[512];
fgets(line, sizeof(line), users); // Read the header line
fprintf(temp, "%s", line); // Write the header line to the temp file
// Read and modify user information based on input
while (fscanf(users, "%d,%49[^,],%49[^,],%d,%19[^,],%49[^,],%49[^,],%255[^\n]\n",
&user.id, user.first_name, user.last_name, &user.age, user.role, user.faculty,
user.username, user.password) == 8) { // Adjusted for 8 fields
if (user.id == target_id) {
found = 1; // Mark that the user was found
printf("Modifying user %d. Leave fields blank to keep current values.\n", target_id);
char input[200];
// First Name
printf("Enter new first name [%s]: ", user.first_name);
fgets(input, sizeof(input), stdin);
if (input[0] != '\n') {
input[strcspn(input, "\n")] = 0;
strcpy(user.first_name, input); // Update first name
}
// Last Name
printf("Enter new last name [%s]: ", user.last_name);
fgets(input, sizeof(input), stdin);
if (input[0] != '\n') {
input[strcspn(input, "\n")] = 0;
strcpy(user.last_name, input); // Update last name
}
// Age
printf("Enter new age [%d]: ", user.age);
fgets(input, sizeof(input), stdin);
if (input[0] != '\n') {
user.age = atoi(input); // Update age
}
// Role
printf("Enter new role [%s]: ", user.role);
fgets(input, sizeof(input), stdin);
if (input[0] != '\n') {
input[strcspn(input, "\n")] = 0;
strcpy(user.role, input); // Update role
}
// Faculty
printf("Enter new faculty [%s]: ", user.faculty);
fgets(input, sizeof(input), stdin);
if (input[0] != '\n') {
input[strcspn(input, "\n")] = 0;
strcpy(user.faculty, input); // Update faculty
}
// Username (keep the current username if left blank)
printf("Enter new username [%s]: ", user.username);
fgets(input, sizeof(input), stdin);
if (input[0] != '\n') {
input[strcspn(input, "\n")] = 0;
strcpy(user.username, input); // Update username
}
}
// Write the user data to the temp file, including the username and password
fprintf(temp, "%d,%s,%s,%d,%s,%s,%s,%s\n", user.id, user.first_name, user.last_name, user.age,
user.role, user.faculty, user.username, user.password);
}
fclose(users); // Close user file
fclose(temp); // Close temp file
remove("users.csv"); // Delete original user file
rename("temp.csv", "users.csv"); // Rename temp file to user file
if (found) {
printf("User %d modified successfully.\n", target_id); // Success message
} else {
printf("User %d not found.\n", target_id); // Error message
}
}
void deleteuser() {
int target_id;
FILE *users_file = fopen("users.csv", "r");
FILE *temp_file = fopen("temp.csv", "w");
if (users_file == NULL || temp_file == NULL) {
printf("Error opening files.\n");
return;
}
printf("Enter the ID of the user to delete: ");
scanf("%d", &target_id);
getchar(); // Consume the newline character
USER user;
int found = 0;
char line[512]; // Increased buffer size to handle potentially long lines
// Copy the header line from the original file to the temporary file
fgets(line, sizeof(line), users_file);
fprintf(temp_file, "%s", line);
// Iterate through the users in the original file
while (fgets(line, sizeof(line), users_file) != NULL) {
// Parse the line using sscanf. Added check for number of arguments.
if (sscanf(line, "%d,%49[^,],%49[^,],%d,%19[^,],%49[^,],%49[^,],%255[^\n]",
&user.id, user.first_name, user.last_name, &user.age, user.role,
user.faculty, user.username, user.password) == 8) {
if (user.id == target_id) {
found = 1;
printf("User \"%s\" deleted.\n", user.username);
// Don't write the user to the temp file
} else {
// Write the user to the temp file
fprintf(temp_file, "%d,%s,%s,%d,%s,%s,%s,%s\n",
user.id, user.first_name, user.last_name, user.age, user.role,
user.faculty, user.username, user.password);
}
} else {
// Handle the case where a line in the CSV file is malformed.
printf("Skipping malformed line: %s", line);
}
}
fclose(users_file);
fclose(temp_file);
// Replace the original file with the temporary file
remove("users.csv");
rename("temp.csv", "users.csv");
if (!found) {
printf("User ID not found.\n");
}
}