xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/win32/win32os.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: win32os.c,v 1.2 2024/08/18 20:47:16 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos /*
4897be3a4Schristos  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
5897be3a4Schristos  * Copyright (C) 2002  Internet Software Consortium.
6897be3a4Schristos  *
7897be3a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8897be3a4Schristos  * purpose with or without fee is hereby granted, provided that the above
9897be3a4Schristos  * copyright notice and this permission notice appear in all copies.
10897be3a4Schristos  *
11897be3a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12897be3a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13897be3a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14897be3a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15897be3a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16897be3a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17897be3a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18897be3a4Schristos  */
19897be3a4Schristos 
20897be3a4Schristos /* Id: win32os.c,v 1.5 2007/06/19 23:47:19 tbox Exp  */
21897be3a4Schristos 
22897be3a4Schristos #include <windows.h>
23897be3a4Schristos 
24897be3a4Schristos #include <isc/win32os.h>
25897be3a4Schristos 
26897be3a4Schristos static BOOL bInit = FALSE;
27897be3a4Schristos static OSVERSIONINFOEX osVer;
28897be3a4Schristos 
29897be3a4Schristos static void
30897be3a4Schristos initialize_action(void) {
31897be3a4Schristos 	BOOL bSuccess;
32897be3a4Schristos 
33897be3a4Schristos 	if (bInit)
34897be3a4Schristos 		return;
35897be3a4Schristos 	/*
36897be3a4Schristos 	 * NOTE: VC++ 6.0 gets this function declaration wrong
37897be3a4Schristos 	 * so we compensate by casting the argument
38897be3a4Schristos 	 */
39897be3a4Schristos 	osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
40897be3a4Schristos 	bSuccess = GetVersionEx((OSVERSIONINFO *) &osVer);
41897be3a4Schristos 
42897be3a4Schristos 	/*
43897be3a4Schristos 	 * Versions of NT before NT4.0 SP6 did not return the
44897be3a4Schristos 	 * extra info that the EX structure provides and returns
45897be3a4Schristos 	 * a failure so we need to retry with the old structure.
46897be3a4Schristos 	 */
47897be3a4Schristos 	if(!bSuccess) {
48897be3a4Schristos 		osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
49897be3a4Schristos 		bSuccess = GetVersionEx((OSVERSIONINFO *) &osVer);
50897be3a4Schristos 	}
51897be3a4Schristos 	bInit = TRUE;
52897be3a4Schristos }
53897be3a4Schristos 
54897be3a4Schristos unsigned int
55897be3a4Schristos isc_win32os_majorversion(void) {
56897be3a4Schristos 	initialize_action();
57897be3a4Schristos 	return ((unsigned int)osVer.dwMajorVersion);
58897be3a4Schristos }
59897be3a4Schristos 
60897be3a4Schristos unsigned int
61897be3a4Schristos isc_win32os_minorversion(void) {
62897be3a4Schristos 	initialize_action();
63897be3a4Schristos 	return ((unsigned int)osVer.dwMinorVersion);
64897be3a4Schristos }
65897be3a4Schristos 
66897be3a4Schristos unsigned int
67897be3a4Schristos isc_win32os_servicepackmajor(void) {
68897be3a4Schristos 	initialize_action();
69897be3a4Schristos 	return ((unsigned int)osVer.wServicePackMajor);
70897be3a4Schristos }
71897be3a4Schristos 
72897be3a4Schristos unsigned int
73897be3a4Schristos isc_win32os_servicepackminor(void) {
74897be3a4Schristos 	initialize_action();
75897be3a4Schristos 	return ((unsigned int)osVer.wServicePackMinor);
76897be3a4Schristos }
77897be3a4Schristos 
78897be3a4Schristos int
79897be3a4Schristos isc_win32os_versioncheck(unsigned int major, unsigned int minor,
80897be3a4Schristos 		     unsigned int spmajor, unsigned int spminor) {
81897be3a4Schristos 
82897be3a4Schristos 	initialize_action();
83897be3a4Schristos 
84897be3a4Schristos 	if (major < isc_win32os_majorversion())
85897be3a4Schristos 		return (1);
86897be3a4Schristos 	if (major > isc_win32os_majorversion())
87897be3a4Schristos 		return (-1);
88897be3a4Schristos 	if (minor < isc_win32os_minorversion())
89897be3a4Schristos 		return (1);
90897be3a4Schristos 	if (minor > isc_win32os_minorversion())
91897be3a4Schristos 		return (-1);
92897be3a4Schristos 	if (spmajor < isc_win32os_servicepackmajor())
93897be3a4Schristos 		return (1);
94897be3a4Schristos 	if (spmajor > isc_win32os_servicepackmajor())
95897be3a4Schristos 		return (-1);
96897be3a4Schristos 	if (spminor < isc_win32os_servicepackminor())
97897be3a4Schristos 		return (1);
98897be3a4Schristos 	if (spminor > isc_win32os_servicepackminor())
99897be3a4Schristos 		return (-1);
100897be3a4Schristos 
101897be3a4Schristos 	/* Exact */
102897be3a4Schristos 	return (0);
103897be3a4Schristos }