bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getlogin on Windows systems


From: Bruno Haible
Subject: Re: getlogin on Windows systems
Date: Sat, 9 Jan 2010 16:06:54 +0100
User-agent: KMail/1.9.9

> > > the gnulib replacement for getlogin_r calls getlogin unconditionally, but
> > > getlogin doesn't exist on Windows systems.
> 
> Oh, I see now what you mean. The cause is that getlogin_r had no unit test. 

The new unit test indeed fails to link on mingw:

gcc -mno-cygwin  -g -O2  -L/usr/local/mingw/lib -o test-getlogin_r.exe 
test-getlogin_r.o ../gllib/libgnu.a 
../gllib/libgnu.a(getlogin_r.o): In function `getlogin_r':
/home/bruno/testdir2/gllib/getlogin_r.c:41: undefined reference to `_getlogin'
collect2: ld returned 1 exit status
make[4]: *** [test-getlogin_r.exe] Error 1

This fixes it:


2010-01-09  Bruno Haible  <address@hidden>

        getlogin_r: Support for native Windows.
        * lib/getlogin_r.c: Include <windows.h>
        (getlogin_r): Implement for native Windows.
        * tests/test-getlogin_r.c (main): Also test with a huge buffer.
        Reported by <address@hidden> via John W. Eaton <address@hidden>.

--- lib/getlogin_r.c.orig       Sat Jan  9 16:03:17 2010
+++ lib/getlogin_r.c    Sat Jan  9 16:00:20 2010
@@ -16,7 +16,7 @@
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-/* written by Paul Eggert and Derek Price */
+/* Written by Paul Eggert, Derek Price, and Bruno Haible.  */
 
 #include <config.h>
 
@@ -26,14 +26,41 @@
 #include <errno.h>
 #include <string.h>
 
-#if !HAVE_DECL_GETLOGIN
-char *getlogin (void);
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# define WIN32_LEAN_AND_MEAN
+# include <windows.h>
+#else
+# if !HAVE_DECL_GETLOGIN
+extern char *getlogin (void);
+# endif
 #endif
 
 /* See unistd.in.h for documentation.  */
 int
 getlogin_r (char *name, size_t size)
 {
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+  /* Native Windows platform.  */
+  DWORD sz;
+
+  /* When size > 0x7fff, the doc says that GetUserName will fail.
+     Actually, on Windows XP SP3, it succeeds.  But let's be safe,
+     for the sake of older Windows versions.  */
+  if (size > 0x7fff)
+    size = 0x7fff;
+  sz = size;
+  if (!GetUserName (name, &sz))
+    {
+      if (GetLastError () == ERROR_INSUFFICIENT_BUFFER)
+        /* In this case, the doc says that sz contains the required size, but
+           actually, on Windows XP SP3, it contains 2 * the required size.  */
+        return ERANGE;
+      else
+        return ENOENT;
+    }
+  return 0;
+#else
+  /* Platform with a getlogin() function.  */
   char *n;
   size_t nlen;
 
@@ -48,4 +75,5 @@
     return ERANGE;
   memcpy (name, n, nlen + 1);
   return 0;
+#endif
 }
--- tests/test-getlogin_r.c.orig        Sat Jan  9 16:03:17 2010
+++ tests/test-getlogin_r.c     Sat Jan  9 15:15:18 2010
@@ -69,5 +69,13 @@
       ASSERT (getlogin_r (smallbuf, i) == ERANGE);
   }
 
+  /* Test with a huge buffer.  */
+  {
+    static char hugebuf[70000];
+
+    ASSERT (getlogin_r (hugebuf, sizeof (hugebuf)) == 0);
+    ASSERT (strcmp (hugebuf, buf) == 0);
+  }
+
   return 0;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]