diff options
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/include/tools.h | 17 | ||||
-rw-r--r-- | src/session-initialize-bincimap-up.cc | 11 | ||||
-rw-r--r-- | src/session-initialize-bincimapd.cc | 13 | ||||
-rw-r--r-- | src/session.cc | 10 | ||||
-rw-r--r-- | src/tools.cc | 31 |
6 files changed, 23 insertions, 61 deletions
diff --git a/src/Makefile b/src/Makefile index de9d16f..aff63b7 100644 --- a/src/Makefile +++ b/src/Makefile @@ -41,7 +41,7 @@ bincimap_updatecache_OBJECTS = bincimap-updatecache.o address.o argparser.o \ maildir-writecache.o maildir-delete.o maildirmessage.o mime.o \ mime-parsefull.o mime-parseonlyheader.o mime-printdoc.o \ mime-printbody.o mime-printheader.o mime-getpart.o pendingupdates.o \ - session.o status.o + session.o status.o tools.o .PHONY: default default: it-base diff --git a/src/include/tools.h b/src/include/tools.h index f70486f..1d86fca 100644 --- a/src/include/tools.h +++ b/src/include/tools.h @@ -5,19 +5,10 @@ * @date 2002-2005 */ +#include <optional> #include <string> -namespace Binc { - - class Tools { - private: - Tools(); - - public: - void setenv(const std::string &key, const std::string &value) const; - std::string getenv(const std::string &key) const; - - static Tools &getInstance(); - }; - +namespace Binc::Tools { + void setenv(const std::string &key, const std::string &value); + std::optional<std::string> getenv(const std::string &key); } diff --git a/src/session-initialize-bincimap-up.cc b/src/session-initialize-bincimap-up.cc index c18ef26..a17a171 100644 --- a/src/session-initialize-bincimap-up.cc +++ b/src/session-initialize-bincimap-up.cc @@ -27,8 +27,6 @@ using namespace Binc; using std::string; -extern char **environ; - bool Session::initialize(int argc, char *argv[]) { IOFactory &ioFactory = IOFactory::getInstance(); @@ -61,9 +59,8 @@ bool Session::initialize(int argc, char *argv[]) session.assignCommandLineArgs(); // for log input - string ip = getenv("TCP6REMOTEIP") ? getenv("TCP6REMOTEIP") - : getenv("TCPREMOTEIP") ? getenv("TCPREMOTEIP") - : "?"; + string ip = Tools::getenv("TCP6REMOTEIP") + .value_or(Tools::getenv("TCPREMOTEIP").value_or("?")); session.setIP(ip); string logtype = session.getEnv("LOG_TYPE"); @@ -113,11 +110,11 @@ bool Session::initialize(int argc, char *argv[]) // imaps (port 993) -- requires sslserver with option -e int stls = 0; - if (getenv("SSL_SESSION_ID")) { + if (Tools::getenv("SSL_SESSION_ID")) { session.command.ssl = true; stls = -1; // else we will do starttls - requires new FDs - } else if (getenv("UCSPITLS")) { + } else if (Tools::getenv("UCSPITLS")) { string ucspitls = session.getEnv("UCSPITLS"); if (ucspitls == "+") stls = 1; if (ucspitls == "-") stls = 0; diff --git a/src/session-initialize-bincimapd.cc b/src/session-initialize-bincimapd.cc index 871510b..e98f0af 100644 --- a/src/session-initialize-bincimapd.cc +++ b/src/session-initialize-bincimapd.cc @@ -32,8 +32,6 @@ using namespace Binc; using std::endl; using std::string; -extern char **environ; - bool Session::initialize(int argc, char *argv[]) { IOFactory &ioFactory = IOFactory::getInstance(); @@ -65,9 +63,8 @@ bool Session::initialize(int argc, char *argv[]) session.assignCommandLineArgs(); // log settings - string ip = getenv("TCP6REMOTEIP") ? getenv("TCP6REMOTEIP") - : getenv("TCPREMOTEIP") ? getenv("TCPREMOTEIP") - : "?"; + string ip = Tools::getenv("TCP6REMOTEIP") + .value_or(Tools::getenv("TCPREMOTEIP").value_or("?")); session.setIP(ip); string logtype = session.getEnv("LOG_TYPE"); @@ -116,8 +113,8 @@ bool Session::initialize(int argc, char *argv[]) string pid = std::to_string(session.getPid()); - char *logindetails = getenv("BINCIMAP_LOGIN"); - if (logindetails == nullptr) { + auto logindetails = Tools::getenv("BINCIMAP_LOGIN"); + if (!logindetails) { bincLog << "bincimapd: pid " << pid << " BINCIMAP_LOGIN missing from environment (are you sure you invoked " << argv[0] << " properly?)\n"; @@ -199,7 +196,7 @@ bool Session::initialize(int argc, char *argv[]) session.setState(Session::AUTHENTICATED); - const string details = logindetails; + const string details = *logindetails; string::size_type det = details.find('+'); if (det == string::npos) { bincLog << "bincimapd: pid " << pid diff --git a/src/session.cc b/src/session.cc index aa09abe..0ae87df 100644 --- a/src/session.cc +++ b/src/session.cc @@ -19,8 +19,6 @@ using namespace Binc; using std::string; -extern char **environ; - Session::Session() { readbytes = 0; @@ -192,17 +190,15 @@ int Session::timeout() const bool Session::hasEnv(const string &key) const { - return getenv(key.c_str()) != nullptr; + return Tools::getenv(key).has_value(); } string Session::getEnv(const string &key) { - char *c = getenv(key.c_str()); - return c ? c : ""; + return Tools::getenv(key).value_or(string{}); } void Session::setEnv(const string &key, const string &value) { - string env = key + "=" + value; - putenv(strdup(env.c_str())); + Tools::setenv(key, value); } diff --git a/src/tools.cc b/src/tools.cc index abc0e5f..f4e6835 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -7,34 +7,15 @@ #include "tools.h" -#include <cstring> +#include <stdlib.h> -#include <errno.h> - -using namespace Binc; -using std::string; - -Tools::Tools() {} - -Tools &Tools::getInstance() +void Binc::Tools::setenv(const std::string &key, const std::string &value) { - static Tools tools; - return tools; + ::setenv(key.c_str(), value.c_str(), 1); } -void Tools::setenv(const string &key, const string &value) const +std::optional<std::string> Binc::Tools::getenv(const std::string &key) { - char *c = strdup((key + "=" + value).c_str()); - putenv(c); -} - -string Tools::getenv(const string &key) const -{ - static const string NIL = ""; - - const char *c = ::getenv((char *)key.c_str()); - if (c == nullptr) - return NIL; - else - return string(c); + const char *c = ::getenv(key.c_str()); + return c ? std::make_optional(c) : std::nullopt; } |