133 lines
3.2 KiB
C++
133 lines
3.2 KiB
C++
#include <iostream>
|
|
#include <exception>
|
|
#include <augeas.h>
|
|
#include <stdio.h>
|
|
#include <vector>
|
|
#include <map>
|
|
// 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";
|
|
static const char *loadpath = "/opt/homebrew/share/augeas/lenses/dist";
|
|
|
|
|
|
static void testGet() {
|
|
int r;
|
|
const char *value;
|
|
const char *label;
|
|
struct augeas *aug;
|
|
char *s;
|
|
|
|
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
|
|
|
|
r = aug_load_file(aug, "/etc/hosts");
|
|
r = aug_source(aug, "/files/etc/hosts/1", &s);
|
|
|
|
std::cout << "The value read from node: " << s << std::endl;
|
|
|
|
aug_close(aug);
|
|
}
|
|
|
|
static void readHostsFile() {
|
|
struct augeas *aug;
|
|
int r;
|
|
char *s;
|
|
char *etc_hosts_fn = NULL;
|
|
FILE *hosts_fp = NULL;
|
|
char *hosts_txt = NULL;
|
|
|
|
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);
|
|
}
|
|
|
|
static void printHostsText() {
|
|
struct augeas *aug;
|
|
int r;
|
|
char *s;
|
|
char *etc_hosts_fn = NULL;
|
|
|
|
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(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 <std::string, std::string> stdBindList;
|
|
std::map <std::string , std::string>::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<std::string,std::string>(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();
|
|
readHostsTree();
|
|
return 0;
|
|
} |