From 9364403925f5e6a60b067e62bca71a2a62949cdf Mon Sep 17 00:00:00 2001 From: Pieter Lexis Date: Fri, 16 Dec 2016 16:28:51 +0100 Subject: [PATCH] dnsdist: Save history to home-dir Closes #4562 --- pdns/dnsdist-console.cc | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index 45052f08d..fdd4524d8 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("> "); -- 2.40.0