summaryrefslogtreecommitdiff
path: root/lib/uname.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2009-10-03 20:34:12 +0200
committerBruno Haible <bruno@clisp.org>2009-10-03 20:34:12 +0200
commit2fbaa37725d14ebeae463fbe7e2ed912327f85a5 (patch)
tree009e8192df14729dd522226abfe5e7d69fa4b7eb /lib/uname.c
parenta5665a875890a79d9e207c5cd3d86f13f7c90bd8 (diff)
downloadgnulib-2fbaa37725d14ebeae463fbe7e2ed912327f85a5.tar.gz
Do only one call to GetVersionEx in the common case.
Diffstat (limited to 'lib/uname.c')
-rw-r--r--lib/uname.c28
1 files changed, 20 insertions, 8 deletions
diff --git a/lib/uname.c b/lib/uname.c
index 70ebb95e51..95c577698c 100644
--- a/lib/uname.c
+++ b/lib/uname.c
@@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <windows.h>
@@ -49,16 +50,31 @@ int
uname (struct utsname *buf)
{
OSVERSIONINFO version;
+ OSVERSIONINFOEX versionex;
+ BOOL have_versionex; /* indicates whether versionex is filled */
const char *super_version;
+ /* Preparation: Fill version and, if possible, also versionex.
+ But try to call GetVersionEx only once in the common case. */
+ versionex.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
+ have_versionex = GetVersionEx (&versionex);
+ if (have_versionex)
+ {
+ /* We know that OSVERSIONINFO is a subset of OSVERSIONINFOEX. */
+ memcpy (&version, &versionex, sizeof (OSVERSIONINFO));
+ }
+ else
+ {
+ version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
+ if (!GetVersionEx (&version))
+ abort ();
+ }
+
/* Fill in nodename. */
if (gethostname (buf->nodename, sizeof (buf->nodename)) < 0)
strcpy (buf->nodename, "localhost");
/* Determine major-major Windows version. */
- version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
- if (!GetVersionEx (&version))
- abort ();
if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
/* Windows NT or newer. */
@@ -135,11 +151,7 @@ uname (struct utsname *buf)
}
else if (version.dwMajorVersion == 6)
{
- OSVERSIONINFOEX versionex;
-
- versionex.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
- if (GetVersionEx ((OSVERSIONINFO *) &versionex)
- && versionex.wProductType != VER_NT_WORKSTATION)
+ if (have_versionex && versionex.wProductType != VER_NT_WORKSTATION)
strcpy (buf->release, "Windows Server 2008");
else
switch (version.dwMinorVersion)