1 /**
2  * \file
3  * System.Environment support internal calls
4  *
5  * Authors:
6  *	Dick Porter (dick@ximian.com)
7  *	Sebastien Pouliot (sebastien@ximian.com)
8  *
9  * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
10  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12  */
13 
14 #include <config.h>
15 #include <glib.h>
16 
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/environment.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/handle.h>
21 #include <mono/utils/mono-compiler.h>
22 #include <mono/utils/w32api.h>
23 
24 extern MonoStringHandle ves_icall_System_Environment_GetOSVersionString (MonoError *error);
25 
26 #if !defined(HOST_WIN32) && defined(HAVE_SYS_UTSNAME_H)
27 #include <sys/utsname.h>
28 #endif
29 
30 static gint32 exitcode=0;
31 
32 /**
33  * mono_environment_exitcode_get:
34  */
35 gint32
mono_environment_exitcode_get(void)36 mono_environment_exitcode_get (void)
37 {
38 	return(exitcode);
39 }
40 
41 /**
42  * mono_environment_exitcode_set:
43  */
44 void
mono_environment_exitcode_set(gint32 value)45 mono_environment_exitcode_set (gint32 value)
46 {
47 	exitcode=value;
48 }
49 
50 /* note: we better manipulate the string in managed code (easier and safer) */
51 MonoStringHandle
ves_icall_System_Environment_GetOSVersionString(MonoError * error)52 ves_icall_System_Environment_GetOSVersionString (MonoError *error)
53 {
54 	error_init (error);
55 #ifdef HOST_WIN32
56 	OSVERSIONINFOEX verinfo;
57 
58 	verinfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
59 	if (GetVersionEx ((OSVERSIONINFO*)&verinfo)) {
60 		char version [128];
61 		/* maximum string length is 45 bytes
62 		   4 x 10 bytes per number, 1 byte for 0, 3 x 1 byte for dots, 1 for NULL */
63 		sprintf (version, "%ld.%ld.%ld.%d",
64 				 verinfo.dwMajorVersion,
65 				 verinfo.dwMinorVersion,
66 				 verinfo.dwBuildNumber,
67 				 verinfo.wServicePackMajor << 16);
68 		return mono_string_new_handle (mono_domain_get (), version, error);
69 	}
70 #elif defined(HAVE_SYS_UTSNAME_H)
71 	struct utsname name;
72 
73 	if (uname (&name) >= 0) {
74 		return mono_string_new_handle (mono_domain_get (), name.release, error);
75 	}
76 #endif
77 	return mono_string_new_handle (mono_domain_get (), "0.0.0.0", error);
78 }
79 
80