1 /* Provide a working getlogin_r for systems which lack it.
3 Copyright (C) 2005-2007, 2010-2011 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert, Derek Price, and Bruno Haible. */
29 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 # define WIN32_LEAN_AND_MEAN
33 # if !HAVE_DECL_GETLOGIN
34 extern char *getlogin (void);
38 /* See unistd.in.h for documentation. */
40 getlogin_r (char *name, size_t size)
43 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
44 /* Native Windows platform. */
47 /* When size > 0x7fff, the doc says that GetUserName will fail.
48 Actually, on Windows XP SP3, it succeeds. But let's be safe,
49 for the sake of older Windows versions. */
53 if (!GetUserName (name, &sz))
55 if (GetLastError () == ERROR_INSUFFICIENT_BUFFER)
56 /* In this case, the doc says that sz contains the required size, but
57 actually, on Windows XP SP3, it contains 2 * the required size. */
64 /* Platform with a getlogin_r() function. */
65 int ret = getlogin_r (name, size);
67 if (ret == 0 && memchr (name, '\0', size) == NULL)
68 /* name contains a truncated result. */
72 /* Platform with a getlogin() function. */
79 /* ENOENT is a reasonable errno value if getlogin returns NULL. */
80 return (errno != 0 ? errno : ENOENT);
85 memcpy (name, n, nlen + 1);