1 /* Implementation of the HOSTNM intrinsic.
2 Copyright (C) 2005, 2007, 2009 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 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"
36 /* Windows32 version */
37 #if defined __MINGW32__ && !defined HAVE_GETHOSTNAME
38 #define WIN32_LEAN_AND_MEAN
43 w32_gethostname (char *name
, size_t len
)
45 /* We could try the WinSock API gethostname, but that will
46 fail if WSAStartup function has has not been called. We don't
47 really need a name that will be understood by socket API, so avoid
48 unnecessary dependence on WinSock libraries by using
49 GetComputerName instead. */
51 /* On Win9x GetComputerName fails if the input size is less
52 than MAX_COMPUTERNAME_LENGTH + 1. */
53 char buffer
[MAX_COMPUTERNAME_LENGTH
+ 1];
54 DWORD size
= sizeof (buffer
);
56 if (!GetComputerName (buffer
, &size
))
59 if ((size
= strlen (buffer
) + 1) > len
)
62 /* Truncate as per POSIX spec. We do not NUL-terminate. */
65 memcpy (name
, buffer
, (size_t) size
);
71 #define gethostname w32_gethostname
72 #define HAVE_GETHOSTNAME 1
77 /* SUBROUTINE HOSTNM(NAME, STATUS)
78 CHARACTER(len=*), INTENT(OUT) :: NAME
79 INTEGER, INTENT(OUT), OPTIONAL :: STATUS */
81 #ifdef HAVE_GETHOSTNAME
82 extern void hostnm_i4_sub (char *, GFC_INTEGER_4
*, gfc_charlen_type
);
83 iexport_proto(hostnm_i4_sub
);
86 hostnm_i4_sub (char *name
, GFC_INTEGER_4
*status
, gfc_charlen_type name_len
)
91 memset (name
, ' ', name_len
);
92 p
= gfc_alloca (name_len
+ 1);
94 val
= gethostname (p
, name_len
);
99 while (i
< name_len
&& p
[++i
] != '\0')
104 *status
= (val
== 0) ? 0 : errno
;
106 iexport(hostnm_i4_sub
);
108 extern void hostnm_i8_sub (char *, GFC_INTEGER_8
*, gfc_charlen_type
);
109 iexport_proto(hostnm_i8_sub
);
112 hostnm_i8_sub (char *name
, GFC_INTEGER_8
*status
, gfc_charlen_type name_len
)
117 memset (name
, ' ', name_len
);
118 p
= gfc_alloca (name_len
+ 1);
120 val
= gethostname (p
, name_len
);
125 while (i
< name_len
&& p
[++i
] != '\0')
130 *status
= (val
== 0) ? 0 : errno
;
132 iexport(hostnm_i8_sub
);
134 extern GFC_INTEGER_4
hostnm (char *, gfc_charlen_type
);
135 export_proto(hostnm
);
138 hostnm (char *name
, gfc_charlen_type name_len
)
141 hostnm_i4_sub (name
, &val
, name_len
);