]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Save history to home-dir 4779/head
authorPieter Lexis <pieter.lexis@powerdns.com>
Fri, 16 Dec 2016 15:28:51 +0000 (16:28 +0100)
committerPieter Lexis <pieter.lexis@powerdns.com>
Tue, 20 Dec 2016 11:51:03 +0000 (12:51 +0100)
Closes #4562

pdns/dnsdist-console.cc

index 45052f08db4997a28e8a44a9006a86186a6473b1..fdd4524d898f1aebb0cca045019a7612f8874998 100644 (file)
@@ -21,6 +21,7 @@
  */
 #include "dnsdist.hh"
 #include "sodcrypto.hh"
+#include "pwd.h"
 
 #if defined (__OpenBSD__)
 #include <readline/readline.h>
@@ -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<string> 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<string> 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("> ");