From 0a9b9a2ef61c60b5b74ab648969a161d94cdb58e Mon Sep 17 00:00:00 2001 From: <> Date: Sun, 12 Mar 2017 21:13:44 -0700 Subject: [PATCH] taken into account of a NULL query string --- main.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index f9ccc73..190892c 100644 --- a/main.c +++ b/main.c @@ -4,21 +4,20 @@ #include #include -inline char *get_filename(const char c) { - char *qout; - switch(c) { - default: - qout = "queries/mranks.sql"; - break; - case 'm': - qout = "queries/mleaderboard-ojoin.sql"; - break; - case 'p': - qout = "queries/rplayers.sql"; - break; - case 't': - qout = "queries/test.txt"; - break; +inline char *get_filename(const char *c) { + char *qout = "queries/mranks.sql"; + if (c != NULL) { + switch(*c) { + case 'm': + qout = "queries/mleaderboard-ojoin.sql"; + break; + case 'p': + qout = "queries/rplayers.sql"; + break; + case 't': + qout = "queries/test.txt"; + break; + } } return qout; } @@ -58,7 +57,7 @@ inline void print_tblheader(const char *c) { printf("%s", out); } -//display and format the results of the query. +// display and format the results of the query. void qresult(sqlite3_stmt *sp, const char *c) { int e; unsigned int i; @@ -74,9 +73,12 @@ void qresult(sqlite3_stmt *sp, const char *c) { printf(""); } +// 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) { - const char *p = strchr(str, '=') + 1; - const char qc = str[0]; + const char *p = (str != NULL) ? strchr(str, '=') + 1 : NULL; + const char qc = (str != NULL) ? str[0] : '\0'; sqlite3 *db; char *error; 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. void getquery(const char *qs) { char *qf, *qp; - char *fname = get_filename(qs[0]); + char *fname = get_filename(qs); if (fname != NULL) { FILE *f = fopen(fname, "r"); if (f != NULL && qf != NULL) { @@ -118,7 +120,6 @@ void getquery(const char *qs) { } fclose(f); } - qp = strchr(qs, '=') + 1; executequery(qf, qs); free(qf); } @@ -136,7 +137,8 @@ void html(void) { "; printf("%s", html_top); // getquery(getenv("QUERY_STRING")); - getquery("map=pornstar-kaine"); +// getquery("map=pornstar-kaine"); + getquery(NULL); printf("%s", html_bot); }