xref: /minix3/external/bsd/bind/dist/lib/isc/win32/win32os.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: win32os.c,v 1.6 2015/07/08 17:29:00 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007, 2013-2015  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 #include <windows.h>
21 
22 #ifndef TESTVERSION
23 #include <isc/win32os.h>
24 #else
25 #include <stdio.h>
26 #endif
27 
28 int
isc_win32os_versioncheck(unsigned int major,unsigned int minor,unsigned int spmajor,unsigned int spminor)29 isc_win32os_versioncheck(unsigned int major, unsigned int minor,
30 			 unsigned int spmajor, unsigned int spminor)
31 {
32 	OSVERSIONINFOEX osVer;
33 	DWORD typeMask;
34 	ULONGLONG conditionMask;
35 
36 	memset(&osVer, 0, sizeof(OSVERSIONINFOEX));
37 	osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
38 	typeMask = 0;
39 	conditionMask = 0;
40 
41 	/* Optimistic: likely greater */
42 	osVer.dwMajorVersion = major;
43 	typeMask |= VER_MAJORVERSION;
44 	conditionMask = VerSetConditionMask(conditionMask,
45 					    VER_MAJORVERSION,
46 					    VER_GREATER);
47 	osVer.dwMinorVersion = minor;
48 	typeMask |= VER_MINORVERSION;
49 	conditionMask = VerSetConditionMask(conditionMask,
50 					    VER_MINORVERSION,
51 					    VER_GREATER);
52 	osVer.wServicePackMajor = spmajor;
53 	typeMask |= VER_SERVICEPACKMAJOR;
54 	conditionMask = VerSetConditionMask(conditionMask,
55 					    VER_SERVICEPACKMAJOR,
56 					    VER_GREATER);
57 	osVer.wServicePackMinor = spminor;
58 	typeMask |= VER_SERVICEPACKMINOR;
59 	conditionMask = VerSetConditionMask(conditionMask,
60 					    VER_SERVICEPACKMINOR,
61 					    VER_GREATER);
62 	if (VerifyVersionInfo(&osVer, typeMask, conditionMask))
63 		return (1);
64 
65 	/* Failed: retry with equal */
66 	conditionMask = 0;
67 	conditionMask = VerSetConditionMask(conditionMask,
68 					    VER_MAJORVERSION,
69 					    VER_EQUAL);
70 	conditionMask = VerSetConditionMask(conditionMask,
71 					    VER_MINORVERSION,
72 					    VER_EQUAL);
73 	conditionMask = VerSetConditionMask(conditionMask,
74 					    VER_SERVICEPACKMAJOR,
75 					    VER_EQUAL);
76 	conditionMask = VerSetConditionMask(conditionMask,
77 					    VER_SERVICEPACKMINOR,
78 					    VER_EQUAL);
79 	if (VerifyVersionInfo(&osVer, typeMask, conditionMask))
80 		return (0);
81 	else
82 		return (-1);
83 }
84 
85 #ifdef TESTVERSION
86 int
main(int argc,char ** argv)87 main(int argc, char **argv) {
88 	unsigned int major = 0;
89 	unsigned int minor = 0;
90 	unsigned int spmajor = 0;
91 	unsigned int spminor = 0;
92 	int ret;
93 
94 	if (argc > 1) {
95 		--argc;
96 		++argv;
97 		major = (unsigned int) atoi(argv[0]);
98 	}
99 	if (argc > 1) {
100 		--argc;
101 		++argv;
102 		minor = (unsigned int) atoi(argv[0]);
103 	}
104 	if (argc > 1) {
105 		--argc;
106 		++argv;
107 		spmajor = (unsigned int) atoi(argv[0]);
108 	}
109 	if (argc > 1) {
110 		--argc;
111 		++argv;
112 		spminor = (unsigned int) atoi(argv[0]);
113 	}
114 
115 	ret = isc_win32os_versioncheck(major, minor, spmajor, spminor);
116 
117 	printf("%s major %u minor %u SP major %u SP minor %u\n",
118 	       ret > 0 ? "greater" : (ret == 0 ? "equal" : "less"),
119 	       major, minor, spmajor, spminor);
120 	return (ret);
121 }
122 #endif
123