int DB_create_db(const char *db_directory) { int err; FILE *f; char *tmp_file; if (db_directory[strlen(db_directory) - 1] == '/') { tmp_file = strdcat(db_directory, UPDATE_NAME); } else { tmp_file = strdcat(db_directory, "/" UPDATE_NAME); } err = mkdir(db_directory, DIR_MOD); if (err != 0) { return -1; } f = fopen(tmp_file, "w"); if (f == NULL) { free(tmp_file); return -1; } fclose(f); chmod(tmp_file, FILE_MOD); free(tmp_file); return 0; }
DBConnection DBC_open(const char *db_directory) { DBConnection dbc; FILE *f; char *tmp_file; dbc = (DBConnection) malloc(sizeof (struct DBC_struct)); if (db_directory[strlen(db_directory) - 1] == '/') { dbc->db_root = strdcat(db_directory, ""); } else { dbc->db_root = strdcat(db_directory, "/"); } chmod(db_directory, DIR_MOD); tmp_file = DBC_tmp_file(dbc); f = fopen(tmp_file, "w"); chmod(tmp_file, FILE_MOD); free(tmp_file); if (f == NULL) { free(dbc->db_root); free(dbc); return NULL; } fclose(f); dbc->result_names = StrList_create(); dbc->result_row_capacity = 10; dbc->result_row_mem = (StrList*) malloc(dbc->result_row_capacity * sizeof(StrList)); dbc->result_row_size = 0; dbc->result_row_actual = -1; return dbc; }
static char *DBC_table_filename(DBConnection dbc, const char *table) { char *s1, *s2, *s3; s1 = URL_encode(table, strlen(table)); s2 = strdcat(DATA_PREFIX, s1); s3 = strdcat(dbc->db_root, s2); free(s1); free(s2); return s3; }
int response_setContentType(const char *mimeType) { if (response_committed) { return -1; } else { response_contentType = strdcat(mimeType, ""); return 0; } }
char *get_message(void) { FILE *fmessages; fmessages = fopen(MESSAGE_FILE, "r"); if (fmessages==NULL) { return strdcat("", ""); } else { char *mess; mess = read_std_file(fmessages, -1); fclose(fmessages); return mess; } }
static void session_create(void) { char buf[64]; long rands[4]; long t = time(NULL); StrList cvalues; DBConnection dbc; if (session_id != NULL) { free(session_id); } if (session_attrs != NULL) { Form_destroy(session_attrs); } dbc = DBC_open(session_db); while (1) { /* Make session id. */ int isDup = 0; random_getBytes((char*) rands, sizeof(rands)); sprintf(buf, "%08lx%08lx%08lx%08lx", rands[0], rands[1], rands[2], rands[3]); /* Search duplicated & remove invalid sessions */ DBC_select(dbc, "sessions", NULL, NULL); while (DBC_next(dbc) == 0) { char *sid = DBC_get(dbc, "id"); char *slasttime = DBC_get(dbc, "last_time"); if (strcmp(sid, buf) == 0) { isDup = 1; } if (t - atoi(slasttime) >= CGI_SESSION_MAXINACTIVETIME) { DBC_delete(dbc, "sessions", "id", sid); } free(sid); free(slasttime); } if (! isDup) break; } /* Insert... */ session_id = strdcat(buf, ""); session_attrs = Form_create(""); cvalues = StrList_create(); /* Add id */ StrList_add(cvalues, buf); /* Add creation & last access time */ sprintf(buf, "%ld", t); StrList_add(cvalues, buf); StrList_add(cvalues, buf); /* Add session attributes */ StrList_add(cvalues, ""); DBC_insert(dbc, "sessions", cvalues); StrList_destroy(cvalues); DBC_close(dbc); }
char *DBC_get(DBConnection dbc, const char *cname) { int cindex; if (dbc->result_row_actual < 0) { return NULL; } if (dbc->result_row_actual >= dbc->result_row_size) { return NULL; } cindex = StrList_index(dbc->result_names, cname); if (cindex == -1) { return NULL; } return strdcat("", StrList_get(dbc->result_row_mem[dbc->result_row_actual], cindex)); }
// New strtok function to allow for empty ("") separators char *_strtok(char **str, const char *sep){ size_t pos = 1; char *tmp = strdcat(*str, ""); if(strcmp(sep, "") != 0) pos = strcspn(*str, sep); *str += pos; tmp[pos] = '\0'; if(strlen(*str) == 0) *str = NULL; else if(strcmp(sep, "") != 0) *str += 1; return tmp; }
static const char *session_getId(void) { /* Session-id Cookie processing */ if (session_id == NULL) { if (session_req_cookie <= -2) { int i; session_req_cookie = -1; for (i = 0; i < request_cookieCount(); i ++) { if (strcmp(request_cookieName(i), CGI_SESSIONID_NAME) == 0) { session_req_cookie = i; session_id = strdcat(request_cookieValue(i), ""); break; } } } } return session_id; }
void session_init(const char *db) { session_db = strdcat(db, ""); }