taken into account of a NULL query string

This commit is contained in:
2017-03-12 21:13:44 -07:00
parent 9894f3c77a
commit 0a9b9a2ef6

24
main.c
View File

@ -4,12 +4,10 @@
#include <string.h> #include <string.h>
#include <sqlite3.h> #include <sqlite3.h>
inline char *get_filename(const char c) { inline char *get_filename(const char *c) {
char *qout; char *qout = "queries/mranks.sql";
switch(c) { if (c != NULL) {
default: switch(*c) {
qout = "queries/mranks.sql";
break;
case 'm': case 'm':
qout = "queries/mleaderboard-ojoin.sql"; qout = "queries/mleaderboard-ojoin.sql";
break; break;
@ -20,6 +18,7 @@ inline char *get_filename(const char c) {
qout = "queries/test.txt"; qout = "queries/test.txt";
break; break;
} }
}
return qout; return qout;
} }
@ -74,9 +73,12 @@ void qresult(sqlite3_stmt *sp, const char *c) {
printf("</TABLE>"); printf("</TABLE>");
} }
// const char 'sql' is never NULL.
// const char 'str' may be NULL, however it is only when
// the contents of mranks.sql is in const char 'sql'.
bool executequery(const char *sql, const char *str) { bool executequery(const char *sql, const char *str) {
const char *p = strchr(str, '=') + 1; const char *p = (str != NULL) ? strchr(str, '=') + 1 : NULL;
const char qc = str[0]; const char qc = (str != NULL) ? str[0] : '\0';
sqlite3 *db; sqlite3 *db;
char *error; char *error;
int con = sqlite3_open("db/cts.db", &db); int con = sqlite3_open("db/cts.db", &db);
@ -103,7 +105,7 @@ bool executequery(const char *sql, const char *str) {
// containing the data from query files. // containing the data from query files.
void getquery(const char *qs) { void getquery(const char *qs) {
char *qf, *qp; char *qf, *qp;
char *fname = get_filename(qs[0]); char *fname = get_filename(qs);
if (fname != NULL) { if (fname != NULL) {
FILE *f = fopen(fname, "r"); FILE *f = fopen(fname, "r");
if (f != NULL && qf != NULL) { if (f != NULL && qf != NULL) {
@ -118,7 +120,6 @@ void getquery(const char *qs) {
} }
fclose(f); fclose(f);
} }
qp = strchr(qs, '=') + 1;
executequery(qf, qs); executequery(qf, qs);
free(qf); free(qf);
} }
@ -136,7 +137,8 @@ void html(void) {
</body></html>"; </body></html>";
printf("%s", html_top); printf("%s", html_top);
// getquery(getenv("QUERY_STRING")); // getquery(getenv("QUERY_STRING"));
getquery("map=pornstar-kaine"); // getquery("map=pornstar-kaine");
getquery(NULL);
printf("%s", html_bot); printf("%s", html_bot);
} }