From 562bad603ce0fe2a6556f0f1aae4f08c9c300987 Mon Sep 17 00:00:00 2001
From: gramanas <anastasis.gramm2@gmail.com>
Date: Tue, 17 Apr 2018 12:30:44 +0300
Subject: fix memory leaks, add logo

---
 src/actions.c    |  3 ++-
 src/ck.c         | 20 +++++++-------------
 src/confparser.c | 14 +++++---------
 src/dblayer.c    | 27 ++++++++++++++++-----------
 src/dblayer.h    |  2 ++
 5 files changed, 32 insertions(+), 34 deletions(-)

(limited to 'src')

diff --git a/src/actions.c b/src/actions.c
index 71f27da..98e161c 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -28,7 +28,7 @@ int run_INIT(UserOpt * opt, Conf *conf) {
 
 int run_ADD(UserOpt * opt, Conf *conf) {
   printf("Running %s\n", "add");
-  DB db = open_DB();
+  DB db = open_DB(opt);
   if (db.ptr == NULL) {
     if (db.error == SQL_ERR_NO_TABLES) {
       printf("no tables\n");
@@ -37,6 +37,7 @@ int run_ADD(UserOpt * opt, Conf *conf) {
   for (int i = 0; i < opt->argc; i++) {
     printf("[%d]: %s\n", i, opt->argv[i]);
   }
+  close_DB(&db);
   return 0;
 }
 
diff --git a/src/ck.c b/src/ck.c
index 61797b4..98190b1 100644
--- a/src/ck.c
+++ b/src/ck.c
@@ -26,30 +26,24 @@ int main(int argc, char *argv[]) {
   if (opt.action != CKA_INIT) {
     if (!db_exists(&opt)) {
       printf("ck is not initialized in %s.\nRun ck init first.\n", opt.confDir);
+      free_user_opt(&opt);
       return 1;
     }
     if (!config_file_parse(&conf, &opt)) {
+      free_user_opt(&opt);
       return 1;
     }
   }
 
-  int ok;
-  switch(opt.action) {
-#define X(ACTION)                               \
-    case CKA_##ACTION:                          \
-      ok = run_##ACTION(&opt, &conf);           \
-      break;
-    CK_ACTIONS
-#undef X
-  }
 
+  /* Run action and print results */
   switch(opt.action) {
-#define X(ACTION)                                       \
-    case CKA_##ACTION:                                  \
-      print_##ACTION##_result(ok);                      \
+#define X(ACTION)                                               \
+    case CKA_##ACTION:                                          \
+      print_##ACTION##_result(run_##ACTION(&opt, &conf));       \
       break;
     CK_ACTIONS
-#undef X    
+#undef X
   }
   free_user_opt(&opt);
   return 0;
diff --git a/src/confparser.c b/src/confparser.c
index 0edd155..2ffe701 100644
--- a/src/confparser.c
+++ b/src/confparser.c
@@ -26,15 +26,13 @@ int remove_newline(char buff[]) {
   return strlen(buff);
 }
 
-char* read_next_line(FILE *f) {
+int read_next_line(char *line, FILE *f) {
   char nextLine[200];
-  char *line;
   if (fgets(nextLine, 200, f) == NULL) {
-    return NULL;
+    return -1;
   }
-  line = malloc(remove_newline(nextLine));
   strcpy(line, nextLine);
-  return line;
+  return 0;
 }
 
 int is_empty(const char *s) {
@@ -88,9 +86,9 @@ ConfigParserResult parse(Conf *conf, UserOpt *opt) {
   }
   free(confName);
   int flag = 1;
-  char *line = read_next_line(confPtr);
+  char line[200];
   char matched[200];
-  while (line != NULL) {
+  while (read_next_line(line, confPtr)) {
     switch(match_variables(line, matched)) {
 #define X(var, str, name)                       \
       case CV_##var:                            \
@@ -108,8 +106,6 @@ ConfigParserResult parse(Conf *conf, UserOpt *opt) {
     default:
       printf("%s:\n%s\n", "Config error in line", line);
     }
-    free(line);
-    line=read_next_line(confPtr);
   }
 #define X(var, str)                               \
   if (conf->var == NULL) {                        \
diff --git a/src/dblayer.c b/src/dblayer.c
index a40fd85..b781f47 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -12,7 +12,7 @@
 
 const char * const DB_NAME = "/ckdb";
 
-char *make_db_name(char * confPath) {
+char *make_db_name(char *confPath) {
   char *db_path = strdup(confPath);
   
   db_path = realloc(db_path, strlen(confPath)+strlen(DB_NAME)+1);
@@ -34,7 +34,7 @@ int db_exists(UserOpt *opt) {
 
 // check if db has the correct tables
 int check_initialized_DB(sqlite3 *db) {
-  char *sql = "SELECT * FROM SQLITE_MASTER;";
+  char *sql = "SELECT * FROM SQLITE_MASTER WHERE type='table';";
   sqlite3_stmt *stmt;
 
   int rc = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
@@ -109,17 +109,18 @@ DB open_DB(UserOpt *opt) {
 }
 
 void init_make_tables(DB *db) {
-  char * sql = "CREATE TABLE REL("
-    "pId INT    NOT NULL,"
-    "cId INT    NOT NULL);"
+  char * sql =
+    "CREATE TABLE REL("
+    "pId INT NOT NULL,"
+    "cId INT NOT NULL);"
     "CREATE TABLE PROGRAM("
-    "id INT PRIMARY KEY     NOT NULL,"
-    "name           TEXT    NOT NULL);"
+    "id   INT  NOT NULL PRIMARY KEY,"
+    "name TEXT NOT NULL);"
     "CREATE TABLE CONFIG("
-    "id INT PRIMARY KEY     NOT NULL,"
-    "path           TEXT    NOT NULL,"
-    "secret         INT     NOT NULL,"
-    "prime          INT     NOT NULL);";
+    "id     INT  NOT NULL PRIMARY KEY,"
+    "path   TEXT NOT NULL,"
+    "secret INT  NOT NULL,"
+    "prime  INT  NOT NULL);";
 
   char *err_msg = NULL;
   int rc = sqlite3_exec(db->ptr, sql, 0, 0, &err_msg);
@@ -130,3 +131,7 @@ void init_make_tables(DB *db) {
     return;
   }
 }
+
+int add_insert_program_to_db(DB *db, char* name) {
+  return 0;
+}
diff --git a/src/dblayer.h b/src/dblayer.h
index 8a57dca..d12118d 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -35,4 +35,6 @@ extern void close_DB(DB *DB);
 extern void init_make_tables(DB *db);
 extern DB init_make_DB();
 
+/* add */
+extern int add_insert_program_to_db(DB *db, char * name);
 #endif /* DBLAEYR_H */
-- 
cgit v1.2.3