xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/win32/win32os.c (revision 4439cfd0acf9c7dc90625e5cd83b2317a9ab8967)
1 /*	$NetBSD: win32os.c,v 1.2 2024/08/18 20:47:16 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2002  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: win32os.c,v 1.5 2007/06/19 23:47:19 tbox Exp  */
21 
22 #include <windows.h>
23 
24 #include <isc/win32os.h>
25 
26 static BOOL bInit = FALSE;
27 static OSVERSIONINFOEX osVer;
28 
29 static void
30 initialize_action(void) {
31 	BOOL bSuccess;
32 
33 	if (bInit)
34 		return;
35 	/*
36 	 * NOTE: VC++ 6.0 gets this function declaration wrong
37 	 * so we compensate by casting the argument
38 	 */
39 	osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
40 	bSuccess = GetVersionEx((OSVERSIONINFO *) &osVer);
41 
42 	/*
43 	 * Versions of NT before NT4.0 SP6 did not return the
44 	 * extra info that the EX structure provides and returns
45 	 * a failure so we need to retry with the old structure.
46 	 */
47 	if(!bSuccess) {
48 		osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
49 		bSuccess = GetVersionEx((OSVERSIONINFO *) &osVer);
50 	}
51 	bInit = TRUE;
52 }
53 
54 unsigned int
55 isc_win32os_majorversion(void) {
56 	initialize_action();
57 	return ((unsigned int)osVer.dwMajorVersion);
58 }
59 
60 unsigned int
61 isc_win32os_minorversion(void) {
62 	initialize_action();
63 	return ((unsigned int)osVer.dwMinorVersion);
64 }
65 
66 unsigned int
67 isc_win32os_servicepackmajor(void) {
68 	initialize_action();
69 	return ((unsigned int)osVer.wServicePackMajor);
70 }
71 
72 unsigned int
73 isc_win32os_servicepackminor(void) {
74 	initialize_action();
75 	return ((unsigned int)osVer.wServicePackMinor);
76 }
77 
78 int
79 isc_win32os_versioncheck(unsigned int major, unsigned int minor,
80 		     unsigned int spmajor, unsigned int spminor) {
81 
82 	initialize_action();
83 
84 	if (major < isc_win32os_majorversion())
85 		return (1);
86 	if (major > isc_win32os_majorversion())
87 		return (-1);
88 	if (minor < isc_win32os_minorversion())
89 		return (1);
90 	if (minor > isc_win32os_minorversion())
91 		return (-1);
92 	if (spmajor < isc_win32os_servicepackmajor())
93 		return (1);
94 	if (spmajor > isc_win32os_servicepackmajor())
95 		return (-1);
96 	if (spminor < isc_win32os_servicepackminor())
97 		return (1);
98 	if (spminor > isc_win32os_servicepackminor())
99 		return (-1);
100 
101 	/* Exact */
102 	return (0);
103 }