-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.c
153 lines (134 loc) · 4.08 KB
/
setup.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
/*
The is just to setup the initial database.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define LEN 1024 // length of the password
#define ACC 5 // maximum number of ids permitted in a joint account
typedef struct account // this is used to store all the information regarding an account
{
unsigned int this_id;
unsigned int id[ACC];
char password[LEN];
float balance;
} account_t;
#define SIZE sizeof(account_t) // size of the account datastructure
void strreverse(char* begin, char* end) { // function to reverse a string
char aux;
while(end>begin)
aux=*end, *end--=*begin, *begin++=aux;
}
void itoa(int value, char* str, int base) { // function to get the string for an integer
static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char* wstr=str;
int sign;
// Validate base
if (base<2 || base>35){ *wstr='\0'; return; }
// Take care of sign
if ((sign=value) < 0) value = -value;
// Conversion. Number is reversed.
do *wstr++ = num[value%base]; while(value/=base);
if(sign<0) *wstr++='-';
*wstr='\0';
// Reverse string
strreverse(str,wstr-1);
}
int print_id(unsigned int* arr)
{
int counter = 0;
if(*(arr + counter) == 1) // this is the administrator's account
return 1;
printf("Ids attaced to this account are: ");
while(*(arr + counter) != 0)
{
if(counter == 0) printf("%d",*(arr + counter));
else printf(", %d",*(arr + counter));
counter ++;
}
if(counter > 1)
printf("(This is a joint account)\n");
else
printf("(This is a single account)\n");
return 0;
}
void print_account_details(void) // prints all the account details
{
int fd = open("pds.bin", O_RDONLY); /* Open the file for writing */
if (fd == -1) { /* In the case of error, open returns -1 ! */
printf("Error: Database Failed To Open!\n");
exit(1);
}
account_t* pointer = (account_t*)calloc(1, sizeof(SIZE));
if(read(fd, pointer, SIZE) != SIZE)
{
printf("Error: Database Failed To Read!\n");
exit(1);
}
int num_of_accounts = pointer->balance - 1;
for(int i = 0; i < num_of_accounts; i++)
{
read(fd, pointer, SIZE);
printf("++++++++++++++++++++++++++++\n");
if(print_id(pointer->id) == 1)
continue ;
printf("Password: %s\n",pointer->password);
printf("Balance: %f\n", pointer->balance);
printf("++++++++++++++++++++++++++++\n");
}
free(pointer);
close(fd);
}
int main() {
int fd;
account_t admin_account = {0, {0}, "password", 21.}; // the balance field refers to the total number of structs in the database
admin_account.id[0] = 1;
fd = open("pds.bin", O_WRONLY|O_CREAT|O_TRUNC, 0666); /* Open the file for writing */
if (fd == -1) { /* In the case of error, open returns -1 ! */
printf("Error: Database Failed To Open!\n");
exit(1);
}
if(write(fd, &admin_account, SIZE) != SIZE)
{
printf("Error: Failed To Write!\n");
exit(1);
}
printf("Adding 10 dummy singe-user accounts...\n");
account_t* dummy_account = (account_t*)calloc(1, sizeof(account_t));
for(int i = 1; i < 11; i++)
{
itoa(i+1, dummy_account->password, 10);
dummy_account->id[0] = i+1;
for(int j = 1; j < ACC; j++)
dummy_account->id[j] = 0;
dummy_account->balance = 0.0;
if(write(fd, dummy_account, SIZE) != SIZE)
{
printf("Error: Failed To Write!\n");
free(dummy_account);
exit(1);
}
}
printf("Adding 10 dummy joint-user accounts...\n"); // all with just two users
for(int i = 11; i < 30; i = i + 2)
{
itoa(i+1, dummy_account->password, 10);
dummy_account->id[0] = i+1;
dummy_account->id[1] = i+2;
for(int j = 2; j < ACC; j++)
dummy_account->id[j] = 0;
dummy_account->balance = 0.0;
if(write(fd, dummy_account, SIZE) != SIZE)
{
printf("Error: Failed To Write!\n");
free(dummy_account);
exit(1);
}
}
close(fd);
print_account_details();
return 0;
}