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