X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fgetlogin_r.c;h=ac4f495cd6ead179ce9143c039e57d732e7a13d9;hb=7ef6c64e210ac0979d7e8ac69bc5b5208c2405ab;hp=b8b6cdd2d31fe00ec189815f459da684e1d749c0;hpb=89b39efd31cd0ffdaf05c9785c1af1e479728472;p=gnulib.git diff --git a/lib/getlogin_r.c b/lib/getlogin_r.c index b8b6cdd2d..ac4f495cd 100644 --- a/lib/getlogin_r.c +++ b/lib/getlogin_r.c @@ -1,6 +1,6 @@ /* Provide a working getlogin_r for systems which lack it. - Copyright (C) 2005-2007, 2009-2010 Free Software Foundation, Inc. + Copyright (C) 2005-2007, 2010-2014 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,10 +13,9 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program; if not, see . */ -/* written by Paul Eggert and Derek Price */ +/* Written by Paul Eggert, Derek Price, and Bruno Haible. */ #include @@ -26,14 +25,50 @@ #include #include -#if !HAVE_DECL_GETLOGIN -char *getlogin (void); +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +# define WIN32_LEAN_AND_MEAN +# include +#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) { +#undef getlogin_r +#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; +#elif HAVE_GETLOGIN_R + /* Platform with a getlogin_r() function. */ + int ret = getlogin_r (name, size); + + if (ret == 0 && memchr (name, '\0', size) == NULL) + /* name contains a truncated result. */ + return ERANGE; + return ret; +#else + /* Platform with a getlogin() function. */ char *n; size_t nlen; @@ -48,4 +83,5 @@ getlogin_r (char *name, size_t size) return ERANGE; memcpy (name, n, nlen + 1); return 0; +#endif }