diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-11-17 19:49:52 +0200 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-11-17 19:49:52 +0200 |
commit | 21c92f735ff52ff98b50f7f9d8e8ab9c46dad557 (patch) | |
tree | 3b244ec29c3b3ad96fc141461bff4d141a36227a /src/search.c | |
parent | 8eb4df24eb30353bea82268440396e50ed8b1bbc (diff) | |
download | ck-21c92f735ff52ff98b50f7f9d8e8ab9c46dad557.tar.gz ck-21c92f735ff52ff98b50f7f9d8e8ab9c46dad557.tar.bz2 ck-21c92f735ff52ff98b50f7f9d8e8ab9c46dad557.zip |
Finish restructure and simplify include graph
Diffstat (limited to 'src/search.c')
-rw-r--r-- | src/search.c | 54 |
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"); +} |