Make UnameHelper() efficient

refs #6452
This commit is contained in:
Alexander A. Klimov 2018-12-20 15:48:10 +01:00
parent c4d57afa3d
commit 17e86f98a2

View File

@ -49,6 +49,7 @@
#ifndef _WIN32 #ifndef _WIN32
# include <sys/types.h> # include <sys/types.h>
# include <sys/utsname.h>
# include <pwd.h> # include <pwd.h>
# include <grp.h> # include <grp.h>
# include <errno.h> # include <errno.h>
@ -1463,28 +1464,23 @@ String Utility::UnescapeString(const String& s)
#ifndef _WIN32 #ifndef _WIN32
static String UnameHelper(char type) static String UnameHelper(char type)
{ {
/* Unfortunately the uname() system call doesn't support some of the struct utsname name;
* query types we're interested in - so we're using popen() instead. */ uname(&name);
char cmd[] = "uname -X 2>&1"; switch (type) {
cmd[7] = type; case 'm':
return (char*)name.machine;
FILE *fp = popen(cmd, "r"); case 'n':
return (char*)name.nodename;
if (!fp) case 'r':
return "Unknown"; return (char*)name.release;
case 's':
char line[1024]; return (char*)name.sysname;
std::ostringstream msgbuf; case 'v':
return (char*)name.version;
while (fgets(line, sizeof(line), fp)) default:
msgbuf << line; VERIFY(!"Invalid uname query.");
}
pclose(fp);
String result = msgbuf.str();
return result.Trim();
} }
#endif /* _WIN32 */ #endif /* _WIN32 */
static bool ReleaseHelper(String *platformName, String *platformVersion) static bool ReleaseHelper(String *platformName, String *platformVersion)