2010-04-06 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / eglib / src / gmisc-win32.c
blobf1fe019c55023712ace94dcdca963c203deb6450
1 /*
2 * gmisc.c: Misc functions with no place to go (right now)
4 * Author:
5 * Aaron Bockover (abockover@novell.com)
7 * (C) 2006 Novell, Inc.
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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.
29 #include <stdlib.h>
30 #include <glib.h>
32 #include <windows.h>
33 #include <direct.h>
34 #include <io.h>
36 const gchar *
37 g_getenv(const gchar *variable)
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 = GetEnvironmentVariable (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 = GetEnvironmentVariable (var, buffer, buffer_size);
53 val = u16to8 (buffer);
55 g_free(var);
56 g_free(buffer);
57 return val;
60 gboolean
61 g_setenv(const gchar *variable, const gchar *value, gboolean overwrite)
63 gunichar2 *var, *val;
64 gboolean result;
65 var = u8to16(variable);
66 val = u8to16(value);
67 result = (SetEnvironmentVariable(var, val) != 0) ? TRUE : FALSE;
68 g_free(var);
69 g_free(val);
70 return result;
73 void
74 g_unsetenv(const gchar *variable)
76 gunichar2 *var;
77 var = u8to16(variable);
78 SetEnvironmentVariable(var, TEXT(""));
79 g_free(var);
82 gchar*
83 g_win32_getlocale(void)
85 /* FIXME: Use GetThreadLocale
86 * and convert LCID to standard
87 * string form, "en_US" */
88 return strdup ("en_US");
91 gboolean
92 g_path_is_absolute (const char *filename)
94 g_return_val_if_fail (filename != NULL, FALSE);
96 if (filename[0] != '\0' && filename[1] != '\0')
97 if (filename[1] == ':' && filename[2] != '\0' &&
98 (filename[2] == '\\' || filename[2] == '/'))
99 return TRUE;
100 /* UNC paths */
101 else if (filename[0] == '\\' && filename[1] == '\\' &&
102 filename[2] != '\0')
103 return TRUE;
105 return FALSE;
108 const gchar *
109 g_get_home_dir (void)
111 /* FIXME */
112 const gchar *drive = g_getenv ("HOMEDRIVE");
113 const gchar *path = g_getenv ("HOMEPATH");
114 gchar *home_dir = NULL;
116 if (drive && path) {
117 home_dir = g_malloc(strlen(drive) + strlen(path) +1);
118 if (home_dir) {
119 sprintf(home_dir, "%s%s", drive, path);
123 return home_dir;
126 const char *
127 g_get_user_name (void)
129 const char * retName = g_getenv ("USER");
130 if (!retName)
131 retName = g_getenv ("USERNAME");
132 return retName;
135 static const char *tmp_dir;
137 const gchar *
138 g_get_tmp_dir (void)
140 if (tmp_dir == NULL){
141 if (tmp_dir == NULL){
142 tmp_dir = g_getenv ("TMPDIR");
143 if (tmp_dir == NULL){
144 tmp_dir = g_getenv ("TMP");
145 if (tmp_dir == NULL){
146 tmp_dir = g_getenv ("TEMP");
147 if (tmp_dir == NULL)
148 tmp_dir = "C:\\temp";
153 return tmp_dir;