/* acro.c */ #include "acro.h" #include #include #include #include #include int main(int argc, char** argv) { printf("Content-type: text/html\n\n"); printf(""); char *data = getenv("QUERY_STRING"); char acro[STR_LEN]; sscanf(data, "acronym=%s", &acro); int mode = (strcmp("", data) == STR_EQ) ? SHOW_ALL : LOOK_UP; MYSQL *connection; MYSQL *connect; MYSQL_RES *result; connection = mysql_init((MYSQL*)NULL); if (!connection) { fprintf(stderr, "The connection couldn't be initialized.\n"); return EXIT_FAILURE; } char host[STR_LEN] = ""; char name[STR_LEN] = ""; char passwd[STR_LEN] = ""; char db[STR_LEN] = ""; get_credentials(host, name, passwd, db); connect = mysql_real_connect (connection, host, name, passwd, db, 0, (const char *)NULL, 0); if (!connection) { printf("Couldn't connect to the database.\n"); print_mysql_error(connection); return EXIT_FAILURE; } if (mode == SHOW_ALL) { send_query_show_all(connection); strcpy(acro, "All acronyms
(some may indicate several things)"); } else if (mode == LOOK_UP) { send_query(connection, acro); } result = mysql_store_result(connection); if (!result) { printf("Couldn't get any result from the database.\n"); print_mysql_error(connection); return EXIT_FAILURE; } print_all(result, acro); clean_and_exit(result, connection); } void get_credentials(char* host, char* name, char* passwd, char* db) { char string_path[STR_LEN] = "/etc/.acro"; struct stat sb; stat(string_path, &sb); // 33216 = 100700 in octal => rwx for owner only if (sb.st_mode != 33216) { fprintf(stderr, "Corrupt login file: permissions\n"); exit(EXIT_FAILURE); } else { FILE *login_file = fopen(string_path, "r"); fgets(host, STR_LEN, login_file); fgets(name, STR_LEN, login_file); fgets(passwd, STR_LEN, login_file); fgets(db, STR_LEN, login_file); host[strcspn (host, "\n")] = '\0'; name[strcspn (name, "\n")] = '\0'; passwd[strcspn (passwd, "\n")] = '\0'; db[strcspn (db, "\n")] = '\0'; fclose(login_file); } } void clean_and_exit(MYSQL_RES *result, MYSQL *connection) { mysql_free_result(result); mysql_close(connection); exit(EXIT_SUCCESS); } void print_mysql_error(MYSQL *connection) { printf("

Database error message %s

", mysql_error(connection)); } void print_all(MYSQL_RES *result, char acro[]) { printf("%s ", acro); print_result(result); printf("

« Back

"); } void print_result(MYSQL_RES *result) { MYSQL_ROW row = mysql_fetch_row(result); if (!row) { printf("
    No hit!
"); } else { printf("
    "); do printf("
  • %s
  • ", row[0]); while (row = mysql_fetch_row(result)) ; printf("
"); } } void send_query(MYSQL *connection, char acro[]) { char query[STR_LEN]; strcpy(query, "select acro_spelled_out from acronyms where acronym = '"); strcat(query, acro); strcat(query, "';"); execute_query(connection, query); } void send_query_show_all(MYSQL *connection) { char query[STR_LEN]; strcpy(query, "select distinct acronym from acronyms;"); execute_query(connection, query); } void execute_query(MYSQL *connection, char query[]) { const int QUERY_OK = 0; if(mysql_query(connection, query) != QUERY_OK) { printf("The select query failed."); print_mysql_error(connection); exit(EXIT_FAILURE); } }