From: Pieter Lexis Date: Fri, 16 Dec 2016 15:28:51 +0000 (+0100) Subject: dnsdist: Save history to home-dir X-Git-Tag: rec-4.1.0-alpha1~329^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F4779%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Save history to home-dir Closes #4562 --- diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index 45052f08db..fdd4524d89 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -21,6 +21,7 @@ */ #include "dnsdist.hh" #include "sodcrypto.hh" +#include "pwd.h" #if defined (__OpenBSD__) #include @@ -45,6 +46,26 @@ void feedConfigDelta(const std::string& line) g_confDelta.push_back({now,line}); } +string historyFile(const bool &ignoreHOME = false) +{ + string ret; + + struct passwd pwd; + struct passwd *result; + char buf[16384]; + getpwuid_r(geteuid(), &pwd, buf, sizeof(buf), &result); + + const char *homedir = getenv("HOME"); + if (result) + ret = string(pwd.pw_dir); + if (homedir && !ignoreHOME) // $HOME overrides what the OS tells us + ret = string(homedir); + if (ret.empty()) + ret = "."; // CWD if nothing works.. + ret.append("/.dnsdist_history"); + return ret; +} + void doClient(ComboAddress server, const std::string& command) { if(g_verbose) @@ -85,14 +106,15 @@ void doClient(ComboAddress server, const std::string& command) return; } + string histfile = historyFile(); set dupper; { - ifstream history(".dnsdist_history"); + ifstream history(histfile); string line; while(getline(history, line)) add_history(line.c_str()); } - ofstream history(".dnsdist_history", std::ios_base::app); + ofstream history(histfile, std::ios_base::app); string lastline; for(;;) { char* sline = readline("> "); @@ -142,14 +164,15 @@ void doClient(ComboAddress server, const std::string& command) void doConsole() { + string histfile = historyFile(true); set dupper; { - ifstream history(".dnsdist_history"); + ifstream history(histfile); string line; while(getline(history, line)) add_history(line.c_str()); } - ofstream history(".dnsdist_history", std::ios_base::app); + ofstream history(histfile, std::ios_base::app); string lastline; for(;;) { char* sline = readline("> ");