Reverting ex17 to a state that should be pretty close to the book source ... still need to check

This commit is contained in:
Dan Buch 2012-03-02 22:14:13 -05:00
parent 40a718203c
commit b70415e2c0

110
ex17.c
View File

@ -4,21 +4,18 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#define DEFAULT_MAX_DATA 512 #define MAX_DATA 512
#define DEFAULT_MAX_ROWS 100 #define MAX_ROWS 100
struct Address { struct Address {
int id; int id;
int set; int set;
char *name; char name[MAX_DATA];
char *email; char email[MAX_DATA];
}; };
struct Database { struct Database {
size_t size; struct Address rows[MAX_ROWS];
int max_data;
int max_rows;
struct Address *rows;
}; };
struct Connection { struct Connection {
@ -26,9 +23,7 @@ struct Connection {
struct Database *db; struct Database *db;
}; };
void Database_close(struct Connection *conn); void die(const char *message)
void die(const char *message, struct Connection *conn)
{ {
if(errno) { if(errno) {
perror(message); perror(message);
@ -36,8 +31,6 @@ void die(const char *message, struct Connection *conn)
printf("ERROR: %s\n", message); printf("ERROR: %s\n", message);
} }
Database_close(conn);
exit(1); exit(1);
} }
@ -49,30 +42,17 @@ void Address_print(struct Address *addr)
void Database_load(struct Connection *conn) void Database_load(struct Connection *conn)
{ {
rewind(conn->file); int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to load database.");
int rc = fread(&conn->db->max_rows, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to load database max_rows.", conn);
rc = fread(&conn->db->max_data, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to load database max_data.", conn);
rc = fread(conn->db->rows, sizeof(struct Address), conn->db->max_rows, conn->file);
if(rc != 1) die("Failed to load database rows.", conn);
} }
struct Connection* Database_open(const char *filename, char mode, int max_rows) struct Connection* Database_open(const char *filename, char mode)
{ {
struct Connection *conn = malloc(sizeof(struct Connection)); struct Connection *conn = malloc(sizeof(struct Connection));
if(!conn) die("Memory error", conn); if(!conn) die("Memory error");
conn->db = malloc(sizeof(struct Database)); conn->db = malloc(sizeof(struct Database));
if(!conn->db) die("Memory error", conn); if(!conn->db) die("Memory error");
size_t rowsize = max_rows * sizeof(struct Address);
conn->db->size = rowsize;
conn->db->rows = malloc(rowsize);
if(!conn->db->rows) die("Memory error", conn);
if(mode == 'c') { if(mode == 'c') {
conn->file = fopen(filename, "w"); conn->file = fopen(filename, "w");
@ -84,14 +64,14 @@ struct Connection* Database_open(const char *filename, char mode, int max_rows)
} }
} }
if(!conn->file) die("Failed to open the file", conn); if(!conn->file) die("Failed to open the file");
return conn; return conn;
} }
void Database_close(struct Connection *conn) void Database_close(struct Connection *conn)
{ {
if(conn != NULL) { if(conn) {
if(conn->file) fclose(conn->file); if(conn->file) fclose(conn->file);
if(conn->db) free(conn->db); if(conn->db) free(conn->db);
free(conn); free(conn);
@ -102,29 +82,18 @@ void Database_write(struct Connection *conn)
{ {
rewind(conn->file); rewind(conn->file);
int rc = fwrite(&conn->db->size, sizeof(size_t), 1, conn->file); int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to write database size.", conn); if(rc != 1) die("Failed to write database.");
rc = fwrite(&conn->db->max_rows, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to write database max_rows.", conn);
rc = fwrite(&conn->db->max_data, sizeof(int), 1, conn->file);
if(rc != 1) die("Failed to write database max_data.", conn);
rc = fwrite(conn->db->rows, conn->db->size, 1, conn->file);
if(rc != 1) die("Failed to write database rows.", conn);
rc = fflush(conn->file); rc = fflush(conn->file);
if(rc == -1) die("Cannot flush database.", conn); if(rc == -1) die("Cannot flush database.");
} }
void Database_create(struct Connection *conn, int max_rows, int max_data) void Database_create(struct Connection *conn)
{ {
int i = 0; int i = 0;
conn->db->max_rows = max_rows;
conn->db->max_data = max_data;
for(i = 0; i < conn->db->max_rows; i++) { for(i = 0; i < 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
@ -134,18 +103,17 @@ void Database_create(struct Connection *conn, int max_rows, int max_data)
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");
addr->set = 1; addr->set = 1;
char *res = strncpy(addr->name, name, max_data); // WARNING: bug, read the "How To Break It" and fix this
if(!res) die("Name copy failed", conn); char *res = strncpy(addr->name, name, MAX_DATA);
addr->name[max_data - 1] = '\0'; // demonstrate the strncpy bug
if(!res) die("Name copy failed");
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");
addr->email[max_data - 1] = '\0';
} }
void Database_get(struct Connection *conn, int id) void Database_get(struct Connection *conn, int id)
@ -155,7 +123,7 @@ void Database_get(struct Connection *conn, int id)
if(addr->set) { if(addr->set) {
Address_print(addr); Address_print(addr);
} else { } else {
die("ID is not set", conn); die("ID is not set");
} }
} }
@ -170,7 +138,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 < conn->db->max_rows; i++) { for(i = 0; i < MAX_ROWS; i++) {
struct Address *cur = &db->rows[i]; struct Address *cur = &db->rows[i];
if(cur->set) { if(cur->set) {
@ -181,45 +149,37 @@ 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>[:max_data,max_rows] [action params]", NULL); if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");
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]) { struct Connection *conn = Database_open(filename, action);
sscanf(argv[2], "%c:%d,%d", &action, &max_data, &max_rows);
}
struct Connection *conn = Database_open(filename, action, max_rows);
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.");
switch(action) { switch(action) {
case 'c': case 'c':
Database_create(conn, max_rows, max_data); Database_create(conn);
Database_write(conn); Database_write(conn);
break; break;
case 'g': case 'g':
if(argc != 4) die("Need an id to get", conn); if(argc != 4) die("Need an id to get");
Database_get(conn, id); Database_get(conn, id);
break; break;
case 's': case 's':
if(argc != 6) die("Need id, name, email to set", conn); if(argc != 6) die("Need id, name, email to set");
Database_set(conn, id, argv[4], argv[5]); Database_set(conn, id, argv[4], argv[5]);
Database_write(conn); Database_write(conn);
break; break;
case 'd': case 'd':
if(argc != 4) die("Need id to delete", conn); if(argc != 4) die("Need id to delete");
Database_delete(conn, id); Database_delete(conn, id);
Database_write(conn); Database_write(conn);
@ -229,7 +189,7 @@ int main(int argc, char *argv[])
Database_list(conn); Database_list(conn);
break; break;
default: default:
die("Invalid action, only: c=create, g=get, s=set, d=del, l=list", conn); die("Invalid action, only: c=create, g=get, s=set, d=del, l=list");
} }
Database_close(conn); Database_close(conn);