From 1622b36b4ae889ea79ddc444e86fea31cd91755b Mon Sep 17 00:00:00 2001
From: Eric Blake <eblake@redhat.com>
Date: Wed, 25 May 2011 15:15:14 -0600
Subject: [PATCH] getcwd: work around mingw bug

mingw getcwd(buf, 0) fails with ERANGE, instead of the required
EINVAL.  Since we're already replacing getcwd on mingw, the
workaround is trivial.

* lib/getcwd-lgpl.c (rpl_getcwd): Guarantee correct error.
* doc/posix-functions/getcwd.texi (getcwd): Document it.
Reported by Matthias Bolte.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 ChangeLog                       | 7 +++++++
 doc/posix-functions/getcwd.texi | 4 ++++
 lib/getcwd-lgpl.c               | 9 ++++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 390d4e808d..e3c4d221a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-05-25  Eric Blake  <eblake@redhat.com>
+
+	getcwd: work around mingw bug
+	* lib/getcwd-lgpl.c (rpl_getcwd): Guarantee correct error.
+	* doc/posix-functions/getcwd.texi (getcwd): Document it.
+	Reported by Matthias Bolte.
+
 2011-05-24  Paul Eggert  <eggert@cs.ucla.edu>
 
 	test-intprops: disable -Wtype-limits diagnostics
diff --git a/doc/posix-functions/getcwd.texi b/doc/posix-functions/getcwd.texi
index a49a8990dc..ffb5086ffb 100644
--- a/doc/posix-functions/getcwd.texi
+++ b/doc/posix-functions/getcwd.texi
@@ -16,6 +16,10 @@ On some other platforms, this call is not allowed.
 On some platforms, the prototype for @code{getcwd} uses @code{int}
 instead of @code{size_t} for the size argument:
 mingw.
+@item
+On some platforms, @code{getcwd (buf, 0)} fails with @code{ERANGE}
+instead of the required @code{EINVAL}:
+mingw.
 @end itemize
 
 Portability problems fixed by Gnulib module @code{getcwd}:
diff --git a/lib/getcwd-lgpl.c b/lib/getcwd-lgpl.c
index 53c5562357..27614228a0 100644
--- a/lib/getcwd-lgpl.c
+++ b/lib/getcwd-lgpl.c
@@ -45,7 +45,14 @@ rpl_getcwd (char *buf, size_t size)
 
   /* Handle single size operations.  */
   if (buf)
-    return getcwd (buf, size);
+    {
+      if (!size)
+        {
+          errno = EINVAL;
+          return NULL;
+        }
+      return getcwd (buf, size);
+    }
 
   if (size)
     {
-- 
2.30.2