getlogin_r.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Provide a working getlogin_r for systems which lack it.
  2. Copyright (C) 2005-2007, 2010-2021 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert, Derek Price, and Bruno Haible. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include "malloca.h"
  20. #if defined _WIN32 && ! defined __CYGWIN__
  21. # define WIN32_LEAN_AND_MEAN
  22. # include <windows.h>
  23. /* Don't assume that UNICODE is not defined. */
  24. # undef GetUserName
  25. # define GetUserName GetUserNameA
  26. #else
  27. # if !HAVE_DECL_GETLOGIN
  28. extern char *getlogin (void);
  29. # endif
  30. #endif
  31. /* See unistd.in.h for documentation. */
  32. int
  33. getlogin_r (char *name, size_t size)
  34. {
  35. #undef getlogin_r
  36. #if defined _WIN32 && ! defined __CYGWIN__
  37. /* Native Windows platform. */
  38. DWORD sz;
  39. /* When size > 0x7fff, the doc says that GetUserName will fail.
  40. Actually, on Windows XP SP3, it succeeds. But let's be safe,
  41. for the sake of older Windows versions. */
  42. if (size > 0x7fff)
  43. size = 0x7fff;
  44. sz = size;
  45. if (!GetUserName (name, &sz))
  46. {
  47. if (GetLastError () == ERROR_INSUFFICIENT_BUFFER)
  48. /* In this case, the doc says that sz contains the required size, but
  49. actually, on Windows XP SP3, it contains 2 * the required size. */
  50. return ERANGE;
  51. else
  52. return ENOENT;
  53. }
  54. return 0;
  55. #elif HAVE_GETLOGIN_R
  56. /* Platform with a getlogin_r() function. */
  57. int ret = getlogin_r (name, size);
  58. if (ret == 0)
  59. {
  60. const char *nul = memchr (name, '\0', size);
  61. if (nul == NULL)
  62. /* name contains a truncated result. */
  63. return ERANGE;
  64. if (size > 0 && nul == name + size - 1)
  65. {
  66. /* strlen(name) == size-1. Determine whether the untruncated result
  67. would have had length size-1 or size. */
  68. char *room = (char *) malloca (size + 1);
  69. if (room == NULL)
  70. return ENOMEM;
  71. ret = getlogin_r (room, size + 1);
  72. /* The untruncated result should be the same as in the first call. */
  73. if (ret == 0 && memcmp (name, room, size) != 0)
  74. /* The untruncated result would have been different. */
  75. ret = ERANGE;
  76. freea (room);
  77. }
  78. }
  79. return ret;
  80. #else
  81. /* Platform with a getlogin() function. */
  82. char *n;
  83. size_t nlen;
  84. errno = 0;
  85. n = getlogin ();
  86. if (!n)
  87. /* ENOENT is a reasonable errno value if getlogin returns NULL. */
  88. return (errno != 0 ? errno : ENOENT);
  89. nlen = strlen (n);
  90. if (size <= nlen)
  91. return ERANGE;
  92. memcpy (name, n, nlen + 1);
  93. return 0;
  94. #endif
  95. }