diff options
-rw-r--r-- | src/actionhelper.c | 5 | ||||
-rw-r--r-- | src/ckutil.c | 19 | ||||
-rw-r--r-- | src/ckutil.h | 3 | ||||
-rw-r--r-- | src/confparser.h | 5 | ||||
-rw-r--r-- | src/dblayer.c | 28 | ||||
-rw-r--r-- | test/00_init | 2 | ||||
-rw-r--r-- | unit/ck-test.c | 52 |
7 files changed, 73 insertions, 41 deletions
diff --git a/src/actionhelper.c b/src/actionhelper.c index d1891c9..1d2a518 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -191,6 +191,11 @@ int init_create_config_file(UserOpt *opt) { strcat(tmp, "\n"); fputs(tmp, f); + strcpy(tmp, "home_dir = "); + strcat(tmp, getenv("HOME")); + strcat(tmp, "\n"); + fputs(tmp, f); + fclose(f); return 0; } diff --git a/src/ckutil.c b/src/ckutil.c index 5f8da98..0895677 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -143,17 +143,12 @@ int str_is_empty(const char *s) { return 1; } -int swap_tilde_and_home(char *ret, const char *s) { - if (!ret || !s) { - return -1; - } - char *home = getenv("HOME"); - if (!home) { +int swap_tilde_with_home(char *ret, const char *s, const char *home) { + if (!ret || !s || !home) { return -1; } ulong i; ulong homelen = strlen(home); - if (s[0] == '~') { strcpy(ret, home); for (i = 1; i < strlen(s); i++) { @@ -162,6 +157,16 @@ int swap_tilde_and_home(char *ret, const char *s) { return 0; } + return -1; +} + +int swap_home_with_tilde(char *ret, const char *s, const char *home) { + if (!ret || !s || !home) { + return -1; + } + ulong i; + ulong homelen = strlen(home); + for (i = 0; i < strlen(home); i++) { if (s[i] != home[i]) { return -1; diff --git a/src/ckutil.h b/src/ckutil.h index 72216ef..b9d94db 100644 --- a/src/ckutil.h +++ b/src/ckutil.h @@ -59,7 +59,8 @@ extern void str_join_dirname_with_basename(char *ret, const char *path, * else returns 0. */ extern int str_is_empty(const char *s); -extern int swap_tilde_and_home(char *ret, const char *s); +extern int swap_tilde_with_home(char *ret, const char *s, const char *home); +extern int swap_home_with_tilde(char *ret, const char *s, const char *home); /*********************/ /* utility functions */ diff --git a/src/confparser.h b/src/confparser.h index d7af842..3e772fc 100644 --- a/src/confparser.h +++ b/src/confparser.h @@ -18,9 +18,10 @@ #include "actionparser.h" -#define CONFIG_VARIABLES_TABLE \ +#define CONFIG_VARIABLES_TABLE \ X(vc_dir, " version_control_dir = %s ", "Version Control directory") \ - X(scrt_dir, " secret_dir = %s " , "Secret directory") + X(scrt_dir, " secret_dir = %s " , "Secret directory") \ + X(home_dir, " home_dir = %s " , "Home directory") enum ConfingVariables { CV_NO_VAL_OR_COMMENT, diff --git a/src/dblayer.c b/src/dblayer.c index 244db62..08b1e4c 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -420,9 +420,7 @@ int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secr if (program_has_primary_config(db, pid, path, secret) == 1) { if (!str_is_empty(path)) { if (ret) { - char confName[STR_M]; - str_make_ck_config_name(confName, path, pName); - strcpy(ret, confName); + str_make_ck_config_name(ret, path, pName); } return 0; } @@ -532,14 +530,14 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int att strcat(entry, tmp); } if (attr) { - /* secret */ - if (sqlite3_column_int(stmt, 1)) { - strcat(entry, " [s]"); - } /* primary */ if (sqlite3_column_int(stmt, 2)) { strcat(entry, " [p]"); } + /* secret */ + if (sqlite3_column_int(stmt, 1)) { + strcat(entry, " [s]"); + } /* root */ if (util_is_link_owned_by_root((char *)sqlite3_column_text(stmt, 0))) { strcat(entry, " [root]"); @@ -577,14 +575,14 @@ int list_get_paths(DB *db, cklist *ckl, int bName, int attr) { } free(tmp); if (attr) { - /* secret */ - if (sqlite3_column_int(stmt, 1)) { - strcat(path, " [s]"); - } /* primary */ if (sqlite3_column_int(stmt, 2)) { strcat(path, " [p]"); } + /* secret */ + if (sqlite3_column_int(stmt, 1)) { + strcat(path, " [s]"); + } /* root */ if (util_is_link_owned_by_root((char *)sqlite3_column_text(stmt, 0))) { strcat(path, " [root]"); @@ -668,14 +666,14 @@ int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr) { } free(tmp); if (attr) { - /* secret */ - if (sqlite3_column_int(stmt2, 1)) { - strcat(treePath, " [s]"); - } /* primary */ if (sqlite3_column_int(stmt2, 2)) { strcat(treePath, " [p]"); } + /* secret */ + if (sqlite3_column_int(stmt2, 1)) { + strcat(treePath, " [s]"); + } /* root */ if (util_is_link_owned_by_root((char *)sqlite3_column_text(stmt2, 0))) { strcat(treePath, " [root]"); diff --git a/test/00_init b/test/00_init index 1f3f2a6..daba787 100644 --- a/test/00_init +++ b/test/00_init @@ -20,5 +20,5 @@ if [ ! -f $BIN/ckdb ]; then err "DB file not created." fi -clear_tests 2&>1 /dev/null +clear_tests > /dev/null 2>&1 echo -e $PASS diff --git a/unit/ck-test.c b/unit/ck-test.c index 1264431..8d8ee08 100644 --- a/unit/ck-test.c +++ b/unit/ck-test.c @@ -3,38 +3,60 @@ #include <string.h> #include "cklist.h" +#include "ckutil.h" /* total number of unit tests */ #define TOTAL 2 -#define CK_UNIT_TESTS \ - X(ck_list_init, "Basic cklist test") \ - X(ck_list_exists, "Test list exists function") +#define CK_UNIT_TESTS \ + X(ck_list_test, "Basic cklist test") \ + X(ck_str_utils_test, "Test string utility functions") \ -void ck_list_init() { - cklist *ckl1 = list_make_new(); +void ck_list_test() { + /* init and add */ + cklist *ckl = list_make_new(); cklist *ckl2 = list_make_and_add("first"); list_add(ckl2, "second"); - list_add(ckl1, "first"); - list_add(ckl1, "second"); - assert(strcmp(list_get_at(ckl1, 0), + list_add(ckl, "first"); + list_add(ckl, "second"); + assert(strcmp(list_get_at(ckl, 0), list_get_at(ckl2, 0)) == 0); - assert(strcmp(list_get_at(ckl1, 1), + assert(strcmp(list_get_at(ckl, 1), list_get_at(ckl2, 1)) == 0); - list_free(ckl1); list_free(ckl2); -} -void ck_list_exists() { - cklist *ckl = list_make_new(); - list_add(ckl, "first"); - list_add(ckl, "second"); + /* exists */ assert(list_exists(ckl, "first")); assert(list_exists(ckl, "second")); assert(!list_exists(ckl, "thrid")); list_free(ckl); } +void ck_str_utils_test() { + /* make_ck_config_name */ + char ck_conf_path[STR_M]; + str_make_ck_config_name(ck_conf_path, "/test/path/.config", "emacs"); + assert(strcmp(ck_conf_path, "emacs/.config") == 0); + + /* str_is_empty */ + assert(str_is_empty("")); + assert(str_is_empty(" ")); + assert(str_is_empty("\t")); + + /* swap ~ and home */ + char str[STR_M] = ""; + char str2[STR_M] = ""; + /* getenv("HOME") -> /home/user */ + char home[STR_S] = "/home/user"; + char testpath[STR_S] = "/home/user/file"; + assert(swap_home_with_tilde(str, testpath, home) == 0); + assert(strcmp(str, "~/file") == 0); + assert(swap_tilde_with_home(str2, str, home) == 0); + assert(strcmp(str2, testpath) == 0); + assert(swap_home_with_tilde(str, "/not/starting/with/home", home)); + assert(swap_tilde_with_home(str2, "/not/starting/with/tilde", home)); +} + int main() { int count = 1; #define X(TEST, DESC) \ |