aboutsummaryrefslogtreecommitdiffstats
path: root/src/search.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/search.c')
-rw-r--r--src/search.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/search.c b/src/search.c
new file mode 100644
index 0000000..a76bb29
--- /dev/null
+++ b/src/search.c
@@ -0,0 +1,54 @@
+/* search.c - the search action ----------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
+#include "dblayer.h"
+#include "ckerrlog.h"
+
+ERRLOG(search);
+
+int run_SEARCH(UserOpt *opt, Conf *conf) {
+ if (system("which grep > /dev/null 2>&1") != 0) {
+ ERR("No grep avaliable. Please make sure you have grep installed.");
+ return -1;
+ }
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+
+ cklist *paths = list_make_new();
+ list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/, conf->home_dir);
+ close_DB(&db);
+ if (list_size(paths) && list_size(opt->args)) {
+ do {
+ FILE *cmd;
+ char result[STR_L] = "";
+ char command[STR_L] = "grep -H -n \"";
+ strcat(command, list_get(opt->args));
+ strcat(command, "\" ");
+ strcat(command, list_get(paths));
+ cmd = popen(command, "r");
+ if (cmd == NULL) {
+ list_free(paths);
+ return -1;
+ }
+ while (fgets(result, sizeof(result), cmd)) {
+ printf("%s", result);
+ }
+ pclose(cmd);
+ } while(list_next(paths));
+ }
+ list_free(paths);
+ return 0;
+}
+
+void print_SEARCH_help() {
+ HELP("ck search SEARCH_TERM");
+}