1 /* Implementation of the HOSTNM intrinsic.
2 Copyright (C) 2005, 2007 Free Software Foundation, Inc.
3 Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
5 This file is part of the GNU Fortran 95 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
41 /* Windows32 version */
42 #if defined __MINGW32__ && !defined HAVE_GETHOSTNAME
43 #define WIN32_LEAN_AND_MEAN
48 w32_gethostname (char *name
, size_t len
)
50 /* We could try the WinSock API gethostname, but that will
51 fail if WSAStartup function has has not been called. We don't
52 really need a name that will be understood by socket API, so avoid
53 unnecessary dependence on WinSock libraries by using
54 GetComputerName instead. */
56 /* On Win9x GetComputerName fails if the input size is less
57 than MAX_COMPUTERNAME_LENGTH + 1. */
58 char buffer
[MAX_COMPUTERNAME_LENGTH
+ 1];
59 DWORD size
= sizeof (buffer
);
61 if (!GetComputerName (buffer
, &size
))
64 if ((size
= strlen (buffer
) + 1) > len
)
67 /* Truncate as per POSIX spec. We do not NUL-terminate. */
70 memcpy (name
, buffer
, (size_t) size
);
76 #define gethostname w32_gethostname
77 #define HAVE_GETHOSTNAME 1
82 /* SUBROUTINE HOSTNM(NAME, STATUS)
83 CHARACTER(len=*), INTENT(OUT) :: NAME
84 INTEGER, INTENT(OUT), OPTIONAL :: STATUS */
86 #ifdef HAVE_GETHOSTNAME
87 extern void hostnm_i4_sub (char *, GFC_INTEGER_4
*, gfc_charlen_type
);
88 iexport_proto(hostnm_i4_sub
);
91 hostnm_i4_sub (char *name
, GFC_INTEGER_4
*status
, gfc_charlen_type name_len
)
96 memset (name
, ' ', name_len
);
97 p
= gfc_alloca (name_len
+ 1);
99 val
= gethostname (p
, name_len
);
104 while (i
< name_len
&& p
[++i
] != '\0')
109 *status
= (val
== 0) ? 0 : errno
;
111 iexport(hostnm_i4_sub
);
113 extern void hostnm_i8_sub (char *, GFC_INTEGER_8
*, gfc_charlen_type
);
114 iexport_proto(hostnm_i8_sub
);
117 hostnm_i8_sub (char *name
, GFC_INTEGER_8
*status
, gfc_charlen_type name_len
)
122 memset (name
, ' ', name_len
);
123 p
= gfc_alloca (name_len
+ 1);
125 val
= gethostname (p
, name_len
);
130 while (i
< name_len
&& p
[++i
] != '\0')
135 *status
= (val
== 0) ? 0 : errno
;
137 iexport(hostnm_i8_sub
);
139 extern GFC_INTEGER_4
hostnm (char *, gfc_charlen_type
);
140 export_proto(hostnm
);
143 hostnm (char *name
, gfc_charlen_type name_len
)
146 hostnm_i4_sub (name
, &val
, name_len
);