Use GetCurrentDirectory() directly for simpler buffer length management. I
authorTor Lillqvist <tml@iki.fi>
Thu, 30 Dec 2004 17:48:23 +0000 (17:48 +0000)
committerTor Lillqvist <tml@src.gnome.org>
Thu, 30 Dec 2004 17:48:23 +0000 (17:48 +0000)
2004-12-30  Tor Lillqvist  <tml@iki.fi>

* glib/gutils.c (g_get_current_dir): Use GetCurrentDirectory()
directly for simpler buffer length management. I don't trust
getcwd() getting it right all the time.

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib/gutils.c

index 7890b77..7e3b1bd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2004-12-30  Tor Lillqvist  <tml@iki.fi>
+
+       * glib/gutils.c (g_get_current_dir): Use GetCurrentDirectory()
+       directly for simpler buffer length management. I don't trust
+       getcwd() getting it right all the time.
+
 2004-12-30  Matthias Clasen  <mclasen@redhat.com>
 
        * glib/gfileutils.c (g_file_test): Typo fix.
index 7890b77..7e3b1bd 100644 (file)
@@ -1,3 +1,9 @@
+2004-12-30  Tor Lillqvist  <tml@iki.fi>
+
+       * glib/gutils.c (g_get_current_dir): Use GetCurrentDirectory()
+       directly for simpler buffer length management. I don't trust
+       getcwd() getting it right all the time.
+
 2004-12-30  Matthias Clasen  <mclasen@redhat.com>
 
        * glib/gfileutils.c (g_file_test): Typo fix.
index 7890b77..7e3b1bd 100644 (file)
@@ -1,3 +1,9 @@
+2004-12-30  Tor Lillqvist  <tml@iki.fi>
+
+       * glib/gutils.c (g_get_current_dir): Use GetCurrentDirectory()
+       directly for simpler buffer length management. I don't trust
+       getcwd() getting it right all the time.
+
 2004-12-30  Matthias Clasen  <mclasen@redhat.com>
 
        * glib/gfileutils.c (g_file_test): Typo fix.
index 7890b77..7e3b1bd 100644 (file)
@@ -1,3 +1,9 @@
+2004-12-30  Tor Lillqvist  <tml@iki.fi>
+
+       * glib/gutils.c (g_get_current_dir): Use GetCurrentDirectory()
+       directly for simpler buffer length management. I don't trust
+       getcwd() getting it right all the time.
+
 2004-12-30  Matthias Clasen  <mclasen@redhat.com>
 
        * glib/gfileutils.c (g_file_test): Typo fix.
index 7890b77..7e3b1bd 100644 (file)
@@ -1,3 +1,9 @@
+2004-12-30  Tor Lillqvist  <tml@iki.fi>
+
+       * glib/gutils.c (g_get_current_dir): Use GetCurrentDirectory()
+       directly for simpler buffer length management. I don't trust
+       getcwd() getting it right all the time.
+
 2004-12-30  Matthias Clasen  <mclasen@redhat.com>
 
        * glib/gfileutils.c (g_file_test): Typo fix.
index 92ad216..f9fb931 100644 (file)
@@ -718,35 +718,51 @@ g_path_get_dirname (const gchar      *file_name)
 gchar*
 g_get_current_dir (void)
 {
-  gchar *buffer = NULL;
+#ifdef G_OS_WIN32
+
   gchar *dir = NULL;
-  static gulong max_len = 0;
 
-  if (max_len == 0) 
-    max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
-  
-#ifdef G_OS_WIN32
   if (G_WIN32_HAVE_WIDECHAR_API ())
     {
-      wchar_t *wdir = _wgetcwd (NULL, max_len);
-      if (wdir)
+      wchar_t dummy[2], *wdir;
+      int len;
+
+      len = GetCurrentDirectoryW (2, dummy);
+      wdir = g_new (wchar_t, len);
+
+      if (GetCurrentDirectoryW (len, wdir) == len - 1)
        dir = g_utf16_to_utf8 (wdir, -1, NULL, NULL, NULL);
-      else
-       dir = g_strdup (G_DIR_SEPARATOR_S);
-      free (wdir);
+
+      g_free (wdir);
     }
   else
     {
-      char *cpdir = getcwd (NULL, max_len);
-      if (cpdir)
+      gchar dummy[2], *cpdir;
+      int len;
+
+      len = GetCurrentDirectoryA (2, dummy);
+      cpdir = g_new (gchar, len);
+
+      if (GetCurrentDirectoryA (len, cpdir) == len - 1)
        dir = g_locale_to_utf8 (cpdir, -1, NULL, NULL, NULL);
-      else
-       dir = g_strdup (G_DIR_SEPARATOR_S);
-      free (cpdir);
+
+      g_free (cpdir);
     }
 
+  if (dir == NULL)
+    dir = g_strdup ("\\");
+
   return dir;
+
 #else
+
+  gchar *buffer = NULL;
+  gchar *dir = NULL;
+  static gulong max_len = 0;
+
+  if (max_len == 0) 
+    max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
+  
   /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
    * and, if that wasn't bad enough, hangs in doing so.
    */