diff --git a/catalogue/CmdLineTool.cpp b/catalogue/CmdLineTool.cpp index d0847f2ebafc18f261c36fb69f37b5a9f4509131..e70d90ae2b0e7f146c445b6b4ad5196ca4d5333f 100644 --- a/catalogue/CmdLineTool.cpp +++ b/catalogue/CmdLineTool.cpp @@ -44,28 +44,24 @@ CmdLineTool::~CmdLineTool() = default; // getUsername //------------------------------------------------------------------------------ std::string CmdLineTool::getUsername() { - std::string buf(256, '\0'); + char buf[256]; - if(getlogin_r(&buf[0], buf.size())) { + if (getlogin_r(buf, sizeof(buf)) != 0) { return "UNKNOWN"; - } else { - buf.resize(strlen(buf.c_str())); // Resize the string to remove extra null characters - return buf; } + return std::string(buf); } //------------------------------------------------------------------------------ // getHostname //------------------------------------------------------------------------------ std::string CmdLineTool::getHostname() { - std::string buf(256, '\0'); + char buf[256]; - if(gethostname(&buf[0], buf.size())) { + if (gethostname(buf, sizeof(buf)) != 0) { return "UNKNOWN"; - } else { - buf.resize(strlen(buf.c_str())); // Resize the string to remove extra null characters - return buf; } + return std::string(buf); } //------------------------------------------------------------------------------ diff --git a/common/CmdLineTool.cpp b/common/CmdLineTool.cpp index 1565b03c3f85dacdb2a39d22cc0aafd8df9364cd..72bf67e32c2469652b8db21e5fd294ec5bfa0390 100644 --- a/common/CmdLineTool.cpp +++ b/common/CmdLineTool.cpp @@ -46,11 +46,10 @@ CmdLineTool::~CmdLineTool() = default; std::string CmdLineTool::getUsername() { char buf[256]; - if(getlogin_r(buf, sizeof(buf))) { + if (getlogin_r(buf, sizeof(buf)) != 0) { return "UNKNOWN"; - } else { - return buf; } + return std::string(buf); } //------------------------------------------------------------------------------ @@ -59,12 +58,10 @@ std::string CmdLineTool::getUsername() { std::string CmdLineTool::getHostname() { char buf[256]; - if(gethostname(buf, sizeof(buf))) { + if (gethostname(buf, sizeof(buf)) != 0) { return "UNKNOWN"; - } else { - buf[sizeof(buf) - 1] = '\0'; - return buf; } + return std::string(buf); } //------------------------------------------------------------------------------