-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadminCreator.c
202 lines (189 loc) · 6.52 KB
/
adminCreator.c
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
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "db/db.h"
void create_db() {
const char *db_files[] = {"db/admins.db", "db/otdata.db", "db/employees.db", "db/customers.db", "db/feedbacks.db", "db/loans.db", "db/transactions.db"};
size_t num_files = sizeof(db_files) / sizeof(db_files[0]);
for (size_t i = 0; i < num_files; ++i) {
int fd = open(db_files[i], O_CREAT | O_RDWR | O_APPEND, 0666);
if (fd == -1) {
printf("Failed to create database: %s\n", db_files[i]);
} else {
printf("Created %s\n", db_files[i]);
close(fd);
}
}
}
void set_otdata() {
const char* file_path="db/otdata.db";
int fd1 = open(file_path, O_CREAT | O_RDWR | O_APPEND, 0777);
OTData otdata;
otdata.highest_admin_id=0;
otdata.highest_employee_id=0;
otdata.highest_customer_id=0;
otdata.highest_feedback_id=0;
otdata.highest_loan_id=0;
otdata.highest_transaction_id=0;
if (write(fd1, &otdata, sizeof(OTData))<0) {
printf("Error writing otdata to file.\n");
} else {
printf("OTData set to 0 successfully!\n");
}
close(fd1);
}
void clear_db() {
const char *db_files[] = {"db/admins.db", "db/employees.db", "db/customers.db", "db/feedbacks.db", "db/loans.db", "db/transactions.db"};
size_t num_files = sizeof(db_files) / sizeof(db_files[0]);
for (size_t i = 0; i < num_files; ++i) {
int fd = open(db_files[i], O_CREAT | O_RDWR | O_TRUNC, 0666);
if (fd == -1) {
printf("Failed to clear database: %s\n", db_files[i]);
} else {
printf("Cleared %s\n", db_files[i]);
close(fd);
}
}
set_otdata();
}
void add_admin() {
Admin admin;
const int fd = open("db/admins.db", O_CREAT | O_RDWR | O_APPEND, 0666);
if (fd == -1) {
perror("Failed to open file db/admins.db");
return;
}
printf("\nEnter Admin Details\n");
getchar();
printf("Enter Name:");
fgets(admin.name, sizeof(admin.name), stdin);
admin.name[strcspn(admin.name, "\n")] = '\0';
printf("Enter Email:");
fgets(admin.email, sizeof(admin.email), stdin);
admin.email[strcspn(admin.email, "\n")] = '\0';
printf("Enter Password:");
fgets(admin.password, sizeof(admin.password), stdin);
admin.password[strcspn(admin.password, "\n")] = '\0';
admin.login_status = 0;
// Write the admin structure to the file
if (write(fd, &admin, sizeof(Admin))<0) {
printf("Error writing admin data to file.\n");
} else {
printf("\nName: %s\n", admin.name);
printf("Email: %s\t", admin.email);
printf("Password: %s\n", admin.password);
printf("Admin added successfully!\n");
}
close(fd);
}
void clear_logins(int choice) {
if (choice==1) {
const char *db_files[] = {"db/admins.db", "db/employees.db", "db/customers.db"};
size_t num_files = sizeof(db_files) / sizeof(db_files[0]);
for (size_t i = 0; i < num_files; ++i) {
int fd = open(db_files[i], O_CREAT | O_RDWR , 0666);
if (fd == -1) {
printf("Failed to open file: %s\n", db_files[i]);
}
else {
if(i==0) {
Admin admin;
while (read(fd, &admin, sizeof(Admin)) > 0) {
admin.login_status = 0;
lseek(fd, -sizeof(Admin), SEEK_CUR);
write(fd, &admin, sizeof(Admin));
}
printf("Cleared logins for %s\n", db_files[i]);
close(fd);
}
if (i==1) {
Employee emp;
while (read(fd, &emp, sizeof(Employee)) > 0) {
emp.login_status = 0;
lseek(fd, -sizeof(Employee), SEEK_CUR);
write(fd, &emp, sizeof(Employee));
}
printf("Cleared logins for %s\n", db_files[i]);
close(fd);
}
if (i==2) {
Customer cust;
while (read(fd, &cust, sizeof(Customer)) > 0) {
cust.login_status = 0;
lseek(fd, -sizeof(Customer), SEEK_CUR);
write(fd, &cust, sizeof(Customer));
}
printf("Cleared logins for %s\n", db_files[i]);
close(fd);
}
}
}
}
else {
printf("\nWhich database to clear logins\n1. Admins\n2. Employees\n3. Customers\n");
int i;
scanf("%d", &i);
i--;
const char *db_files[] = {"db/admins.db", "db/employees.db", "db/customers.db"};
int fd = open(db_files[i], O_CREAT | O_RDWR , 0666);
if(i==0) {
Admin admin;
while (read(fd, &admin, sizeof(Admin)) > 0) {
admin.login_status = 0;
lseek(fd, -sizeof(Admin), SEEK_CUR);
write(fd, &admin, sizeof(Admin));
}
printf("Cleared logins for %s\n", db_files[i]);
close(fd);
}
if (i==1) {
Employee emp;
while (read(fd, &emp, sizeof(Employee)) > 0) {
emp.login_status = 0;
lseek(fd, -sizeof(Employee), SEEK_CUR);
write(fd, &emp, sizeof(Employee));
}
printf("Cleared logins for %s\n", db_files[i]);
close(fd);
}
if (i==2) {
Customer cust;
while (read(fd, &cust, sizeof(Customer)) > 0) {
cust.login_status = 0;
lseek(fd, -sizeof(Customer), SEEK_CUR);
write(fd, &cust, sizeof(Customer));
}
printf("Cleared logins for %s\n", db_files[i]);
close(fd);
}
close(fd);
}
}
int main() {
char c,c1,c2;
printf("\nDo you wish to create all databases? (y/n): ");
scanf(" %c", &c);
if (c == 'y') {
create_db();
}
printf("\nDo you wish to Clear all databases? (y/n): ");
scanf(" %c", &c1);
if (c1 == 'y') {
clear_db();
}
printf("\nDo you wish to Create a New Admin? (y/n): ");
scanf(" %c", &c2);
if (c2 == 'y') {
add_admin();;
}
printf("\nDo you wish to clear all Logins or select specific databse? \n'y' for all / 'n' to select specific: ");
scanf(" %c", &c2);
if (c2 == 'y') {
clear_logins(1);
}
else {
clear_logins(0);
}
return 0;
}