1258945Sroberto/*
2258945Sroberto * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 2002  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18258945Sroberto/* $Id: win32os.c,v 1.5 2007/06/19 23:47:19 tbox Exp $ */
19258945Sroberto
20258945Sroberto#include <windows.h>
21258945Sroberto
22258945Sroberto#include <isc/win32os.h>
23258945Sroberto
24258945Srobertostatic BOOL bInit = FALSE;
25258945Srobertostatic OSVERSIONINFOEX osVer;
26258945Sroberto
27258945Srobertostatic void
28258945Srobertoinitialize_action(void) {
29258945Sroberto	BOOL bSuccess;
30258945Sroberto
31258945Sroberto	if (bInit)
32258945Sroberto		return;
33258945Sroberto	/*
34258945Sroberto	 * NOTE: VC++ 6.0 gets this function declaration wrong
35258945Sroberto	 * so we compensate by casting the argument
36258945Sroberto	 */
37258945Sroberto	osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
38258945Sroberto	bSuccess = GetVersionEx((OSVERSIONINFO *) &osVer);
39258945Sroberto
40258945Sroberto	/*
41258945Sroberto	 * Versions of NT before NT4.0 SP6 did not return the
42258945Sroberto	 * extra info that the EX structure provides and returns
43258945Sroberto	 * a failure so we need to retry with the old structure.
44258945Sroberto	 */
45258945Sroberto	if(!bSuccess) {
46258945Sroberto		osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
47258945Sroberto		bSuccess = GetVersionEx((OSVERSIONINFO *) &osVer);
48258945Sroberto	}
49258945Sroberto	bInit = TRUE;
50258945Sroberto}
51258945Sroberto
52258945Srobertounsigned int
53258945Srobertoisc_win32os_majorversion(void) {
54258945Sroberto	initialize_action();
55258945Sroberto	return ((unsigned int)osVer.dwMajorVersion);
56258945Sroberto}
57258945Sroberto
58258945Srobertounsigned int
59258945Srobertoisc_win32os_minorversion(void) {
60258945Sroberto	initialize_action();
61258945Sroberto	return ((unsigned int)osVer.dwMinorVersion);
62258945Sroberto}
63258945Sroberto
64258945Srobertounsigned int
65258945Srobertoisc_win32os_servicepackmajor(void) {
66258945Sroberto	initialize_action();
67258945Sroberto	return ((unsigned int)osVer.wServicePackMajor);
68258945Sroberto}
69258945Sroberto
70258945Srobertounsigned int
71258945Srobertoisc_win32os_servicepackminor(void) {
72258945Sroberto	initialize_action();
73258945Sroberto	return ((unsigned int)osVer.wServicePackMinor);
74258945Sroberto}
75258945Sroberto
76258945Srobertoint
77258945Srobertoisc_win32os_versioncheck(unsigned int major, unsigned int minor,
78258945Sroberto		     unsigned int spmajor, unsigned int spminor) {
79258945Sroberto
80258945Sroberto	initialize_action();
81258945Sroberto
82258945Sroberto	if (major < isc_win32os_majorversion())
83258945Sroberto		return (1);
84258945Sroberto	if (major > isc_win32os_majorversion())
85258945Sroberto		return (-1);
86258945Sroberto	if (minor < isc_win32os_minorversion())
87258945Sroberto		return (1);
88258945Sroberto	if (minor > isc_win32os_minorversion())
89258945Sroberto		return (-1);
90258945Sroberto	if (spmajor < isc_win32os_servicepackmajor())
91258945Sroberto		return (1);
92258945Sroberto	if (spmajor > isc_win32os_servicepackmajor())
93258945Sroberto		return (-1);
94258945Sroberto	if (spminor < isc_win32os_servicepackminor())
95258945Sroberto		return (1);
96258945Sroberto	if (spminor > isc_win32os_servicepackminor())
97258945Sroberto		return (-1);
98258945Sroberto
99258945Sroberto	/* Exact */
100258945Sroberto	return (0);
101258945Sroberto}