Silence ARI warning about floatformat_to_double
[deliverable/binutils-gdb.git] / gnulib / import / getlogin_r.c
CommitLineData
6ec2e0f5
SDJ
1/* Provide a working getlogin_r for systems which lack it.
2
5e8754f9 3 Copyright (C) 2005-2007, 2010-2016 Free Software Foundation, Inc.
6ec2e0f5
SDJ
4
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 3, or (at your option)
8 any later version.
9
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.
14
15 You should have received a copy of the GNU General Public License
5e8754f9 16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
6ec2e0f5
SDJ
17
18/* Written by Paul Eggert, Derek Price, and Bruno Haible. */
19
20#include <config.h>
21
22/* Specification. */
23#include <unistd.h>
24
25#include <errno.h>
26#include <string.h>
27
5e8754f9 28#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
6ec2e0f5
SDJ
29# define WIN32_LEAN_AND_MEAN
30# include <windows.h>
31#else
32# if !HAVE_DECL_GETLOGIN
33extern char *getlogin (void);
34# endif
35#endif
36
37/* See unistd.in.h for documentation. */
38int
39getlogin_r (char *name, size_t size)
40{
41#undef getlogin_r
5e8754f9 42#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
6ec2e0f5
SDJ
43 /* Native Windows platform. */
44 DWORD sz;
45
46 /* When size > 0x7fff, the doc says that GetUserName will fail.
47 Actually, on Windows XP SP3, it succeeds. But let's be safe,
48 for the sake of older Windows versions. */
49 if (size > 0x7fff)
50 size = 0x7fff;
51 sz = size;
52 if (!GetUserName (name, &sz))
53 {
54 if (GetLastError () == ERROR_INSUFFICIENT_BUFFER)
55 /* In this case, the doc says that sz contains the required size, but
56 actually, on Windows XP SP3, it contains 2 * the required size. */
57 return ERANGE;
58 else
59 return ENOENT;
60 }
61 return 0;
62#elif HAVE_GETLOGIN_R
63 /* Platform with a getlogin_r() function. */
64 int ret = getlogin_r (name, size);
65
5e8754f9
SDJ
66 if (ret == 0 && memchr (name, '\0', size) == NULL)
67 /* name contains a truncated result. */
68 return ERANGE;
6ec2e0f5
SDJ
69 return ret;
70#else
71 /* Platform with a getlogin() function. */
72 char *n;
73 size_t nlen;
74
75 errno = 0;
76 n = getlogin ();
77 if (!n)
78 /* ENOENT is a reasonable errno value if getlogin returns NULL. */
79 return (errno != 0 ? errno : ENOENT);
80
81 nlen = strlen (n);
82 if (size <= nlen)
83 return ERANGE;
84 memcpy (name, n, nlen + 1);
85 return 0;
86#endif
87}
This page took 0.199707 seconds and 4 git commands to generate.