From 17e86f98a23707e37e4b643e243e9bc0cecebc0a Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 20 Dec 2018 15:48:10 +0100 Subject: [PATCH] Make UnameHelper() efficient refs #6452 --- lib/base/utility.cpp | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 466a236de..e6582de59 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -49,6 +49,7 @@ #ifndef _WIN32 # include +# include # include # include # include @@ -1463,28 +1464,23 @@ String Utility::UnescapeString(const String& s) #ifndef _WIN32 static String UnameHelper(char type) { - /* Unfortunately the uname() system call doesn't support some of the - * query types we're interested in - so we're using popen() instead. */ - - char cmd[] = "uname -X 2>&1"; - cmd[7] = type; - - FILE *fp = popen(cmd, "r"); - - if (!fp) - return "Unknown"; - - char line[1024]; - std::ostringstream msgbuf; - - while (fgets(line, sizeof(line), fp)) - msgbuf << line; - - pclose(fp); - - String result = msgbuf.str(); - - return result.Trim(); + struct utsname name; + uname(&name); + + switch (type) { + case 'm': + return (char*)name.machine; + case 'n': + return (char*)name.nodename; + case 'r': + return (char*)name.release; + case 's': + return (char*)name.sysname; + case 'v': + return (char*)name.version; + default: + VERIFY(!"Invalid uname query."); + } } #endif /* _WIN32 */ static bool ReleaseHelper(String *platformName, String *platformVersion) -- 2.40.0