Created
May 25, 2012 18:50
-
-
Save jbaiter/2789804 to your computer and use it in GitHub Desktop.
Segfaults when run normally, runs fine in Valgrind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <assert.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <string.h> | |
char *names_addr = 0; | |
char *mails_addr = 0; | |
struct Address { | |
int id; | |
int set; | |
char *name; | |
char *email; | |
}; | |
struct Database { | |
int max_data; | |
int max_rows; | |
struct Address *rows; | |
}; | |
struct Connection { | |
FILE *file; | |
struct Database *db; | |
}; | |
void Database_close(struct Connection *conn) | |
{ | |
if(conn) { | |
if(conn->file) fclose(conn->file); | |
if(names_addr) free(names_addr); | |
if(mails_addr) free(mails_addr); | |
if(conn->db->rows) free(conn->db->rows); | |
if(conn->db) free(conn->db); | |
free(conn); | |
} | |
} | |
void die(struct Connection *conn, const char *message) | |
{ | |
if(errno) { | |
perror(message); | |
} else { | |
printf("ERROR: %s\n", message); | |
} | |
Database_close(conn); | |
exit(1); | |
} | |
void Address_print(struct Address *addr) | |
{ | |
printf("%d %s %s\n", | |
addr->id, addr->name, addr->email); | |
} | |
void Database_load(struct Connection *conn) | |
{ | |
int rc = fread(conn->db, sizeof(struct Database), 1, conn->file); | |
if(rc != 1) die(conn, "Failed to load database."); | |
int max_rows = conn->db->max_rows; | |
int max_data = conn->db->max_data; | |
printf("max_rows: %d; max_data: %d\n", max_rows, max_data); | |
conn->db->rows = calloc(max_rows, sizeof(struct Address)); | |
names_addr = calloc(max_rows*max_data, sizeof(char)); | |
mails_addr = calloc(max_rows*max_data, sizeof(char)); | |
rc = fread(conn->db->rows, sizeof(struct Address)*max_rows, | |
1, conn->file); | |
if(rc != 1) die(conn, "Failed to load rows."); | |
rc = fread(names_addr, max_rows*max_data*sizeof(char), | |
1, conn->file); | |
if(rc != 1) die(conn, "Failed to load names."); | |
rc = fread(mails_addr, max_rows*max_data*sizeof(char), | |
1, conn->file); | |
if(rc != 1) die(conn, "Failed to load mails."); | |
int i = 0; | |
for(i = 0; i < conn->db->max_rows; i++) { | |
struct Address addr = conn->db->rows[i]; | |
// then just assign it | |
addr.name = names_addr+(conn->db->max_data*i); | |
addr.email = mails_addr+(conn->db->max_data*i); | |
} | |
} | |
struct Connection* Database_open(const char *filename, char mode) | |
{ | |
struct Connection *conn = calloc(1, sizeof(struct Connection)); | |
if(!conn) die(conn, "Memory error"); | |
conn->db = calloc(1, sizeof(struct Database)); | |
if(!conn->db) die(conn, "Memory error"); | |
if(mode == 'c') { | |
conn->file = fopen(filename, "w"); | |
} else { | |
conn->file = fopen(filename, "r+"); | |
if(conn->file) { | |
Database_load(conn); | |
} | |
} | |
if(!conn->file) die(conn, "Failed to open the file"); | |
return conn; | |
} | |
void Database_write(struct Connection *conn) | |
{ | |
rewind(conn->file); | |
int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file); | |
if(rc != 1) die(conn, "Failed to write database."); | |
rc = fwrite(conn->db->rows, sizeof(struct Address)*conn->db->max_rows, | |
1, conn->file); | |
if(rc != 1) die(conn, "Failed to write rows."); | |
rc = fwrite(names_addr, sizeof(char)*conn->db->max_rows*conn->db->max_data, | |
1, conn->file); | |
if(rc != 1) die(conn, "Failed to write names."); | |
rc = fwrite(mails_addr, sizeof(char)*conn->db->max_rows*conn->db->max_data, | |
1, conn->file); | |
if(rc != 1) die(conn, "Failed to write mails."); | |
rc = fflush(conn->file); | |
if(rc == -1) die(conn, "Cannot flush database."); | |
} | |
void Database_create(int max_data, int max_rows, struct Connection *conn) | |
{ | |
int i = 0; | |
conn->db->max_rows = max_rows; | |
conn->db->max_data = max_data; | |
conn->db->rows = calloc(max_rows, sizeof(struct Address)); | |
names_addr = calloc(max_rows*max_data, sizeof(char)); | |
mails_addr = calloc(max_rows*max_data, sizeof(char)); | |
for(i = 0; i < max_rows; i++) { | |
// make a prototype to initialize it | |
struct Address addr = {.id = i, .set = 0}; | |
// then just assign it | |
addr.name = names_addr+(max_data*i); | |
addr.email = mails_addr+(max_data*i); | |
conn->db->rows[i] = addr; | |
} | |
} | |
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(conn, "Already set, delete it first"); | |
addr->set = 1; | |
char *res = strncpy(addr->name, name, conn->db->max_data); | |
// Work around design flaw in strncpy when the data is longer | |
// maximum number of bytes | |
addr->name[conn->db->max_data-1] = '\0'; | |
if(!res) die(conn, "Name copy failed"); | |
res = strncpy(addr->email, email, conn->db->max_data); | |
addr->email[conn->db->max_data-1] = '\0'; | |
if(!res) die(conn, "Email copy failed"); | |
printf("Successfully set record %d(%d) with contents %s:%s\n", | |
addr->id, addr->set, addr->name, addr->email); | |
} | |
void Database_get(struct Connection *conn, int id) | |
{ | |
struct Address *addr = &conn->db->rows[id]; | |
if(addr->set) { | |
Address_print(addr); | |
} else { | |
die(conn, "ID is not set"); | |
} | |
} | |
void Database_delete(struct Connection *conn, int id) | |
{ | |
struct Address addr = {.id = id, .set = 0}; | |
conn->db->rows[id] = addr; | |
printf("Row %d is set: %d\n", id, conn->db->rows[id].set); | |
} | |
void Database_list(struct Connection *conn) | |
{ | |
int i = 0; | |
struct Database *db = conn->db; | |
for(i=0; i < db->max_rows; i++) { | |
struct Address *cur = &db->rows[i]; | |
if(cur->set) { | |
Address_print(cur); | |
printf("Row %d is set: %d\n", cur->id, cur->set); | |
} | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if(argc < 3) die(NULL, "USAGE: ex17 <dbfile> <action> [action params]"); | |
char *filename = argv[1]; | |
char action = argv[2][0]; | |
struct Connection *conn = Database_open(filename, action); | |
int id = 0; | |
if(argc > 3 && action != 'c') { | |
id = atoi(argv[3]); | |
if(id >= conn->db->max_rows) { | |
die(conn, "There's not that many records."); | |
} | |
} | |
switch(action) { | |
case 'c': | |
if(argc != 5) die(conn, "Need max_data and max_rows to create db."); | |
Database_create(atoi(argv[3]), atoi(argv[4]), conn); | |
Database_write(conn); | |
break; | |
case 'g': | |
if(argc != 4) die(conn, "Need an id to get"); | |
Database_get(conn, id); | |
break; | |
case 's': | |
if(argc != 6) die(conn, "Need id, name, email to set"); | |
Database_set(conn, id, argv[4], argv[5]); | |
Database_write(conn); | |
break; | |
case 'd': | |
if(argc != 4) die(conn, "Need id to delete"); | |
Database_delete(conn, id); | |
Database_write(conn); | |
break; | |
case 'l': | |
Database_list(conn); | |
break; | |
default: | |
die(conn, "Invalid action, only: c=create, g=get, s=set, d=del, l=list"); | |
} | |
Database_close(conn); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment