From 646ba943a1553267087f11742e904ee885003dc6 Mon Sep 17 00:00:00 2001 From: Wvader <34067397+wvader@users.noreply.github.com> Date: Wed, 2 Nov 2022 18:27:10 +0000 Subject: [PATCH] reading tree example --- main.cpp | 90 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 26 deletions(-) diff --git a/main.cpp b/main.cpp index 7c5711b..2cdb0cb 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,14 @@ #include #include #include - +#include +#include +#include // This file is currently for testing and training using augeas with C++ // https://github.com/hercules-team/augeas/blob/master/tests/test-api.c static const char *abs_top_srcdir; - static const char *root = "../root"; - -// Puth the Augeas lens path here static const char *loadpath = "/opt/homebrew/share/augeas/lenses/dist"; @@ -37,21 +36,6 @@ static void readHostsFile() { char *etc_hosts_fn = NULL; FILE *hosts_fp = NULL; char *hosts_txt = NULL; - int readsz = 0; - - /* Read the original contents of the etc/hosts file */ - if (asprintf(&etc_hosts_fn, "%s/etc/hosts", root) >= 0) { - hosts_fp = fopen(etc_hosts_fn, "r"); - if (hosts_fp) { - hosts_txt = static_cast(calloc(sizeof(char), 4096)); - if (hosts_txt) { - readsz = fread(hosts_txt, sizeof(char), 4096, hosts_fp); - *(hosts_txt + readsz) = '\0'; - } - fclose(hosts_fp); - } - free(etc_hosts_fn); - } aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD); @@ -65,31 +49,85 @@ static void readHostsFile() { aug_close(aug); } -static void readHostsTree() { +static void printHostsText() { struct augeas *aug; int r; char *s; char *etc_hosts_fn = NULL; - FILE *hosts_fp = NULL; - char *hosts_txt = NULL; - int readsz = 0; aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD); r = aug_load_file(aug, "/etc/hosts"); r = aug_preview(aug, "/files/etc/hosts/1", &s); - std::cout << s << std::endl; - free(hosts_txt); free(s); aug_close(aug); } +/// Get the tree of the /etc/hosts file printed in the console +/// Implementation based on https://github.com/Nexenta/node-augeas/blob/756276c432cc9e506836800ac9d217bedbbef55c/libaugeas.cc#L784 + +static void readHostsTree() { + struct augeas *aug; + int r; + FILE *out = tmpfile(); + char *etc_hosts_fn = NULL; + + aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD); + + const std::string matchPath = "/files/etc/hosts/*"; + + r = aug_load_file(aug, "/etc/hosts"); + r = aug_print(aug, out,"/files/etc/hosts/*"); + + std::map stdBindList; + std::map ::iterator pos; + + char line[256]; + rewind(out); + while (fgets(line, 256, out) != NULL) { + // remove end of line + line[strlen(line) - 1] = '\0'; + std::string s = line; + ; + // skip comments + if (s.find("#comment") != std::string::npos) + continue; + s = s.substr(matchPath.length() - 1); + // split by '=' sign + size_t eqpos = s.find(" = "); + if (eqpos == std::string::npos) + continue; + // extract key and value + std::string key = s.substr(0, eqpos); + std::string value = s.substr(eqpos + 3); + // remove '"' sign from around value + value.erase(value.begin()); + value.erase(value.end() - 1); + stdBindList.insert(std::pair(key,value)); // 2 + + } + + + fclose(out); + + for (pos = stdBindList.begin();pos!=stdBindList.end();pos++) + { + std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl; + } + + aug_close(aug); + + + +} + + int main(int argc, char *argv[]) { testGet(); - readHostsFile(); + readHostsTree(); return 0; } \ No newline at end of file