fstat: Fix module dependency conditions.
[gnulib/ericb.git] / lib / getlogin_r.c
blob230aba0ef53025a74a9748d9e46e47d9e7ffd243
1 /* Provide a working getlogin_r for systems which lack it.
3 Copyright (C) 2005-2007, 2010-2017 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)
8 any later version.
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, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert, Derek Price, and Bruno Haible. */
20 #include <config.h>
22 /* Specification. */
23 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
28 #include "malloca.h"
30 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
31 # define WIN32_LEAN_AND_MEAN
32 # include <windows.h>
33 #else
34 # if !HAVE_DECL_GETLOGIN
35 extern char *getlogin (void);
36 # endif
37 #endif
39 /* See unistd.in.h for documentation. */
40 int
41 getlogin_r (char *name, size_t size)
43 #undef getlogin_r
44 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
45 /* Native Windows platform. */
46 DWORD sz;
48 /* When size > 0x7fff, the doc says that GetUserName will fail.
49 Actually, on Windows XP SP3, it succeeds. But let's be safe,
50 for the sake of older Windows versions. */
51 if (size > 0x7fff)
52 size = 0x7fff;
53 sz = size;
54 if (!GetUserName (name, &sz))
56 if (GetLastError () == ERROR_INSUFFICIENT_BUFFER)
57 /* In this case, the doc says that sz contains the required size, but
58 actually, on Windows XP SP3, it contains 2 * the required size. */
59 return ERANGE;
60 else
61 return ENOENT;
63 return 0;
64 #elif HAVE_GETLOGIN_R
65 /* Platform with a getlogin_r() function. */
66 int ret = getlogin_r (name, size);
68 if (ret == 0)
70 const char *nul = memchr (name, '\0', size);
71 if (nul == NULL)
72 /* name contains a truncated result. */
73 return ERANGE;
74 if (size > 0 && nul == name + size - 1)
76 /* strlen(name) == size-1. Determine whether the untruncated result
77 would have had length size-1 or size. */
78 char *room = (char *) malloca (size + 1);
79 if (room == NULL)
80 return ENOMEM;
81 ret = getlogin_r (room, size + 1);
82 /* The untruncated result should be the same as in the first call. */
83 if (ret == 0 && memcmp (name, room, size) != 0)
84 /* The untruncated result would have been different. */
85 ret = ERANGE;
86 freea (room);
89 return ret;
90 #else
91 /* Platform with a getlogin() function. */
92 char *n;
93 size_t nlen;
95 errno = 0;
96 n = getlogin ();
97 if (!n)
98 /* ENOENT is a reasonable errno value if getlogin returns NULL. */
99 return (errno != 0 ? errno : ENOENT);
101 nlen = strlen (n);
102 if (size <= nlen)
103 return ERANGE;
104 memcpy (name, n, nlen + 1);
105 return 0;
106 #endif