1 /* @(#)uname.c	1.3 19/09/01 Copyright 2011-2019 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)uname.c	1.3 19/09/01 Copyright 2011-2019 J. Schilling";
6 #endif
7 /*
8  *	uname() replacement in case it does not exist
9  *
10  *	Copyright (c) 2011-2019 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #ifndef	HAVE_UNAME
27 
28 #include <schily/standard.h>
29 #include <schily/utypes.h>
30 #include <schily/utsname.h>
31 #include <schily/schily.h>
32 
33 #if	defined(__MINGW32__) || defined(_MSC_VER)
34 #define	gethostname	__winsock_gethostname
35 #include <schily/windows.h>
36 #undef	gethostname
37 
38 /*
39  * cystyle believes BOOL is a function name and does not like:
40  * typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
41  */
42 typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
43 
44 LOCAL BOOL
iswow64()45 iswow64()
46 {
47 	BOOL			IsWow64 = FALSE;
48 	LPFN_ISWOW64PROCESS	IsWow64Processp;
49 
50 	/*
51 	 * "IsWow64Process" is not supported on all platforms
52 	 */
53 	IsWow64Processp = (LPFN_ISWOW64PROCESS)
54 			GetProcAddress(GetModuleHandle(TEXT("kernel32")),
55 			"IsWow64Process");
56 
57 	if (IsWow64Processp) {
58 		if (!IsWow64Processp(GetCurrentProcess(), &IsWow64))
59 			return (FALSE);
60 	}
61 	return (IsWow64 != 0);
62 }
63 
64 EXPORT int
uname(ubuf)65 uname(ubuf)
66 	struct utsname	*ubuf;
67 {
68 	SYSTEM_INFO	sysinfo;
69 	UInt32_t	version;
70 	UInt32_t	majversion;
71 	UInt32_t	minversion;
72 	UInt32_t	builtnum = 0;
73 	char		dbuf[16];
74 	char		*p;
75 
76 	strlcpy(ubuf->sysname, "Win32", sizeof (ubuf->sysname));
77 	if (iswow64())
78 		strlcat(ubuf->sysname, "-WOW64", sizeof (ubuf->sysname));
79 
80 	gethostname(ubuf->nodename, sizeof (ubuf->nodename));
81 	ubuf->nodename[sizeof (ubuf->nodename)-1] = '\0';
82 
83 	version = GetVersion();
84 	majversion = version & 0xFF;
85 	minversion = (version >> 8) & 0xFF;
86 	if (version < 0x80000000)
87 		builtnum = (version >> 16) & 0xFFFF;
88 
89 	strlcpy(ubuf->release, "NT-0.0", sizeof (ubuf->release));
90 	if (sizeof (ubuf->release) > 7) {
91 		ubuf->release[3] = majversion + '0';
92 		ubuf->release[5] = minversion + '0';
93 	}
94 
95 	p = &dbuf[sizeof (dbuf)-1];
96 	*p = '\0';
97 	do {
98 		*--p = builtnum % 10 + '0';
99 		builtnum /= 10;
100 	} while (builtnum > 0);
101 	strlcpy(ubuf->version, p, sizeof (ubuf->version));
102 
103 	/*
104 	 * cpu, pagesize, numproc, ...
105 	 * See also IsWow64Process()
106 	 */
107 	GetSystemInfo(&sysinfo);
108 
109 	switch (sysinfo.wProcessorArchitecture) {
110 #ifdef	PROCESSOR_ARCHITECTURE_INTEL
111 	case PROCESSOR_ARCHITECTURE_INTEL:
112 		switch (sysinfo.dwProcessorType) {
113 
114 #ifdef	PROCESSOR_INTEL_386
115 		case PROCESSOR_INTEL_386:
116 			/* FALLTHRU */
117 #endif
118 		default:
119 			strlcpy(ubuf->machine, "i386", sizeof (ubuf->machine));
120 			break;
121 #ifdef	PROCESSOR_INTEL_486
122 		case PROCESSOR_INTEL_486:
123 			strlcpy(ubuf->machine, "i486", sizeof (ubuf->machine));
124 			break;
125 #endif
126 #ifdef	PROCESSOR_INTEL_PENTIUM
127 		case PROCESSOR_INTEL_PENTIUM: {
128 				int level = sysinfo.wProcessorLevel;
129 				if (level > 9)
130 					level = 6;
131 				strlcpy(ubuf->machine, "i586", sizeof (ubuf->machine));
132 				if (level > 5)
133 					ubuf->machine[1] = level + '0';
134 			}
135 			break;
136 #endif
137 		}
138 		break;
139 #endif
140 #ifdef	PROCESSOR_ARCHITECTURE_MIPS
141 	case PROCESSOR_ARCHITECTURE_MIPS:
142 		strlcpy(ubuf->machine, "mips", sizeof (ubuf->machine));
143 		break;
144 #endif
145 #ifdef	PROCESSOR_ARCHITECTURE_ALPHA
146 	case PROCESSOR_ARCHITECTURE_ALPHA:
147 		strlcpy(ubuf->machine, "alpha", sizeof (ubuf->machine));
148 		break;
149 #endif
150 #ifdef	PROCESSOR_ARCHITECTURE_PPC
151 	case PROCESSOR_ARCHITECTURE_PPC:
152 		strlcpy(ubuf->machine, "PPC", sizeof (ubuf->machine));
153 		break;
154 #endif
155 #ifdef	PROCESSOR_ARCHITECTURE_SHX
156 	case PROCESSOR_ARCHITECTURE_SHX:
157 		strlcpy(ubuf->machine, "shx", sizeof (ubuf->machine));
158 		break;
159 #endif
160 #ifdef	PROCESSOR_ARCHITECTURE_ARM
161 	case PROCESSOR_ARCHITECTURE_ARM:
162 		strlcpy(ubuf->machine, "arm", sizeof (ubuf->machine));
163 		break;
164 #endif
165 #ifdef	PROCESSOR_ARCHITECTURE_IA64
166 	case PROCESSOR_ARCHITECTURE_IA64:
167 		strlcpy(ubuf->machine, "ia64", sizeof (ubuf->machine));
168 		break;
169 #endif
170 #ifdef	PROCESSOR_ARCHITECTURE_ALPHA64
171 	case PROCESSOR_ARCHITECTURE_ALPHA64:
172 		strlcpy(ubuf->machine, "alpha64", sizeof (ubuf->machine));
173 		break;
174 #endif
175 #ifdef	PROCESSOR_ARCHITECTURE_MSIL
176 	case PROCESSOR_ARCHITECTURE_MSIL:
177 		strlcpy(ubuf->machine, "msil", sizeof (ubuf->machine));
178 		break;
179 #endif
180 #ifdef	PROCESSOR_ARCHITECTURE_AMD64
181 	case PROCESSOR_ARCHITECTURE_AMD64:
182 		strlcpy(ubuf->machine, "amd64", sizeof (ubuf->machine));
183 		break;
184 #endif
185 #ifdef	PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
186 	case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
187 		strlcpy(ubuf->machine, "ia32_win64", sizeof (ubuf->machine));
188 		break;
189 #endif
190 	default:
191 		strlcpy(ubuf->machine, "unknown", sizeof (ubuf->machine));
192 	}
193 	return (0);
194 }
195 
196 #else
197 EXPORT int
uname(ubuf)198 uname(ubuf)
199 	struct utsname	*ubuf;
200 {
201 	strlcpy(ubuf->sysname, "unknown", sizeof (ubuf->sysname));
202 	strlcpy(ubuf->nodename, "unknown", sizeof (ubuf->nodename));
203 	strlcpy(ubuf->release, "unknown", sizeof (ubuf->release));
204 	strlcpy(ubuf->version, "unknown", sizeof (ubuf->version));
205 
206 	strlcpy(ubuf->machine, "unknown", sizeof (ubuf->machine));
207 #ifdef	mc68000
208 #ifdef	mc68020
209 	strlcpy(ubuf->machine, "mc68020", sizeof (ubuf->machine));
210 #else
211 	strlcpy(ubuf->machine, "mc68000", sizeof (ubuf->machine));
212 #endif
213 #endif
214 #if	defined(__sparc) || defined(sparc)
215 	strlcpy(ubuf->machine, "sparc", sizeof (ubuf->machine));
216 #endif
217 #if	defined(__i386__) || defined(__i386) || defined(i386)
218 #if	defined(__amd64__) || defined(__amd64)
219 	strlcpy(ubuf->machine, "amd64", sizeof (ubuf->machine));
220 #else
221 	strlcpy(ubuf->machine, "i386", sizeof (ubuf->machine));
222 #endif
223 #endif
224 	return (0);
225 }
226 #endif
227 
228 #endif	/* HAVE_UNAME */
229