/* const char *f: this means that we have a changable pointer to a constant string. I believe this means that the filename cannot change, although we could decide to change the address of the filename variable to something else. */ struct Connection *database_open(const char *filename, char mode) { /* We allocate enough space for a new HEAP, since it is a pointer and not called without malloc, so that we can update its fields with the arguments */ struct Connection *conn = malloc(sizeof(struct Connection)); if(!conn) die("Memory error"); // die if our pointer to new conn failed conn->db = malloc(sizeof(struct Database)); if(!conn->db) die("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("Failed to open the file"); return conn; }
struct Connection *database_open(const char *filename, char mode) { struct Connection *conn = (struct Connection *)malloc(sizeof(struct Connection)); if(!conn) die("memory error"); conn->db = (struct Database *)malloc(sizeof(struct Database)); if(!conn->db) die("memory error"); conn->file = fopen(filename, (mode=='c')?"W":"r+"); if(conn->file) database_load(conn); if(!conn->file) die("failed to open the file"); return conn; }
static int tcl_loaddb(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) { if(argc != 1) { sprintf(interp->result, WRONG_NUMBER_OF_ARGUMENTS, argv[0]); return TCL_OK; } else { ya_result return_code; config_data *config = NULL; /* Initialise configuration file */ if(FAIL(config_init(S_CONFIGDIR, &config))) { return EXIT_FAILURE; } /* Read configuration file */ if(FAIL(config_read("main", &config))) { return EXIT_FAILURE; } /* Read configuration file */ if(FAIL(config_read("zone", &config))) { return EXIT_FAILURE; } if(FAIL(return_code = database_load(&server_context->db_zdb, config->data_path, config->zones))) { OSDEBUG(termout, "error: %r\n", return_code); return return_code; } sprintf(interp->result, "done"); return TCL_OK; } return TCL_OK; }
int main(int argc, char *argv[]) { Database db; database_init(&db); if(argv[1]) { database_create(&db, argv[1]); checkDBState(&db); } else database_load(&db, DB_FILE); server(&db); return 0; }