From f6fe2dd0e6c9f50f4ee98025b1342e31be67dcdd Mon Sep 17 00:00:00 2001 From: gramanas Date: Mon, 29 Oct 2018 20:26:57 +0200 Subject: Make exit value -1 if error, 0 else. --- src/actions.c | 71 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 31 deletions(-) (limited to 'src/actions.c') diff --git a/src/actions.c b/src/actions.c index ddfe9b9..36e3a42 100644 --- a/src/actions.c +++ b/src/actions.c @@ -25,7 +25,7 @@ int run_INIT(UserOpt * opt, Conf *conf) { } if (init_create_config_file(opt)) { sERR("Cound not create config file."); - return -2; + return -1; } DB db = init_make_DB(opt); if (db.error == SQL_NO_ERR) { @@ -65,7 +65,7 @@ int run_ADD(UserOpt * opt, Conf *conf) { PRINT_ERR(err); error: close_DB(&db); - return 1; + return -1; } close_DB(&db); return 0; @@ -83,7 +83,7 @@ int run_DEL(UserOpt * opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { ERR("The database file is currupted. Run ck init anew."); } - return -2; + return -1; } int rc = del_transaction_try(&db, delOpt.arg, delOpt.isConf); close_DB(&db); @@ -94,7 +94,7 @@ int run_DEL(UserOpt * opt, Conf *conf) { case DEL_ERR_WRONG_PATH: ERR("Path %s doesnt exist.", delOpt.arg); } - return -2; + return -1; } int run_EDIT(UserOpt *opt, Conf *conf) { @@ -103,7 +103,7 @@ int run_EDIT(UserOpt *opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { ERR("The database file is currupted. Run ck init anew."); } - return 1; + return -1; } list_rewind(opt->args); @@ -146,18 +146,29 @@ int run_EDIT(UserOpt *opt, Conf *conf) { } close_DB(&db); str_join_dirname_with_basename(confPath, secret ? conf->scrt_dir : conf->vc_dir, confName); - HELP("Editing %s", confPath); char *editor = getenv("EDITOR"); char command[STR_L]; - strcpy(command, str_is_empty(editor) ? "nano" : editor); + if (str_is_empty(editor)) { + if (system("which nano > /dev/null 2>&1") != 0) { + ERR("Nano not found. Please set $EDITOR to your desired editor."); + return -1; + } + strcpy(command, "nano"); + } else { + strcpy(command, editor); + } + + strcat(command, " "); strcat(command, confPath); + + HELP("editing...\n%s", command); system(command); return 0; error: close_DB(&db); - return 1; + return -1; } int run_LIST(UserOpt *opt, Conf *conf) { @@ -166,7 +177,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { ERR("The database file is currupted. Run ck init anew."); } - return 1; + return -1; } cklist *the_list = list_make_new(); @@ -227,15 +238,23 @@ int run_LIST(UserOpt *opt, Conf *conf) { error: close_DB(&db); list_free(the_list); - return 1; + return -1; } int run_SEARCH(UserOpt *opt, Conf *conf) { UNUSED(conf); - if (system("which grep > /dev/null") != 0) { - return 2; + if (system("which grep > /dev/null 2>&1") != 0) { + ERR("No grep avaliable. Please make sure you have grep installed."); + return -1; } DB db = open_DB(opt); + if (db.ptr == NULL) { + if (db.error == SQL_ERR_NO_TABLES) { + ERR("The database file is currupted. Run ck init anew."); + } + return -1; + } + cklist *paths = list_make_new(); list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/); close_DB(&db); @@ -250,7 +269,7 @@ int run_SEARCH(UserOpt *opt, Conf *conf) { cmd = popen(command, "r"); if (cmd == NULL) { list_free(paths); - return 1; + return -1; } while (fgets(result, sizeof(result), cmd)) { printf("%s", result); @@ -265,17 +284,6 @@ int run_SEARCH(UserOpt *opt, Conf *conf) { int run_HELP(UserOpt *opt, Conf *conf) { UNUSED(conf); char tmp[STR_M]; - if (strcmp(list_get(opt->args), "man") == 0) { -#define X(ACTION) \ - get_possible_action_strings(tmp, CKA_##ACTION); \ - HELP("%s:\n%s", #ACTION, tmp); \ - print_##ACTION##_help(); \ - HELP(""); - CK_ACTIONS -#undef X - return 0; - } - if (strcmp(list_get(opt->args), "config") == 0) { print_conf_help(); return 0; @@ -298,7 +306,7 @@ int run_HELP(UserOpt *opt, Conf *conf) { default: ERR("Unknown action: %s", list_get(opt->args)); } - return 1; + return -1; } int run_RESTORE(UserOpt *opt, Conf *conf) { @@ -308,7 +316,7 @@ int run_RESTORE(UserOpt *opt, Conf *conf) { if (db.error == SQL_ERR_NO_TABLES) { ERR("The database file is currupted. Run ck init anew."); } - return 1; + return -1; } cklist *from = list_make_new(); cklist *to = list_make_new(); @@ -352,13 +360,14 @@ int run_RESTORE(UserOpt *opt, Conf *conf) { err_flag = 1; } close_DB(&db); + int rc = -1; if (!err_flag) { - int rc = restore_make_links(from, to); - list_free(from); - list_free(to); - return rc; + rc = restore_make_links(from, to); + if (rc) { + sERR("Restore failed."); + } } list_free(from); list_free(to); - return -2; + return rc; } -- cgit v1.2.3