getlog.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Implementation of the GETLOG g77 intrinsic.
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #ifdef HAVE_UNISTD_H
  24. # if defined __MINGW32__ && defined HAVE_GETLOGIN
  25. # define _POSIX 1
  26. # endif
  27. #include <unistd.h>
  28. #endif
  29. #ifdef HAVE_PWD_H
  30. #include <pwd.h>
  31. #endif
  32. /* Windows32 version */
  33. #if defined __MINGW32__ && !defined HAVE_GETLOGIN
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. #include <lmcons.h> /* for UNLEN */
  37. static char *
  38. w32_getlogin (void)
  39. {
  40. static char name [UNLEN + 1];
  41. DWORD namelen = sizeof (name);
  42. GetUserName (name, &namelen);
  43. return (name[0] == 0 ? NULL : name);
  44. }
  45. #undef getlogin
  46. #define getlogin w32_getlogin
  47. #define HAVE_GETLOGIN 1
  48. #endif
  49. /* GETLOG (LOGIN), g77 intrinsic for retrieving the login name for the
  50. process.
  51. CHARACTER(len=*), INTENT(OUT) :: LOGIN */
  52. void PREFIX(getlog) (char *, gfc_charlen_type);
  53. export_proto_np(PREFIX(getlog));
  54. void
  55. PREFIX(getlog) (char * login, gfc_charlen_type login_len)
  56. {
  57. int p_len;
  58. char *p;
  59. memset (login, ' ', login_len); /* Blank the string. */
  60. #if defined(HAVE_POSIX_GETPWUID_R) && defined(HAVE_GETEUID)
  61. struct passwd pwd;
  62. struct passwd *result;
  63. char *buf;
  64. int err;
  65. /* To be pedantic, buflen should be determined by
  66. sysconf(_SC_GETPW_R_SIZE_MAX), which is 1024 on some tested
  67. targets; we do something simple in case the target doesn't
  68. support sysconf. */
  69. static const size_t buflen = 1024;
  70. buf = xmalloc (buflen);
  71. err = getpwuid_r (geteuid (), &pwd, buf, buflen, &result);
  72. if (err != 0 || result == NULL)
  73. goto cleanup;
  74. p = pwd.pw_name;
  75. #elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
  76. {
  77. struct passwd *pw = getpwuid (geteuid ());
  78. if (pw)
  79. p = pw->pw_name;
  80. else
  81. return;
  82. }
  83. #elif HAVE_GETLOGIN
  84. p = getlogin();
  85. # else
  86. return;
  87. #endif
  88. if (p == NULL)
  89. goto cleanup;
  90. p_len = strlen (p);
  91. if (login_len < p_len)
  92. p_len = login_len;
  93. memcpy (login, p, p_len);
  94. cleanup:
  95. #if defined (HAVE_POSIX_GETPWUID_R) && defined(HAVE_GETEUID)
  96. free (buf);
  97. #else
  98. ;
  99. #endif
  100. }