1 /* Implementation of the HOSTNM intrinsic.
2 Copyright (C) 2005-2018 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
38 #define HOST_NAME_MAX 255
42 /* Windows32 version */
43 #if defined __MINGW32__ && !defined HAVE_GETHOSTNAME
44 #define WIN32_LEAN_AND_MEAN
49 w32_gethostname (char *name
, size_t len
)
51 /* We could try the WinSock API gethostname, but that will
52 fail if WSAStartup function has has not been called. We don't
53 really need a name that will be understood by socket API, so avoid
54 unnecessary dependence on WinSock libraries by using
55 GetComputerName instead. */
57 /* On Win9x GetComputerName fails if the input size is less
58 than MAX_COMPUTERNAME_LENGTH + 1. */
59 char buffer
[MAX_COMPUTERNAME_LENGTH
+ 1];
60 DWORD size
= sizeof (buffer
);
62 if (!GetComputerName (buffer
, &size
))
65 if ((size
= strlen (buffer
) + 1) > len
)
68 /* Truncate as per POSIX spec. We do not NUL-terminate. */
71 memcpy (name
, buffer
, (size_t) size
);
77 #define gethostname w32_gethostname
78 #define HAVE_GETHOSTNAME 1
83 /* SUBROUTINE HOSTNM(NAME, STATUS)
84 CHARACTER(len=*), INTENT(OUT) :: NAME
85 INTEGER, INTENT(OUT), OPTIONAL :: STATUS */
87 #ifdef HAVE_GETHOSTNAME
89 hostnm_0 (char *name
, gfc_charlen_type name_len
)
91 char p
[HOST_NAME_MAX
+ 1];
94 memset (name
, ' ', name_len
);
96 size_t reqlen
= sizeof (p
) > (size_t) name_len
+ 1
97 ? (size_t) name_len
+ 1: sizeof (p
);
98 val
= gethostname (p
, reqlen
);
102 for (gfc_charlen_type i
= 0; i
< name_len
&& p
[i
] != '\0'; i
++)
106 return ((val
== 0) ? 0 : errno
);
109 extern void hostnm_i4_sub (char *, GFC_INTEGER_4
*, gfc_charlen_type
);
110 iexport_proto(hostnm_i4_sub
);
113 hostnm_i4_sub (char *name
, GFC_INTEGER_4
*status
, gfc_charlen_type name_len
)
115 int val
= hostnm_0 (name
, name_len
);
119 iexport(hostnm_i4_sub
);
121 extern void hostnm_i8_sub (char *, GFC_INTEGER_8
*, gfc_charlen_type
);
122 iexport_proto(hostnm_i8_sub
);
125 hostnm_i8_sub (char *name
, GFC_INTEGER_8
*status
, gfc_charlen_type name_len
)
127 int val
= hostnm_0 (name
, name_len
);
131 iexport(hostnm_i8_sub
);
133 extern GFC_INTEGER_4
hostnm (char *, gfc_charlen_type
);
134 export_proto(hostnm
);
137 hostnm (char *name
, gfc_charlen_type name_len
)
139 return hostnm_0 (name
, name_len
);