[release/6.0] [mono] win32 implementation of g_get_current_dir (#58555)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Tue, 7 Sep 2021 15:30:43 +0000 (17:30 +0200)
committerGitHub <noreply@github.com>
Tue, 7 Sep 2021 15:30:43 +0000 (17:30 +0200)
Co-authored-by: Radek Doulik <radekdoulik@gmail.com>
src/mono/mono/eglib/gfile-posix.c
src/mono/mono/eglib/gmisc-unix.c
src/mono/mono/eglib/gmisc-win32.c

index 5bda34c..f46f809 100644 (file)
@@ -161,26 +161,3 @@ g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **gerror)
        }
        return fd;
 }
-
-gchar *
-g_get_current_dir (void)
-{
-       int s = 32;
-       char *buffer = NULL, *r;
-       gboolean fail;
-       
-       do {
-               buffer = g_realloc (buffer, s);
-               r = getcwd (buffer, s);
-               fail = (r == NULL && errno == ERANGE);
-               if (fail) {
-                       s <<= 1;
-               }
-       } while (fail);
-
-       /* On amd64 sometimes the bottom 32-bits of r == the bottom 32-bits of buffer
-        * but the top 32-bits of r have overflown to 0xffffffff (seriously, getcwd
-        * so we return the buffer here since it has a pointer to the valid string
-        */
-       return buffer;
-}
index 2a3afc0..2630d51 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <config.h>
 #include <stdlib.h>
+#include <errno.h>
 #include <glib.h>
 #include <pthread.h>
 
@@ -201,3 +202,25 @@ g_get_tmp_dir (void)
        return tmp_dir;
 }
 
+gchar *
+g_get_current_dir (void)
+{
+       int s = 32;
+       char *buffer = NULL, *r;
+       gboolean fail;
+
+       do {
+               buffer = g_realloc (buffer, s);
+               r = getcwd (buffer, s);
+               fail = (r == NULL && errno == ERANGE);
+               if (fail) {
+                       s <<= 1;
+               }
+       } while (fail);
+
+       /* On amd64 sometimes the bottom 32-bits of r == the bottom 32-bits of buffer
+        * but the top 32-bits of r have overflown to 0xffffffff (seriously, getcwd
+        * so we return the buffer here since it has a pointer to the valid string
+        */
+       return buffer;
+}
index f5589cb..82860a5 100644 (file)
@@ -232,3 +232,35 @@ g_get_tmp_dir (void)
        }
        return tmp_dir;
 }
+
+gchar *
+g_get_current_dir (void)
+{
+       gunichar2 *buffer = NULL;
+       gchar* val = NULL;
+       gint32 retval, buffer_size = MAX_PATH;
+
+       buffer = g_new (gunichar2, buffer_size);
+       retval = GetCurrentDirectoryW (buffer_size, buffer);
+
+       if (retval != 0) {
+               // the size might be larger than MAX_PATH
+               // https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd
+               if (retval > buffer_size) {
+                       buffer_size = retval;
+                       buffer = g_realloc (buffer, buffer_size*sizeof(gunichar2));
+                       retval = GetCurrentDirectoryW (buffer_size, buffer);
+               }
+
+               val = u16to8 (buffer);
+       } else {
+               if (GetLastError () != ERROR_ENVVAR_NOT_FOUND) {
+                       val = g_malloc (1);
+                       *val = 0;
+               }
+       }
+
+       g_free (buffer);
+
+       return val;
+}