I'm moderately new to programming in C and I'm not sure about programming practices with code; suddenly I have had fears that my code is messy and disorganized. I can read it when I come back a month later yet I'm still nervous if I'm doing anything that's considered taboo.
This isn't all the code that I've done with this project, I think my other code is fine.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include "functions.h"
#include "errh.h"
#include "mysqlerr.h"
void FIRST_TIME_HANDLER(MYSQL *conn)
{
printw("[+] Doing initialization checks...\n");
SEND_MYSQL_QUERY(conn, "create table IF NOT EXISTS user_2(username varchar(25) not null, passwd varchar(25) not null)");
printw("[+] Handler has checked tables.\n");
refresh();
}
void INThandler(int sig)
{
signal(sig, SIG_IGN);
printw("Closing server...\n");
refresh();
sleep(5);
endwin();
exit(0);
}
MYSQL_INFO *CONNECTION_INIT()
{
MYSQL_INFO *info = malloc(sizeof(*info));
info->IP = "IPADDRGOHERE"; info->user = "Hello";
info->pass = "World"; info->DB = "DBGOHERE";
return info;
}
int EXIT_SERVER(MYSQL *connection)
{
mysql_close(connection);
printw("MySQL session has now closed.\n");
refresh();
return KILL_SIGNAL;
}
MYSQL *MYSQL_CONNECT_DB(MYSQL_INFO *info)
{
MYSQL *connection;
const char *server = info->IP;
const char *user = info->user;
const char *password = info->pass;
const char *database = info->DB;
connection = mysql_init(NULL);
if(!mysql_real_connect(connection, server, user, password, database, 0,
NULL, 0))
{
//printw("[-] Error: %s\n", mysql_error(connection));
MYSQL *f = KILL_SIGNAL;
refresh();
return f;
}
printw("[+] Connected to MySQL server.\n");
FIRST_TIME_HANDLER(connection);
return connection;
}
void SEND_MYSQL_QUERY(MYSQL *conn, const char *query)
{
refresh();
if(mysql_query(conn, query))
{
ERROR err;
throw_error_init(err, 0, 1, NONFATAL, "MySQL QUERY ERROR",
"ERROR IN THE PROCESS OF QUERY.", MySQL_query_error);
}
else
{
printw("Query sent.\n");
}
}
int init_connection()
{
MYSQL *conn;
MYSQL_INFO *info = CONNECTION_INIT();
if((conn = MYSQL_CONNECT_DB(info)) == (MYSQL *) 0)
{
ERROR err;
throw_error_init(err, 1, 1, FATAL, "INIT CONNECTION ERROR",
"Failed to connect to server.", MySQL_init_failure);
}
else {
int key;
nodelay(stdscr,TRUE);
signal(SIGINT, INThandler);
while(1)
{
if((key = getch()) == ERR)
{
if(mysql_ping(conn) == 1)
{
printw("Error!\n");
refresh();
return 1;
}
}
else
{
if(key == EOF) break;
}
}
free(info);
EXIT_SERVER(conn);
return 0;
}
return 0;
}