aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-04-16 14:34:30 +0300
committergramanas <anastasis.gramm2@gmail.com>2018-04-16 14:43:50 +0300
commit94bc38df829c4816e629c7dcaed31b1e7c75bc4b (patch)
treec02d2de0f44462c82e24591127caf66b05d8710d
parentafc0f4906ed9891962d1797581afeb5c61e2e3af (diff)
downloadck-94bc38df829c4816e629c7dcaed31b1e7c75bc4b.tar.gz
ck-94bc38df829c4816e629c7dcaed31b1e7c75bc4b.tar.bz2
ck-94bc38df829c4816e629c7dcaed31b1e7c75bc4b.zip
Add asan option to cmake
finish init without any asan warnings
-rw-r--r--CMakeLists.txt52
-rw-r--r--README.org36
-rwxr-xr-xres/check_ck (renamed from src/tests/check_ck)2
-rw-r--r--res/cmake/FindSQLite3.cmake (renamed from cmake/FindSQLite3.cmake)0
-rw-r--r--src/actionparser.c133
-rw-r--r--src/actionparser.h1
-rw-r--r--src/actions.c44
-rw-r--r--src/checks.c133
-rw-r--r--src/checks.h31
-rw-r--r--src/ck.c12
-rw-r--r--src/confparser.c70
-rw-r--r--src/confparser.h6
-rw-r--r--src/dblayer.c40
-rw-r--r--src/dblayer.h4
-rwxr-xr-xtests/init (renamed from src/tests/init)9
15 files changed, 267 insertions, 306 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index dd7040a..4be093a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,32 +1,43 @@
cmake_minimum_required (VERSION 3.5.6)
project(ck C)
+
+set(CMAKE_C_COMPILER clang)
+# gcc flags
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3")
+
+option(CK_ASAN "Build with asan")
+
+if(CK_ASAN)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
+endif(CK_ASAN)
# Set project directories
-set(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
+set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
+set(RES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/res)
+set(TESTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tests)
+
# Set source code locations
-set(ckBin_src ${PROJECT_SOURCE_DIR}/ck.c)
+set(ckBin_src ${SRC_DIR}/ck.c)
set(ckLib_src
- ${PROJECT_SOURCE_DIR}/ck.c
- ${PROJECT_SOURCE_DIR}/actionparser.c
- ${PROJECT_SOURCE_DIR}/actions.c
- ${PROJECT_SOURCE_DIR}/confparser.c
- ${PROJECT_SOURCE_DIR}/checks.c
- ${PROJECT_SOURCE_DIR}/dblayer.c
+ ${SRC_DIR}/ck.c
+ ${SRC_DIR}/actionparser.c
+ ${SRC_DIR}/actions.c
+ ${SRC_DIR}/confparser.c
+ ${SRC_DIR}/dblayer.c
)
set(ckLib_hdr
- ${PROJECT_SOURCE_DIR}/actionparser.h
- ${PROJECT_SOURCE_DIR}/actions.h
- ${PROJECT_SOURCE_DIR}/confparser.h
- ${PROJECT_SOURCE_DIR}/checks.h
- ${PROJECT_SOURCE_DIR}/dblayer.h
+ ${SRC_DIR}/actionparser.h
+ ${SRC_DIR}/actions.h
+ ${SRC_DIR}/confparser.h
+ ${SRC_DIR}/dblayer.h
)
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RES_DIR}/cmake)
find_package(SQLite3)
# Include directories
-include_directories(${PROJECT_SOURCE_DIR})
+include_directories(${SRC_DIR})
include_directories(${PROJECT_BINARY_DIR})
include_directories(${CMAKE_BINARY_DIR})
include_directories(${SQLITE3_INCLUDE_DIRS})
@@ -42,9 +53,10 @@ add_executable(ck ${ckBin_src})
target_link_libraries (ck ckLib)
target_link_libraries (ck ${SQLITE3_LIBRARIES})
-# copy test file
-set(PROJECT_TEST_DIR ${PROJECT_SOURCE_DIR}/tests)
-set(BIN_TEST_DIR ${CMAKE_BINARY_DIR}/tests)
+# copy test files
+set(BIN_TESTS_DIR ${CMAKE_BINARY_DIR}/res/tests)
set(PROJECT_TESTING_GROUNDS ${CMAKE_BINARY_DIR}/test_files)
-configure_file(${PROJECT_TEST_DIR}/init ${BIN_TEST_DIR}/init @ONLY)
-configure_file(${PROJECT_TEST_DIR}/check_ck check_ck @ONLY)
+configure_file(${TESTS_DIR}/init ${BIN_TESTS_DIR}/init @ONLY)
+
+# check_ck
+configure_file(${RES_DIR}/check_ck check_ck @ONLY)
diff --git a/README.org b/README.org
index 8d9a7b2..eb0362a 100644
--- a/README.org
+++ b/README.org
@@ -1,12 +1,15 @@
* ck
-** The Config Keeper
+*The Config Keeper*
* build it
** requirements
-- gcc
+- clang (llvm) or gcc (gnu)
- cmake
- sqlite3-dev
+** gcc
+Edit the =CMakeLists.txt= file and change the =clang= to =gcc=
+
** make
#+BEGIN_SRC sh
# clone the repo
@@ -17,13 +20,34 @@
> cmake ~/code/ck
# run make
> make
+# run ck
+> ./ck
#+END_SRC
-** run the tests
-Simply go to the build dir and type.
+* for devs
+** make
+Just build with address sanitizer enabled like so:
+llvm has better asan than gcc, so I use that.
+#+BEGIN_SRC sh
+# clone the repo
+> cd ~/code; git clone https://github.com/gramanas/ck
+# make a build directory and enter it
+> mkdir ~/ck_build; cd ~/ck_build;
+# run cmake
+> cmake ~/code/ck -DCK_ASAN=ON
+# run make
+> make
+# run ck
+> ./ck
+#+END_SRC
+
+** tests
+The testing "suite" is just a bash script
+that executs all the bash scripts under
+=tests/=
-*WARNING!* This will destroy any configurations or databases
-you might have. (for now)
+*** run tests
+Simply go to the build dir and type.
#+BEGIN_SRC sh
./check_ck
#+END_SRC
diff --git a/src/tests/check_ck b/res/check_ck
index 3fd2369..14ec5ba 100755
--- a/src/tests/check_ck
+++ b/res/check_ck
@@ -1,6 +1,6 @@
#!/bin/bash
-DIR=@BIN_TEST_DIR@
+DIR=@BIN_TESTS_DIR@
for i in $( ls $DIR ); do
ERROR="TEST "$i" FAILED:"
PASS="TEST "$i" PASSED"
diff --git a/cmake/FindSQLite3.cmake b/res/cmake/FindSQLite3.cmake
index 9c99ae5..9c99ae5 100644
--- a/cmake/FindSQLite3.cmake
+++ b/res/cmake/FindSQLite3.cmake
diff --git a/src/actionparser.c b/src/actionparser.c
index 8261cf7..cba732e 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -53,7 +53,7 @@ void getOpt(int position, UserOpt *opt) {
nextToken();
// allocate memory
- opt->argv[position] = (char *)malloc((strlen(token)+1)*sizeof(char));
+ opt->argv[position] = (char *)malloc((strlen(token))*sizeof(char) + 1);
strcpy(opt->argv[position], token);
}
@@ -92,36 +92,36 @@ int parse_ADD(UserOpt *opt) {
}
int parse_DEL(UserOpt *opt) {
+ return -1;
}
int parse_EDIT(UserOpt *opt) {
+ return -1;
}
int parse_LIST(UserOpt *opt) {
+ return -1;
}
int parse_SEARCH(UserOpt *opt) {
+ return -1;
}
int parse_HELP(UserOpt *opt) {
+ return -1;
}
int parseVals(UserOpt *opt) {
switch (opt->action) {
-#define X(ACTION) \
+#define X(ACTION) \
case CKA_##ACTION: \
return parse_##ACTION(opt);
CK_ACTIONS
#undef X
- default:
- // can't end up here, but it prevents a compiler warning
- opt->err = PERR_UNKONW_ACTION;
- return -1;
- break;
- }
+ }
return 1;
}
CkAction determineAction() {
int i = 0;
-#define X(ACTION) \
+#define X(ACTION) \
for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) { \
if (strcmp(token, str##ACTION[i]) == 0) { \
return CKA_##ACTION; \
@@ -132,17 +132,28 @@ CkAction determineAction() {
return -1;
}
-UserOpt initUserOpt() {
- UserOpt uo;
- uo.action = -1;
- uo.err = PERR_NOERR;
- uo.argc = 0;
+UserOpt make_empty_user_opt() {
+ UserOpt opt;
+ opt.action = -1;
+ opt.err = PERR_NOERR;
+ opt.argc = 0;
+ opt.confDir = NULL;
for (int i = 0; i < 10; i++) {
- uo.argv[i] = NULL;
+ opt.argv[i] = NULL;
}
- return uo;
+ return opt;
}
+void free_user_opt(UserOpt *opt) {
+ for (int i = 0; i < 10; i++) {
+ if (opt->argv[i] != NULL) {
+ free(opt->argv[i]);
+ }
+ }
+ if (opt->confDir != NULL) {
+ free(opt->confDir);
+ }
+}
void getConfig(UserOpt *opt) {
// get first token
@@ -154,19 +165,26 @@ void getConfig(UserOpt *opt) {
printf("Config needs a value\n");
exit(1);
}
+ if (strcmp(token, ".") == 0){
+ printf("Dot\n");
+ }
struct stat st = {0};
if (stat(token, &st) == -1) {
printf("%s is not a directory\n", token);
exit(1);
}
- opt->confDir = malloc(sizeof(token));
+ opt->confDir = malloc(strlen(token) + 1);
strcpy(opt->confDir, token);
+ // remove trailing `/`
+ if (opt->confDir[strlen(token) - 1] == '/') {
+ opt->confDir[strlen(token) - 1] = '\0';
+ }
return;
}
}
char * defaultConf = "/.ck";
char * home = getenv("HOME");
- opt->confDir = malloc(sizeof(defaultConf)+sizeof(home));
+ opt->confDir = malloc(strlen(defaultConf)+strlen(home)+1);
strcpy(opt->confDir, home);
strcat(opt->confDir, defaultConf);
@@ -176,7 +194,7 @@ void getConfig(UserOpt *opt) {
}
ParseResult parseAction(int argc, char* argv[], UserOpt *opt) {
- *opt = initUserOpt();
+ *opt = make_empty_user_opt();
if (argc < 2) {
return OPR_HELP;
}
@@ -211,29 +229,37 @@ ParseResult parseAction(int argc, char* argv[], UserOpt *opt) {
}
}
-const char * getPossibleActionName(const char* const strAction[]) {
- char *names;
- size_t size = 2; // first chars "{ "
- for (int i = 1; i < atoi(strAction[0]) + 1; i++) {
- size += strlen(strAction[i]) + 2; // comma and space for each entry and " }" for the last one
+void getPossibleActionNames(char * dest, CkAction ckAction) {
+ if (ckAction == -1) {
+ dest = NULL;
+ return;
}
- if ((names = malloc(size)) != NULL) {
- strcpy(names, "{ ");
- int i = 1;
- for (; i < atoi(strAction[0]); i++) {
- strcat(names, strAction[i]);
- strcat(names, ", ");
- }
- // last one
- strcat(names, strAction[atoi(strAction[0])]);
- strcat(names, " }");
- return names;
+
+ char buf[30];
+
+ switch (ckAction) {
+#define X(ACTION) \
+ case CKA_##ACTION: \
+ strcpy(buf, "{ "); \
+ for (int i = 1; i < atoi(str##ACTION[0]); i++) { \
+ strcat(buf, str##ACTION[i]); \
+ strcat(buf, ", "); \
+ } \
+ strcat(buf, str##ACTION[atoi(str##ACTION[0])]); \
+ strcat(buf, " }"); \
+ break;
+ CK_ACTIONS
+#undef X
}
- return NULL;
+
+ strcpy(dest, buf);
}
void printParserError(UserOpt *opt) {
char *errStr = NULL;
+ char names[30];
+ getPossibleActionNames(names, opt->action);
+
switch (opt->err) {
case PERR_NOERR:
return;
@@ -241,13 +267,13 @@ void printParserError(UserOpt *opt) {
asprintf(&errStr, "Unknown action: %s", token);
break;
case PERR_INIT_WRONG:
- asprintf(&errStr, "Initialize database\nUsage: %s version_control_dir secret_dir", getPossibleActionName(strINIT));
+ asprintf(&errStr, "Initialize database\nUsage: %s version_control_dir secret_dir", names);
break;
case PERR_ADD_WRONG:
- asprintf(&errStr, "Add config (new or existing)\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", getPossibleActionName(strADD));
+ asprintf(&errStr, "Add config (new or existing)\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
break;
case PERR_DEL_WRONG:
- asprintf(&errStr, "Delete config or program\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", getPossibleActionName(strDEL));
+ asprintf(&errStr, "Delete config or program\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
break;
case PERR_EDIT_WRONG:
asprintf(&errStr, "Edit config\nUsage: add ProgramName ConfigPath [-s](secret) [-p](primary)");
@@ -263,18 +289,25 @@ void printParserError(UserOpt *opt) {
break;
}
printf("Parsing error\n%s\n", errStr);
- exit(1);
+ free(errStr);
}
void printParserHelp() {
- printf("ck - the config keeper\n" );
- printf("Usage:\n" );
- printf("Initialize: \t%s\n", getPossibleActionName(strINIT));
- printf("Add config: \t%s\n", getPossibleActionName(strADD));
- printf("Delete config: \t%s\n", getPossibleActionName(strDEL));
- printf("Edit config: \t%s\n", getPossibleActionName(strEDIT));
- printf("List configs: \t%s\n", getPossibleActionName(strLIST));
- printf("Search: \t%s\n", getPossibleActionName(strSEARCH));
- printf("Print this: \t%s\n", getPossibleActionName(strHELP));
- exit(0);
+ char names[30];
+ printf("ck - the config keeper\n");
+ printf("Usage:\n");
+ getPossibleActionNames(names, CKA_INIT);
+ printf("Initialize: \t%s\n", names);
+ getPossibleActionNames(names, CKA_ADD);
+ printf("Add config: \t%s\n", names);
+ getPossibleActionNames(names, CKA_DEL);
+ printf("Delete config: \t%s\n", names);
+ getPossibleActionNames(names, CKA_EDIT);
+ printf("Edit config: \t%s\n", names);
+ getPossibleActionNames(names, CKA_LIST);
+ printf("List configs: \t%s\n", names);
+ getPossibleActionNames(names, CKA_SEARCH);
+ printf("Search: \t%s\n", names);
+ getPossibleActionNames(names, CKA_HELP);
+ printf("Print this: \t%s\n", names);
}
diff --git a/src/actionparser.h b/src/actionparser.h
index a097464..854624b 100644
--- a/src/actionparser.h
+++ b/src/actionparser.h
@@ -64,5 +64,6 @@ struct UserOptions {
extern ParseResult parseAction(int argc, char* argv[], UserOpt *opt);
extern void printParserError();
extern void printParserHelp();
+extern void free_user_opt(UserOpt *opt);
#endif // ACTIONPARSER_H
diff --git a/src/actions.c b/src/actions.c
index 881c299..71f27da 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -1,52 +1,24 @@
+/* actions.c - ck actions --------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * ------------------------------------------------------------------ */
#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
#include "actions.h"
#include "dblayer.h"
-int init_create_config_file(UserOpt *opt) {
- struct stat st = {0};
- if (stat("/home/gramanas/.ck", &st) == -1) {
- mkdir("/home/gramanas/.ck", 0755);
- }
-
- FILE *f;
- if ((f = fopen("/home/gramanas/.ck/ckrc", "w")) == NULL) {
- return 1;
- }
- char tmp[200];
- if (stat(opt->argv[0], &st) == -1) {
- printf("Version control directory: %s\ndoes not exist.\n", opt->argv[0]);
- return 1;
- }
- strcpy(tmp, "version_control_dir = ");
- strcat(tmp, opt->argv[0]);
- strcat(tmp, "\n");
- fputs(tmp, f);
- if (stat(opt->argv[1], &st) == -1) {
- printf("Secret directory: %s\ndoes not exist.\n", opt->argv[1]);
- return 1;
- }
- strcpy(tmp, "secret_dir = ");
- strcat(tmp, opt->argv[1]);
- strcat(tmp, "\n");
- fputs(tmp, f);
- fclose(f);
- return 0;
-}
int run_INIT(UserOpt * opt, Conf *conf) {
- if (db_exists()) {
+ if (db_exists(opt)) {
+ printf("conf dir: %s\n", opt->confDir);
printf("ck is already initialized.\n");
return 0;
}
if (init_create_config_file(opt)) {
return 0;
}
- DB db = init_make_DB();
+ DB db = init_make_DB(opt);
if (db.error == SQL_NO_ERR) {
init_make_tables(&db);
}
diff --git a/src/checks.c b/src/checks.c
deleted file mode 100644
index 80b1954..0000000
--- a/src/checks.c
+++ /dev/null
@@ -1,133 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <dirent.h>
-
-#include "checks.h"
-
-
-
-/* const char* const matchVCDirName = " version_control_dir = %s"; */
-/* const char* const matchSCRTDirName = " secret_dir = %s"; */
-/* const char* const matchDBDirName = " database_dir = %s"; */
-
-/* ConfigParserResult parseConfigFile(FILE *f, Conf *c) { */
-/* char *vc_dir, *scrt_dir, *db_dir; */
-/* DIR *dir; */
-
-/* char * buffer = 0; */
-/* long length; */
-/* if (f) { */
-/* fseek(f, 0, SEEK_END); */
-/* length = ftell (f); */
-/* fseek(f, 0, SEEK_SET); */
-/* buffer = malloc (length); */
-/* if (buffer) { */
-/* fread (buffer, 1, length, f); */
-/* for (int i = 0; i < length; i++) { */
-/* if (buffer[i] == '\n') { */
-/* buffer[i] = ' '; */
-/* } */
-/* } */
-/* } */
-/* fclose (f); */
-/* } */
-
- /* if (buffer) { */
- /* // VC dir */
- /* vc_dir = malloc(sizeof(char)*100); */
- /* if (sscanf(buffer, matchVCDirName, vc_dir) != 1) { */
- /* return CPR_NO_VC_DIR; */
- /* } */
- /* dir = opendir(vc_dir); */
- /* if (!dir) { */
- /* return CPR_WRONG_VC_DIR; */
- /* } */
- /* closedir(dir); */
- /* c->VC_dir = vc_dir; */
-
-/* /\* printf("%s", buffer); *\/ */
-
-/* // SCRT_dir */
-/* scrt_dir = malloc(sizeof(char)*100); */
-/* if(sscanf(buffer, matchSCRTDirName, scrt_dir) != 1) { */
-/* return CPR_NO_SCRT_DIR; */
-/* } */
-/* dir = opendir(scrt_dir); */
-/* if (!dir) { */
-/* return CPR_WRONG_SCRT_DIR; */
-/* } */
-/* c->SCRT_dir = scrt_dir; */
-
-/* // DB_dir */
-/* db_dir = malloc(sizeof(char)*100); */
-/* if (sscanf(buffer, matchDBDirName, db_dir) != 1) { */
-/* return CPR_NO_DB_DIR; */
-/* } */
-/* dir = opendir(db_dir); */
-/* if (!dir) { */
-/* return CPR_WRONG_DB_DIR; */
-/* } */
-/* c->DB_dir = db_dir; */
-/* } */
-/* return CPR_OK; */
-/* } */
-
-
-/* char *getConfigPath() { */
-/* char *home = getenv("HOME"); */
-/* char *path; */
-/* if (home == NULL) { */
-/* printf("HOME variable is not set. Can't find config."); */
-
-/* } */
-/* if ((path = malloc(strlen(home)+strlen(configFilename)+1 /\* for the slash *\/))) { */
-/* strcat(path, home); */
-/* strcat(path, "/"); */
-/* strcat(path, configFilename); */
-/* } */
-/* return path; */
-/* } */
-
-// -1 config file problem
-// 0 config content problem
-// 1 all good
-/* int doConfigCheck(Conf *conf) { */
-/* char* ckConfigPath = getConfigPath(); */
-/* if (ckConfigPath == NULL) { */
-/* return -1; */
-/* } */
-/* FILE *confPtr; */
-/* if ((confPtr = fopen(ckConfigPath, "r")) == NULL){ */
-/* return -1; */
-/* } */
-/* return 2; */
-/* } */
-
-/* /\* CheckResult doInitCheck(Conf *conf) { */
- /* switch (doConfigCheck(conf)) { */
- /* case CPR_NO_VC_DIR: */
- /* printf("There is no VersionControl directory defined in the configuration file.\n"); */
- /* return CR_NO_CONFIG; */
- /* case CPR_WRONG_VC_DIR: */
- /* printf("The VersionControl directory defined in the configuration file is wrong.\n"); */
- /* return CR_WRONG_CONFIG; */
- /* case CPR_NO_SCRT_DIR: */
- /* printf("There is no Secret directory defined in the configuration file.\n"); */
- /* return CR_NO_CONFIG; */
- /* case CPR_WRONG_SCRT_DIR: */
- /* printf("The Secret directory defined in the configuration file is wrong.\n"); */
- /* return CR_WRONG_CONFIG; */
- /* case CPR_NO_DB_DIR: */
- /* printf("There is no Database directory defined in the configuration file.\n"); */
- /* return CR_NO_CONFIG; */
- /* case CPR_WRONG_DB_DIR: */
- /* printf("The Database directory defined in the configuration file is wrong.\n"); */
- /* return CR_WRONG_CONFIG; */
- /* case CPR_OK: */
- /* break; */
- /* } */
-// return 0;
- //doDbCheck();
-//}
diff --git a/src/checks.h b/src/checks.h
deleted file mode 100644
index c165c95..0000000
--- a/src/checks.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/* optparser.h - Opt parser for ck -----------------------------*- C -*-
- *
- * This file is part of ck, the config keeper
- *
- * ---------------------------------------------------------------------
- *
- * The code here and in checks.c is responsible for checking
- * if a database file is present and if the user has initialized
- * ck before with correct VC_dir and SCRT_dir paths.
- *
- * It is called before the parsing of the user's arguments
- * in order to make sure that everything is setup properly
- * for the later operations.
- *
- * ------------------------------------------------------------------ */
-#ifndef CHECKS_H
-#define CHECKS_H
-
-typedef enum CheckResults CheckResult;
-enum CheckResults {
- CR_OK,
- CR_NO_CONFIG,
- CR_WRONG_CONFIG,
- CR_NO_DB
-};
-
-
-
-CheckResult doInitCheck();
-
-#endif // CHECKS_H
diff --git a/src/ck.c b/src/ck.c
index 2833176..61797b4 100644
--- a/src/ck.c
+++ b/src/ck.c
@@ -11,21 +11,24 @@ int main(int argc, char *argv[]) {
UserOpt opt;
switch(parseAction(argc, argv, &opt)) {
case OPR_HELP:
+ free_user_opt(&opt);
printParserHelp();
+ return 0;
case OPR_ERR:
printParserError(&opt);
+ free_user_opt(&opt);
+ return 1;
case OPR_OK:
break;
}
- printf("%s\n", opt.confDir);
Conf conf;
if (opt.action != CKA_INIT) {
- if (!db_exists()) {
- printf("ck is not initialized.\nRun ck init first.\n");
+ if (!db_exists(&opt)) {
+ printf("ck is not initialized in %s.\nRun ck init first.\n", opt.confDir);
return 1;
}
- if (!config_file_parse(&conf)) {
+ if (!config_file_parse(&conf, &opt)) {
return 1;
}
}
@@ -48,5 +51,6 @@ int main(int argc, char *argv[]) {
CK_ACTIONS
#undef X
}
+ free_user_opt(&opt);
return 0;
}
diff --git a/src/confparser.c b/src/confparser.c
index d4d33f5..0edd155 100644
--- a/src/confparser.c
+++ b/src/confparser.c
@@ -1,11 +1,15 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
-#include <dirent.h>
#include <ctype.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
#include "confparser.h"
-const char* const configFilename = ".ckrc";
+
+const char * const CONFIG_NAME = "/ckrc";
void conf_values_initialize(Conf *c) {
c->SCRT_dir = NULL;
@@ -65,12 +69,24 @@ int is_dir(char *path) {
return 1;
}
-ConfigParserResult parse(Conf *conf) {
+char *make_config_name(char * confPath) {
+ char *db_path = strdup(confPath);
+
+ db_path = realloc(db_path, strlen(confPath) + strlen(CONFIG_NAME)+1);
+ strcat(db_path, CONFIG_NAME);
+
+ return db_path;
+}
+
+ConfigParserResult parse(Conf *conf, UserOpt *opt) {
conf_values_initialize(conf);
FILE *confPtr;
- if ((confPtr = fopen("/home/gramanas/.ck/ckrc", "r")) == NULL) {
+ char *confName = make_config_name(opt->confDir);
+ if ((confPtr = fopen(confName, "r")) == NULL) {
+ free(confName);
return CPR_NO_CONFIG_FILE;
}
+ free(confName);
int flag = 1;
char *line = read_next_line(confPtr);
char matched[200];
@@ -78,9 +94,10 @@ ConfigParserResult parse(Conf *conf) {
switch(match_variables(line, matched)) {
#define X(var, str, name) \
case CV_##var: \
- conf->var = malloc(strlen(matched)); \
+ conf->var = malloc(strlen(matched)+1); \
strcpy(conf->var, matched); \
if (!is_dir(matched)) { \
+ free(conf->var); \
return CPR_WRONG_##var; \
} \
break;
@@ -105,8 +122,8 @@ ConfigParserResult parse(Conf *conf) {
return CPR_NO_CONFIG_FILE;
}
-int config_file_parse(Conf *conf) {
- switch (parse(conf)) {
+int config_file_parse(Conf *conf, UserOpt *opt) {
+ switch (parse(conf, opt)) {
#define X(var,str,name) \
case CPR_WRONG_##var: \
printf("Config error:\n" \
@@ -125,3 +142,42 @@ int config_file_parse(Conf *conf) {
return 1;
}
}
+
+int init_create_config_file(UserOpt *opt) {
+ struct stat st = {0};
+ char tmp[200];
+ if (stat(opt->argv[0], &st) == -1) {
+ printf("Version control directory: %s\ndoes not exist.\n", opt->argv[0]);
+ return 1;
+ }
+
+ if (stat(opt->argv[1], &st) == -1) {
+ printf("Secret directory: %s\ndoes not exist.\n", opt->argv[1]);
+ return 1;
+ }
+
+ if (stat(opt->confDir, &st) == -1) {
+ mkdir(opt->confDir, 0755);
+ }
+
+ char *confName = make_config_name(opt->confDir);
+ FILE *f;
+ if ((f = fopen(confName, "w")) == NULL) {
+ free(confName);
+ return 1;
+ }
+
+ strcpy(tmp, "version_control_dir = ");
+ strcat(tmp, opt->argv[0]);
+ strcat(tmp, "\n");
+ fputs(tmp, f);
+
+ strcpy(tmp, "secret_dir = ");
+ strcat(tmp, opt->argv[1]);
+ strcat(tmp, "\n");
+ fputs(tmp, f);
+ fclose(f);
+
+ free(confName);
+ return 0;
+}
diff --git a/src/confparser.h b/src/confparser.h
index 2ed8f82..197ff3a 100644
--- a/src/confparser.h
+++ b/src/confparser.h
@@ -8,6 +8,9 @@
* ------------------------------------------------------------------ */
#ifndef CONFPARSER_H
#define CONFPARSER_H
+
+#include "actionparser.h"
+
#define CONFIG_VARIABLES_TABLE \
X(VC_dir, " version_control_dir = %s ", "Version Control directory") \
X(SCRT_dir, " secret_dir = %s " , "Secret directory")
@@ -40,6 +43,7 @@ struct ConfigValues {
};
#undef X
-extern int config_file_parse(Conf *conf);
+extern int config_file_parse(Conf *conf, UserOpt *opt);
+extern int init_create_config_file(UserOpt *opt);
#endif // CONFPARSER_H
diff --git a/src/dblayer.c b/src/dblayer.c
index 180cd93..a40fd85 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -6,17 +6,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include "dblayer.h"
-int db_exists() {
- FILE *db_file;
+const char * const DB_NAME = "/ckdb";
- if ((db_file = fopen("/home/gramanas/.ck/ckdb", "rb")) == NULL) {
- return 0;
+char *make_db_name(char * confPath) {
+ char *db_path = strdup(confPath);
+
+ db_path = realloc(db_path, strlen(confPath)+strlen(DB_NAME)+1);
+ strcat(db_path, DB_NAME);
+
+ return db_path;
+}
+
+int db_exists(UserOpt *opt) {
+ char *db_path = make_db_name(opt->confDir);
+ int ret;
+ ret = 0;
+ if (access(db_path, F_OK) == 0) {
+ ret = 1;
}
- fclose(db_file);
- return 1;
+ free(db_path);
+ return ret;
}
// check if db has the correct tables
@@ -62,24 +75,27 @@ void close_DB(DB *db) {
sqlite3_close(db->ptr);
}
-DB init_make_DB() {
+DB init_make_DB(UserOpt *opt) {
sqlite3 *db;
int rc;
- rc = sqlite3_open("/home/gramanas/.ck/ckdb", &db);
-
- if (rc) {
+ char *db_path = make_db_name(opt->confDir);
+ rc = sqlite3_open(db_path, &db);
+ free(db_path);
+ if (rc != SQLITE_OK) {
return empty_DB(SQL_ERR_NO_DB_FILE);
}
return new_DB(db);
}
-DB open_DB() {
+DB open_DB(UserOpt *opt) {
sqlite3 *db;
int rc;
- rc = sqlite3_open("/home/gramanas/.ck/ckdb", &db);
+ char *db_path = make_db_name(opt->confDir);
+ rc = sqlite3_open(db_path, &db);
+ free(db_path);
if (rc) {
return empty_DB(SQL_ERR_NO_DB_FILE);
diff --git a/src/dblayer.h b/src/dblayer.h
index e5c0b73..8a57dca 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -11,6 +11,8 @@
#include <sqlite3.h>
+#include "actionparser.h"
+
typedef enum SqlErrors SqlError;
enum SqlErrors {
SQL_NO_ERR = 0,
@@ -25,7 +27,7 @@ struct DBstruct {
SqlError error;
};
-extern int db_exists();
+extern int db_exists(UserOpt *opt);
extern DB open_DB();
extern void close_DB(DB *DB);
diff --git a/src/tests/init b/tests/init
index 4f78802..0c38da1 100755
--- a/src/tests/init
+++ b/tests/init
@@ -5,19 +5,20 @@ TEST_LOCATION=@PROJECT_TESTING_GROUNDS@
mkdir -p $TEST_LOCATION/vc
mkdir $TEST_LOCATION/sec
-exec $BIN/ck init $TEST_LOCATION/vc $TEST_LOCATION/sec &
+exec $BIN/ck conf $BIN init $TEST_LOCATION/vc $TEST_LOCATION/sec &
wait $!
-if [ ! -f ~/.ck/ckrc ]; then
+if [ ! -f $BIN/ckrc ]; then
echo -e $ERROR"Config file not created."
exit 1
fi
-if [ ! -f ~/.ck/ckdb ]; then
+if [ ! -f $BIN/ckdb ]; then
echo -e $ERROR"DB file not created."
exit 1
fi
-rm -rf $HOME/.ck
+rm $BIN/ckrc
+rm $BIN/ckdb
rm -rf $TEST_LOCATION
echo -e $PASS