/
ex17b.c
281 lines (219 loc) · 6.54 KB
/
ex17b.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
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
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#define MAX_DATA 512
struct Address {
int id;
int set;
char *name;
char *email;
};
struct Database {
int max_rows;
int max_data;
struct Address *rows;
};
struct Connection {
FILE *file;
struct Database *db;
};
void Database_close(struct Connection *conn);
void die(const char *message, struct Connection *conn)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}
if(conn != NULL) {
Database_close(conn);
}
exit(1);
}
void Address_print(struct Address *addr)
{
printf("%d %s %s \n", addr->id, addr->name, addr->email);
}
void Database_alloc_rows(struct Connection *conn) {
conn->db->rows = malloc(conn->db->max_rows * sizeof(struct Address));
if(!conn->db->rows) die("Failed to allocate rows", conn);
}
void Address_read(struct Connection *conn) {
int i;
for(i = 0; i < conn->db->max_rows; i++) {
int rc = fread(&conn->db->rows[i].id, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to read id", conn);
rc = fread(&conn->db->rows[i].set, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to read set.", conn);
conn->db->rows[i].name = malloc(sizeof(char) * conn->db->max_data);
rc = fread(conn->db->rows[i].name, sizeof(char) * conn->db->max_data, 1, conn->file);
if(rc != 1) die("Failed to read name.", conn);
conn->db->rows[i].email = malloc(sizeof(char) * conn->db->max_data);
rc = fread(conn->db->rows[i].email, sizeof(char) * conn->db->max_data, 1, conn->file);
if(rc != 1) die("Failed to read email.", conn);
}
}
void Database_load(struct Connection *conn)
{
int rc = fread(&conn->db->max_rows, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to load max_rows.", conn);
rc = fread(&conn->db->max_data, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to load max_data.", conn);
Database_alloc_rows(conn);
Address_read(conn);
}
void Address_write(struct Connection *conn) {
int i;
for(i = 0; i < conn->db->max_rows; i++) {
int rc = fwrite(&conn->db->rows[i].id, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to write id", conn);
rc = fwrite(&conn->db->rows[i].set, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to write set.", conn);
rc = fwrite(conn->db->rows[i].name, sizeof(char) * conn->db->max_data, 1, conn->file);
if(rc != 1) die("Failed to write name.", conn);
rc = fwrite(conn->db->rows[i].email, sizeof(char) * conn->db->max_data, 1, conn->file);
if(rc != 1) die("Failed to write email.", conn);
}
}
void Database_write(struct Connection *conn)
{
rewind(conn->file);
int rc = fwrite(&(conn->db->max_rows), sizeof(int), 1 ,conn->file);
if(rc != 1) die("Failed to write max_rows.", conn);
rc = fwrite(&(conn->db->max_data), sizeof(int), 1 ,conn->file);
if(rc != 1) die("Failed to write max_data.", conn);
Address_write(conn);
rc = fflush(conn->file);
if(rc == -1) die("Cannot flush database.", conn);
}
void Database_create(struct Connection *conn, int max_rows, int max_data)
{
conn->db->max_rows = max_rows;
conn->db->max_data = max_data;
Database_alloc_rows(conn);
int i = 0;
for(i = 0; i < max_rows; i++) {
struct Address addr = { .id = i, .set = 0};
addr.email = malloc(sizeof(char) * conn->db->max_data);
addr.name = malloc(sizeof(char) * conn->db->max_data);
conn->db->rows[i] = addr;
}
}
struct Connection *Database_open(const char *filename, char mode)
{
struct Connection *conn = malloc(sizeof(struct Connection));
if(!conn) die("Memory error", conn);
conn->db = malloc(sizeof(struct Database));
if(!conn->db) die("Memory error", conn);
if(mode == 'c') {
conn->file = fopen(filename, "w");
} else {
conn->file = fopen(filename, "r+");
if(conn->file) {
Database_load(conn);
}
}
if(!conn->file) die("Failed to open the file", conn);
return conn;
}
void Database_close(struct Connection *conn)
{
if(conn) {
if(conn->file) fclose(conn->file);
if(conn->db->rows) free(conn->db->rows);
if(conn->db) free(conn->db);
free(conn);
}
}
void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{
struct Address *addr = &conn->db->rows[id];
if(addr->set) die("Already set, delete it first", conn);
addr->set = 1;
//WARNING: bug, read the "How to break it" and fix this
char *res = strncpy(addr->name, name, conn->db->max_data);
//demonstrate the strncpy bug
if(!res) die("Name copy failed", conn);
res = strncpy(addr->email, email, conn->db->max_data);
if(!res) die("Email copy failed", conn);
}
void Database_get(struct Connection *conn, int id)
{
if(id > conn->db->max_rows) {
die("There's not that many rows", conn);
}
struct Address *addr = &conn->db->rows[id];
if(addr->set) {
Address_print(addr);
} else {
die("ID not set", conn);
}
}
void Database_delete(struct Connection *conn, int id) {
struct Address addr = {.id = id, .set = 0};
addr.email = malloc(sizeof(char) * conn->db->max_data);
addr.name = malloc(sizeof(char) * conn->db->max_data);
conn->db->rows[id] = addr;
}
void Database_list(struct Connection *conn)
{
int i = 0;
for(i=0; i< conn->db->max_rows; i++) {
struct Address *cur = &(conn->db->rows[i]);
if(cur->set) {
Address_print(cur);
}
}
}
int main(int argc, char *argv[])
{
if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]", NULL);
char *filename = argv[1];
char action = argv[2][0];
struct Connection *conn = Database_open(filename, action);
int param1 = 0;
int param2 = 0;
int max_rows = 100;
int max_data = 512;
if(argc > 3) {
param1 = atoi(argv[3]);
}
if(argc > 4) {
param2 = atoi(argv[4]);
}
switch(action) {
case 'c':
if(argc > 3) {
max_rows = param1;
}
if(argc > 4) {
max_data = param2;
}
Database_create(conn, max_rows, max_data);
Database_write(conn);
break;
case 'g':
if(argc != 4) die("Need an id to get", conn);
Database_get(conn, param1);
break;
case 's':
if(argc != 6) die("Need id, name, email to set", conn);
Database_set(conn, param1, argv[4], argv[5]);
Database_write(conn);
break;
case 'd':
if(argc != 4) die("Need an id to delete", conn);
Database_delete(conn, param1);
Database_write(conn);
break;
case 'l':
Database_list(conn);
break;
default:
die("Invalid action, only: c=create, g-get, s=set, d=del, l=list", conn);
}
Database_close(conn);
return 0;
}