hostnm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Implementation of the HOSTNM intrinsic.
  2. Copyright (C) 2005-2022 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 <errno.h>
  22. #include <string.h>
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include <limits.h>
  27. #ifndef HOST_NAME_MAX
  28. #define HOST_NAME_MAX 255
  29. #endif
  30. /* Windows32 version */
  31. #if defined __MINGW32__ && !defined HAVE_GETHOSTNAME
  32. #define WIN32_LEAN_AND_MEAN
  33. #include <windows.h>
  34. #include <errno.h>
  35. static int
  36. w32_gethostname (char *name, size_t len)
  37. {
  38. /* We could try the WinSock API gethostname, but that will
  39. fail if WSAStartup function has has not been called. We don't
  40. really need a name that will be understood by socket API, so avoid
  41. unnecessary dependence on WinSock libraries by using
  42. GetComputerName instead. */
  43. /* On Win9x GetComputerName fails if the input size is less
  44. than MAX_COMPUTERNAME_LENGTH + 1. */
  45. char buffer[MAX_COMPUTERNAME_LENGTH + 1];
  46. DWORD size = sizeof (buffer);
  47. if (!GetComputerName (buffer, &size))
  48. return -1;
  49. if ((size = strlen (buffer) + 1) > len)
  50. {
  51. errno = EINVAL;
  52. /* Truncate as per POSIX spec. We do not NUL-terminate. */
  53. size = len;
  54. }
  55. memcpy (name, buffer, (size_t) size);
  56. return 0;
  57. }
  58. #undef gethostname
  59. #define gethostname w32_gethostname
  60. #define HAVE_GETHOSTNAME 1
  61. #endif
  62. /* SUBROUTINE HOSTNM(NAME, STATUS)
  63. CHARACTER(len=*), INTENT(OUT) :: NAME
  64. INTEGER, INTENT(OUT), OPTIONAL :: STATUS */
  65. #ifdef HAVE_GETHOSTNAME
  66. static int
  67. hostnm_0 (char *name, gfc_charlen_type name_len)
  68. {
  69. char p[HOST_NAME_MAX + 1];
  70. int val;
  71. memset (name, ' ', name_len);
  72. size_t reqlen = sizeof (p) > (size_t) name_len + 1
  73. ? (size_t) name_len + 1: sizeof (p);
  74. val = gethostname (p, reqlen);
  75. if (val == 0)
  76. {
  77. for (gfc_charlen_type i = 0; i < name_len && p[i] != '\0'; i++)
  78. name[i] = p[i];
  79. }
  80. return ((val == 0) ? 0 : errno);
  81. }
  82. extern void hostnm_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type);
  83. iexport_proto(hostnm_i4_sub);
  84. void
  85. hostnm_i4_sub (char *name, GFC_INTEGER_4 *status, gfc_charlen_type name_len)
  86. {
  87. int val = hostnm_0 (name, name_len);
  88. if (status != NULL)
  89. *status = val;
  90. }
  91. iexport(hostnm_i4_sub);
  92. extern void hostnm_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type);
  93. iexport_proto(hostnm_i8_sub);
  94. void
  95. hostnm_i8_sub (char *name, GFC_INTEGER_8 *status, gfc_charlen_type name_len)
  96. {
  97. int val = hostnm_0 (name, name_len);
  98. if (status != NULL)
  99. *status = val;
  100. }
  101. iexport(hostnm_i8_sub);
  102. extern GFC_INTEGER_4 hostnm (char *, gfc_charlen_type);
  103. export_proto(hostnm);
  104. GFC_INTEGER_4
  105. hostnm (char *name, gfc_charlen_type name_len)
  106. {
  107. return hostnm_0 (name, name_len);
  108. }
  109. #endif