2 * gmisc.c: Misc functions with no place to go (right now)
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.
35 #if _MSC_VER && G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
43 g_hasenv (const gchar *variable)
45 return g_getenv (variable) != NULL;
49 g_getenv(const gchar *variable)
51 gunichar2 *var, *buffer;
53 gint32 buffer_size = 1024;
55 var = u8to16(variable);
56 buffer = g_malloc(buffer_size*sizeof(gunichar2));
57 retval = GetEnvironmentVariableW (var, buffer, buffer_size);
59 if (retval > buffer_size) {
62 buffer = g_malloc(buffer_size*sizeof(gunichar2));
63 retval = GetEnvironmentVariableW (var, buffer, buffer_size);
65 val = u16to8 (buffer);
67 if (GetLastError () != ERROR_ENVVAR_NOT_FOUND){
78 g_setenv(const gchar *variable, const gchar *value, gboolean overwrite)
82 var = u8to16(variable);
84 result = (SetEnvironmentVariableW(var, val) != 0) ? TRUE : FALSE;
91 g_unsetenv(const gchar *variable)
94 var = u8to16(variable);
95 SetEnvironmentVariableW(var, L"");
99 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
101 g_win32_getlocale(void)
103 LCID lcid = GetThreadLocale();
105 gint ccBuf = GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME, buf, 9);
106 buf[ccBuf - 1] = '-';
107 ccBuf += GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME, buf + ccBuf, 9);
108 return g_strdup (buf);
110 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
113 g_path_is_absolute (const char *filename)
115 g_return_val_if_fail (filename != NULL, FALSE);
117 if (filename[0] != '\0' && filename[1] != '\0') {
118 if (filename[1] == ':' && filename[2] != '\0' &&
119 (filename[2] == '\\' || filename[2] == '/'))
122 else if (filename[0] == '\\' && filename[1] == '\\' &&
130 #if _MSC_VER && G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
132 g_get_known_folder_path (void)
134 gchar *folder_path = NULL;
135 PWSTR profile_path = NULL;
136 HRESULT hr = SHGetKnownFolderPath (&FOLDERID_Profile, KF_FLAG_DEFAULT, NULL, &profile_path);
138 folder_path = u16to8 (profile_path);
139 CoTaskMemFree (profile_path);
147 static inline gchar *
148 g_get_known_folder_path (void)
155 g_get_home_dir (void)
157 gchar *home_dir = g_get_known_folder_path ();
160 home_dir = (gchar *) g_getenv ("USERPROFILE");
164 const gchar *drive = g_getenv ("HOMEDRIVE");
165 const gchar *path = g_getenv ("HOMEPATH");
168 home_dir = g_malloc (strlen (drive) + strlen (path) + 1);
170 sprintf (home_dir, "%s%s", drive, path);
181 g_get_user_name (void)
183 const char * retName = g_getenv ("USER");
185 retName = g_getenv ("USERNAME");
189 static const char *tmp_dir;
194 if (tmp_dir == NULL){
195 if (tmp_dir == NULL){
196 tmp_dir = g_getenv ("TMPDIR");
197 if (tmp_dir == NULL){
198 tmp_dir = g_getenv ("TMP");
199 if (tmp_dir == NULL){
200 tmp_dir = g_getenv ("TEMP");
202 tmp_dir = "C:\\temp";