BUSTED while trying to do extra credit #2

This commit is contained in:
Dan Buch 2011-09-23 22:54:52 -04:00
parent c5a1f90200
commit ab98180af0

43
ex17.c
View File

@ -4,18 +4,20 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#define MAX_DATA 512 #define DEFAULT_MAX_DATA 512
#define MAX_ROWS 100 #define DEFAULT_MAX_ROWS 100
struct Address { struct Address {
int id; int id;
int set; int set;
char name[MAX_DATA]; char *name;
char email[MAX_DATA]; char *email;
}; };
struct Database { struct Database {
struct Address rows[MAX_ROWS]; int max_data;
int max_rows;
struct Address rows[];
}; };
struct Connection { struct Connection {
@ -93,11 +95,13 @@ void Database_write(struct Connection *conn)
if(rc == -1) die("Cannot flush database.", conn); if(rc == -1) die("Cannot flush database.", conn);
} }
void Database_create(struct Connection *conn) void Database_create(struct Connection *conn, int max_rows, int max_data)
{ {
int i = 0; int i = 0;
conn->db->max_rows = max_rows;
conn->db->max_data = max_data;
for(i = 0; i < MAX_ROWS; i++) { for(i = 0; i < conn->db->max_rows; i++) {
// make a prototype to initialize it // make a prototype to initialize it
struct Address addr = {.id = i, .set = 0}; struct Address addr = {.id = i, .set = 0};
// then just assign it // then just assign it
@ -107,17 +111,18 @@ void Database_create(struct Connection *conn)
void Database_set(struct Connection *conn, int id, const char *name, const char *email) void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{ {
int max_data = conn->db->max_data;
struct Address *addr = &conn->db->rows[id]; struct Address *addr = &conn->db->rows[id];
if(addr->set) die("Already set, delete it first", conn); if(addr->set) die("Already set, delete it first", conn);
addr->set = 1; addr->set = 1;
char *res = strncpy(addr->name, name, MAX_DATA); char *res = strncpy(addr->name, name, max_data);
if(!res) die("Name copy failed", conn); if(!res) die("Name copy failed", conn);
addr->name[MAX_DATA - 1] = '\0'; addr->name[max_data - 1] = '\0';
res = strncpy(addr->email, email, MAX_DATA); res = strncpy(addr->email, email, max_data);
if(!res) die("Email copy failed", conn); if(!res) die("Email copy failed", conn);
addr->email[MAX_DATA - 1] = '\0'; addr->email[max_data - 1] = '\0';
} }
void Database_get(struct Connection *conn, int id) void Database_get(struct Connection *conn, int id)
@ -142,7 +147,7 @@ void Database_list(struct Connection *conn)
int i = 0; int i = 0;
struct Database *db = conn->db; struct Database *db = conn->db;
for(i = 0; i < MAX_ROWS; i++) { for(i = 0; i < conn->db->max_rows; i++) {
struct Address *cur = &db->rows[i]; struct Address *cur = &db->rows[i];
if(cur->set) { if(cur->set) {
@ -153,19 +158,27 @@ void Database_list(struct Connection *conn)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]", NULL); if(argc < 3) die("USAGE: ex17 <dbfile> <action>[:max_data,max_rows] [action params]", NULL);
char *filename = argv[1]; char *filename = argv[1];
int max_data = DEFAULT_MAX_DATA;
int max_rows = DEFAULT_MAX_ROWS;
char action = argv[2][0]; char action = argv[2][0];
if(':' == argv[2][1]) {
sscanf(argv[2], "%c:%d,%d", &action, &max_data, &max_rows);
}
struct Connection *conn = Database_open(filename, action); struct Connection *conn = Database_open(filename, action);
int id = 0; int id = 0;
if(argc > 3) id = atoi(argv[3]); if(argc > 3) id = atoi(argv[3]);
if(id >= MAX_ROWS) die("There's not that many records.", conn); if(id >= max_rows) die("There's not that many records.", conn);
switch(action) { switch(action) {
case 'c': case 'c':
Database_create(conn); Database_create(conn, max_rows, max_data);
Database_write(conn); Database_write(conn);
break; break;