X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fgetlogin_r.c;h=c1e7435876bf377e03be36d8cdc416593ef10513;hb=c5728261c324a75f8d23dd7d10cb42dde9420227;hp=c02db2fe6559e8370c3f126ed931cc4bb13eacde;hpb=2eb4c7c4b75c78250ba27a844e3d5f1958c113b6;p=gnulib.git diff --git a/lib/getlogin_r.c b/lib/getlogin_r.c index c02db2fe6..c1e743587 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 Free Software Foundation, Inc. + Copyright (C) 2005-2007, 2009-2010 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 @@ -16,39 +16,64 @@ 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. */ -#if HAVE_CONFIG_H -# include -#endif +#include -#include "getlogin_r.h" +/* Specification. */ +#include #include #include -#if HAVE_UNISTD_H -# include -#endif - -#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 getlogin_r.h for documentation. */ +/* 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; errno = 0; n = getlogin (); if (!n) - return errno ? errno : ENOENT; + /* ENOENT is a reasonable errno value if getlogin returns NULL. */ + return (errno != 0 ? errno : ENOENT); + nlen = strlen (n); if (size <= nlen) return ERANGE; memcpy (name, n, nlen + 1); return 0; +#endif }