aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-04-17 12:30:44 +0300
committergramanas <anastasis.gramm2@gmail.com>2018-04-17 17:39:29 +0300
commit562bad603ce0fe2a6556f0f1aae4f08c9c300987 (patch)
tree8646982cdcd625dcf7f04262b66d9e78bf53afe4
parent94bc38df829c4816e629c7dcaed31b1e7c75bc4b (diff)
downloadck-562bad603ce0fe2a6556f0f1aae4f08c9c300987.tar.gz
ck-562bad603ce0fe2a6556f0f1aae4f08c9c300987.tar.bz2
ck-562bad603ce0fe2a6556f0f1aae4f08c9c300987.zip
fix memory leaks, add logo
-rw-r--r--README.org1
-rw-r--r--res/logo.pngbin0 -> 20458 bytes
-rw-r--r--src/actions.c3
-rw-r--r--src/ck.c20
-rw-r--r--src/confparser.c14
-rw-r--r--src/dblayer.c27
-rw-r--r--src/dblayer.h2
7 files changed, 33 insertions, 34 deletions
diff --git a/README.org b/README.org
index eb0362a..320a11b 100644
--- a/README.org
+++ b/README.org
@@ -1,3 +1,4 @@
+#+html: <p align="center"><img src="res/logo.png" width="30%" height="30%" /></p>
* ck
*The Config Keeper*
diff --git a/res/logo.png b/res/logo.png
new file mode 100644
index 0000000..ac1184f
--- /dev/null
+++ b/res/logo.png
Binary files differ
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 */