Enabled g_mem_set_vtable through the configure option --with-overridable-allocators...
[mono.git] / eglib / src / gmisc-win32.c
1 /*
2  * gmisc.c: Misc functions with no place to go (right now)
3  *
4  * Author:
5  *   Aaron Bockover (abockover@novell.com)
6  *
7  * (C) 2006 Novell, Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include <stdlib.h>
30 #include <glib.h>
31
32 #include <windows.h>
33 #include <direct.h>
34 #include <io.h>
35
36 const gchar *
37 g_getenv(const gchar *variable)
38 {
39         gunichar2 *var, *buffer;
40         gchar* val = NULL;
41         gint32 buffer_size = 1024;
42         gint32 retval;
43         var = u8to16(variable); 
44         buffer = g_malloc(buffer_size*sizeof(gunichar2));
45         retval = GetEnvironmentVariableW (var, buffer, buffer_size);
46         if (retval != 0) {
47                 if (retval > buffer_size) {
48                         g_free (buffer);
49                         buffer_size = retval;
50                         buffer = g_malloc(buffer_size*sizeof(gunichar2));
51                         retval = GetEnvironmentVariableW (var, buffer, buffer_size);
52                 }
53                 val = u16to8 (buffer);
54         } else {
55                 if (GetLastError () != ERROR_ENVVAR_NOT_FOUND){
56                         val = g_malloc (1);
57                         *val = 0;
58                 }
59         }
60         g_free(var);
61         g_free(buffer);
62         return val; 
63 }
64
65 gboolean
66 g_setenv(const gchar *variable, const gchar *value, gboolean overwrite)
67 {
68         gunichar2 *var, *val;
69         gboolean result;
70         var = u8to16(variable); 
71         val = u8to16(value);
72         result = (SetEnvironmentVariableW(var, val) != 0) ? TRUE : FALSE;
73         g_free(var);
74         g_free(val);
75         return result;
76 }
77
78 void
79 g_unsetenv(const gchar *variable)
80 {
81         gunichar2 *var;
82         var = u8to16(variable); 
83         SetEnvironmentVariableW(var, L"");
84         g_free(var);
85 }
86
87 gchar*
88 g_win32_getlocale(void)
89 {
90         LCID lcid = GetThreadLocale();
91         gchar buf[19];
92         gint ccBuf = GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, 9);
93         buf[ccBuf - 1] = '-';
94         ccBuf += GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, buf + ccBuf, 9);
95         return g_strdup (buf);
96 }
97
98 gboolean
99 g_path_is_absolute (const char *filename)
100 {
101         g_return_val_if_fail (filename != NULL, FALSE);
102
103         if (filename[0] != '\0' && filename[1] != '\0') {
104                 if (filename[1] == ':' && filename[2] != '\0' &&
105                         (filename[2] == '\\' || filename[2] == '/'))
106                         return TRUE;
107                 /* UNC paths */
108                 else if (filename[0] == '\\' && filename[1] == '\\' && 
109                         filename[2] != '\0')
110                         return TRUE;
111         }
112
113         return FALSE;
114 }
115
116 const gchar *
117 g_get_home_dir (void)
118 {
119         /* FIXME */
120         const gchar *drive = g_getenv ("HOMEDRIVE");
121         const gchar *path = g_getenv ("HOMEPATH");
122         gchar *home_dir = NULL;
123         
124         if (drive && path) {
125                 home_dir = g_malloc(strlen(drive) + strlen(path) +1);
126                 if (home_dir) {
127                         sprintf(home_dir, "%s%s", drive, path);
128                 }
129         }
130
131         g_free (drive);
132         g_free (path);
133
134         return home_dir;
135 }
136
137 const char *
138 g_get_user_name (void)
139 {
140         const char * retName = g_getenv ("USER");
141         if (!retName)
142                 retName = g_getenv ("USERNAME");
143         return retName;
144 }
145
146 static const char *tmp_dir;
147
148 const gchar *
149 g_get_tmp_dir (void)
150 {
151         if (tmp_dir == NULL){
152                 if (tmp_dir == NULL){
153                         tmp_dir = g_getenv ("TMPDIR");
154                         if (tmp_dir == NULL){
155                                 tmp_dir = g_getenv ("TMP");
156                                 if (tmp_dir == NULL){
157                                         tmp_dir = g_getenv ("TEMP");
158                                         if (tmp_dir == NULL)
159                                                 tmp_dir = "C:\\temp";
160                                 }
161                         }
162                 }
163         }
164         return tmp_dir;
165 }
166